Menu

Proportion Graph for some dates within a year?

Stefanie2
2013-01-26
2013-02-18
  • Stefanie2

    Stefanie2 - 2013-01-26

    Hello,
    i have had some difficulty gettin startet with PHPlot. Now it runs very well and i undestand most of it. But i have a problem to make a Graph proportional:
    We collect data in a mysql database. There is: (temperature, pieces and date)
    The graph shows pieces and tempratures on Y and date on X.
    Because data would not collected on every day the output data is not proportional.
    E.g. we have 90 days in a one year graph, instead of 365. How can i get the graph to show all 365 day, filled with data points from 90 day. So the curve can be proportional?
    Any idea?

     
  • lbayuk

    lbayuk - 2013-01-30

    (Sorry for the delayed response... looks like the RSS notification isn't 100% so I didn't see this post)

    If I understand correctly, you have data for selected dates (which are along the X axis), but you want a uniform scale (1 unit = 1 day) on X. There are 2 ways you can do this.

    First, you can use 'text-data' data type, with implied X, and just insert empty rows in your array for the days where you have no data. Those rows would have empty strings for the 2 Y positions for temperature and pieces. Depending on how you build your data array, this might be the simpler solution, or might not be worth the trouble.

    The second way is to use a data type with explicit "X" values: 'data-data'. In this case, your data array rows will look like ('label', date, temperature, pieces). The second value is X, which represents the date. You only need rows where you have data, but the 'date' value is used to position the point along the X axis. For the date (X) values, you could use actual timestamps (number of seconds since a base date), or maybe just the number of days since some reference date. A factor to consider is how you want your X axis to be labeled. If you are using no labels, or the 'label' values from the data array, then the actual X values do not matter. But if you want PHPlot to make tick labels from your X values, you need to take that into account.

    Does this help?

     
  • Stefanie2

    Stefanie2 - 2013-01-31

    Hello ibayuk,
    thank you very much for your answer and time.
    Yes, you understood what id like to say :)

    Solution 1: Do i have to fill my real database with empty strings (thats what i would not prefer)? Or is there a way to fill the array only for PHPlot (what i love very much. Thank you for building this)

    Solution 2: I am not sure if i undestood correct: I Use all my Data on Y Position and map it to a selfmade X Axis lookalike X= 01.01.13 plus 1 ( for i= 365)

    May you help me with a little e.g. Code?

    Actually it is:
    $data[] = array(strtotime($arr['date1']),$arr['pieces'],$arr['temp']);

    It would be nice from you helpin me.
    Maybe it is possible to Donate for your time?
    lg
    Steffi

     
  • lbayuk

    lbayuk - 2013-01-31

    No you would not want to fill your database with empty strings. If the PHPlot data array comes right out of the database (just as a query result, with no intermediate processing in PHP), then it probably would not be convenient to use this approach - padding your data array with extra date rows.

    With a little more information, I should be able to give you some code. (BTW, thanks but this project doesn't take donations.)

    What does $arr['date1'] look like? I assume this $arr is a row from your query result. You are using strtotime() which gives you an integer timestamp value, and then putting that into your data array in the label position, correct? Why is that? Are you doing anything in PHPlot with those labels?

    I hate to jump ahead and guess, but to save some time I think you are looking for something like this:

    Build up the array:

    $data[] = array('', strtotime($arr['date1']), $arr['pieces'], $arr['temp']);
    

    Additional setup for the PHPlot object (here '$plot'):

    $plot->SetDataType('data-data');
    $plot->SetXLabelType('time', '%Y-%m-%d');
    

    This moves the timestamp from the label position to the X position in the data array, changes your data type to indicate explicit X values, and tells PHPlot to format the X tick labels as date strings like this: "2012-01-31". Even if this works, it will likely need some tuning up, but maybe this will get you started in the right direction.

     
  • Stefanie2

    Stefanie2 - 2013-02-14

    Hello Ibayuk,
    yes you are right it are all rows of a table. The additional '' in $data together with the line beyond does not work (The: $plot->SetXLabelType('time', '%Y-%m-%d') is what i use for date1);.
    So I reduce data to 'name' 'pieces' and 'date1'
    Here is a small example of my query:
    name pieces date1
    Smith 12 2013-01-01
    Minder 9 2013-01-08
    Hassfoort 21 2013-01-09
    Mornk 31 2013-01-09
    Bergz 5 2013-01-25
    Preien 63 2013-01-26
    Belham 28 2013-01-31
    Brant 21 2013-02-10

    For a longer period it is necessary to "really see" periods of needed pieces over a year.
    Mostly to compare with other years. It is the only way to have an impression of 'seasons'
    If you like (and tell me how) i add a screenshot from the graph.
    Because it is only local, i cant post a link to it.
    I would be happy if you find a way for this.
    Bye
    Stefanie

     
  • lbayuk

    lbayuk - 2013-02-14

    It seems I didn't really understand what you were trying to do. I thought you were making a line graph (or something similar, but not a bar graph), with dates along the X axis, and 2 plot lines: one for 'pieces' and one for 'temperature'. But that wouldn't make sense, because you would have two different Y scales (this is possible - one left and one right - but it is a lot more complicated).

    Now I see from your query result that there are duplicate dates (2013-01-09 here). How are these to be plotted? Also your data now has 'name', and no longer has 'temperature'.

    Yes, perhaps a picture would help me understand. If you have a local image file, you can attach it to a post in this discussion forum by clicking on the "Add attachment" link which is below where you enter your text. This changes the page to have a Browse... button. Click that and navigate to your image file. When you post your reply, it will also upload your image and attach it to your post.

     
  • Stefanie2

    Stefanie2 - 2013-02-17

    For learning i reduce data only to datum and pieces.
    The only thing i want is filling up x-axis with all dates between 1.1.13 and last entry (one day = one tick)
    This is to estimate seasons of high fluctuation. But with this unproportional view i cant use it for this.
    I thought about filling up with PHP ($array= 1.1.13 +1 365 times and if having data on a date, value from there) but hoping there is an easier way....

    Edit: I forgot one thing: I sum the pieces.

     

    Last edit: Stefanie2 2013-02-17
  • lbayuk

    lbayuk - 2013-02-18

    Here is a sample script that plots data on selected dates, with the X axis representing the first 6 months of the year 2012. The dates are 'proportional', to use your term, meaning 1 day = 1 unit along the graph. I have also set the tick increment to 7 days, so you get 1 tick mark and label per week. This isn't perfect (daylight saving time changes mess up the assumed length of a week), but it makes for a simple example. Output image is attached below.

    <?php
    require_once 'phplot.php';
    
    # Input data: result rows with a date (YYYY-MM-DD) and 2 numbers.
    $result = array(
      array('2012-01-01', 56, 20),
      array('2012-02-03', 49, 20),
      array('2012-02-08', 37, 20),
      array('2012-03-06', 22, 20),
      array('2012-04-16',  5, 20),
      array('2012-04-29', 23, 20),
      array('2012-05-12', 14, 20),
      array('2012-05-26', 32, 20),
      array('2012-06-03', 19, 20),
      array('2012-06-20', 44, 20),
    );
    
    # Build data array from the result rows:
    $data = array();
    foreach ($result as $row) {
        list($date, $n1, $n2) = $row;
        $data[] = array('', strtotime($date), $n1, $n2);
    }
    
    $plot = new PHPlot(800, 600);
    $plot->SetDataType('data-data');
    $plot->SetDataValues($data);
    $plot->SetPlotType('linepoints');
    
    # Set the X axis range to the first 6 months of 2012, and Y to 0 to 60:
    $plot->SetPlotAreaWorld(strtotime('2012-01-01'), 0,
                            strtotime('2012-06-30'), 60);
    # Use formatted date labels along the X axis, turned 90 degrees to fit:
    $plot->SetXLabelType('time', '%Y-%m-%d');
    $plot->SetXLabelAngle(90);
    
    # Set 1 week tick step, but note this is faulty for daylight saving time.
    $plot->SetXTickIncrement(3600*24*7);
    
    $plot->DrawGraph();
    
     
  • Stefanie2

    Stefanie2 - 2013-02-18

    Hi Ibayuk,
    thank you VERY much. It works!!
    One cosmetic thing i have:
    Instead of: $plot->SetPlotAreaWorld(strtotime('2012-01-01'), 0, strtotime('2012-12-31'));
    I would like to have: $plot->SetPlotAreaWorld(strtotime('$year-01-01'), 0,strtotime('$year-12-31'));
    But so the x-axis is 1970-01-01 only how does the format have to look.

    Edit: (Sorry wastin your time: google helped. It is:
    (strtotime($year.'-01-01'), 0, strtotime($year.'-12-31'))

    So coooooooooooooool
    mfg
    Stefffffiiii

     

    Last edit: Stefanie2 2013-02-18
  • lbayuk

    lbayuk - 2013-02-18

    (Don't worry, you are not wasting my time, and anything here is searchable for other people too.)

    The values for the X axis, in your data array and in the SetPlotAreaWorld() method Xmin and Xmax arguments, are "timestamp" values, meaning the number of seconds since a base date/time. Typically the base date is 1/1/1970 at UTC midnight (hence if you give strtotime() something it does not like, it returns FALSE, which may be seen as 0, and you get 1970-1-1 as the result).

    Common ways to make a dynamic timestamp values include:

    strtotime("$year-$month-$day")
    
    mktime($hour, $minute, $second, $month, $day, $year)
    

    Keep in mind there are many pitfalls doing math with dates, including assuming that $timestamp1 + 24*60*60 always creates a timestamp for the next day. (It doesn't, when there are leap seconds, or daylight saving time.)

     

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.