Menu

Combinaition odf Examples 5.1 and 5.38

jon smith
2015-09-21
2015-09-22
  • jon smith

    jon smith - 2015-09-21

    I've been working on using the code from examples 5.1 and 5.38 to create a line graph that displays points based on a timestamp. The X axis has 24 hour points, and the Y axis count up to 50. It looks like in the 5.38 example the data is random, but I cannot figure out how to use specific data. I have arrays ($hour, $min, $sec) and a variable that contains the total (to make sure Y axis counts high enough). Basically, I would like to plot a point each time there is a new timestamp, and its value should be the total numnber of items so far.

    In actuality, the plot is for keeping track of the number of cigarettes smoked per day:

    {
        while($row=mysqli_fetch_assoc($result))
        {
            $smokehour[$i] = $row['hour'];
            $smokemin[$i] = $row['min'];
            $smokesec[$i] = $row['sec'];
        $i+=1;
        }
    }
    $totalsmokes = $i;
    
    # Timestamp for the first data point:
    $i = 1;
    $time0 = mktime($smokehour[1], $smokemin[1], $smokesec[1], $month, $day, $year); // H M S m d y
    
    mt_srand(1); // For repeatable, identical results
    
    # Build the PHPlot data array from the base data, and base timestamp.
    $data = array();
    $ts = $time0;
    $tick_anchor = NULL;
    $d = 5; // Data value
    for ($i = 0; $i < $n_points; $i++) {
    
        # Decode this point's timestamp as hour 0-23:
        $hour = date('G', $ts);
    
        # Label noon data points with the weekday name, all others unlabelled.
        $label = ($hour == 12) ? strftime('%A', $ts) : '';
    
        # Remember the first midnight datapoint seen for use as X tick anchor:
        if (!isset($tick_anchor) && $hour == 0)
            $tick_anchor = $ts;
    
        # Make a random data point, and add a row to the data array:
        $d += mt_rand(-200, 250) / 100; 
        $data[] = array($label, $ts, $d);
    
        # Step to next hour:
        $ts += 3600;
    }
    
     

    Last edit: lbayuk 2015-09-22
  • lbayuk

    lbayuk - 2015-09-22

    Note: I edited your post above to put markers around your code (a line with 4 tildes), to prevent Sourceforge from formatting it in strange ways.

    Example 5.38 is actually plotting a histogram, which sounds like what you are also trying to do. However, if your data is what I think, you first need to 'bin' the data points. This is where you count up all the points that 'fall' into each 'bin', and that is the number you plot. For example, each bin for a timeline histogram could be an hour, or a day.

    A typical way to do this is to loop over the original data and increment $counts[$bin], where $bin is what bin the data point falls into. Then you are going to plot the $counts array (after converting it into a PHPlot data array).

    It does sound like you want a histogram: "I would like to plot a point each time there is a new timestamp, and its value should be the total number of items so far." But I don't see what your histogram bins should be. Your timestamp resolution is 1 second, but I don't think you want 1 second bins in a histogram. Or perhaps I'm not understanding what you are trying to do.

     
  • jon smith

    jon smith - 2015-09-22

    Yes that sounds right, except i'm plotting data for the current day, so the total number for the day is an unknown. Could I do 5 minute increments (288 total) and write a method to sort the stamps into (288) 5 minute buckets by totalling minutes anld looping through incrementing by 5 and moving items at <5, <10, <15...<1440 and only plot up to the most recent timestamp, leaving the rest of the graph blank? Or, would I need to set an end point on the 24th hour so it shows the whole graph?

    I'm just starting with phplot, though the examples are incredibly helpful. For the above way, do you know how the arrays would need to be structured?

     
  • jon smith

    jon smith - 2015-09-22

    It looks to be getting close, here is the data i'm using to make this plot: http://smith-digital.com/nutri/plots/smokes-daily.php

    I tried setting the amount for the step to the next point as the next stamp ($i+2) minus the current stamp ($i+1) but it breaks it..

    Sample Data:

    hour min sec
    10 23 24
    10 25 47
    10 40 1
    11 5 1
    11 52 1
    12 25 1
    12 48 1
    13 35 1
    14 5 1
    15 50 1
    15 58 1
    16 15 1
    17 25 1

    Current code:

        while($row=mysqli_fetch_assoc($result))
        {
            $smokehour[$i] = $row['hour'];
            $smokemin[$i] = $row['min'];
            $smokesec[$i] = $row['sec'];
        $i+=1;
        }
    }
    $totalsmokes = $i;
    
    # Timestamp for the first data point:
    $i = 1;
    $time0 = mktime(0, 0, 1, $month, $day, $year); // H M S m d y
    
    //mt_srand(1); // For repeatable, identical results
    
    # Build the PHPlot data array from the base data, and base timestamp.
    $data = array();
    $ts = $time0;
    $tick_anchor = NULL;
    $d = 0; // Data value
    for ($i = 0; $i < $totalsmokes+1; $i++) {
    
        # Decode this point's timestamp as hour 0-23:
        $hour = $smokehour[$i+1];
    
        # Label noon data points with the weekday name, all others unlabelled.
        //$label = ($hour == 12) ? strftime('%A', $ts) : '';
        $label = "";
        # Remember the first midnight datapoint seen for use as X tick anchor:
        if (!isset($tick_anchor) && $hour == 0)
            $tick_anchor = $ts;
    
        # Make a random data point, and add a row to the data array:
        $d += 1;
        $data[] = array($label, $ts, $d);
    
        # Step to next hour:
        //$ts += ($smokehour[$i+2] - $smokehour[$i+1]);
        $ts += 3600;
    }
    
    $plot = new PHPlot(800, 800);
    
     

    Last edit: jon smith 2015-09-22
  • jon smith

    jon smith - 2015-09-22

    I did some more reading and ended up getting it, thanks for your help!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.