Skip to content

Axis Date Label and Value

Question:
I have a 2D array. Dates and quantities. I want to plot the quantities on the Y axis and the corresponding dates on the Y axis. Here is a dump of the array.

Array ( [0] => 5 [1] => 5 [2] => 5 [3] => 5 [4] => 5 [5] => 5 [6] => 5 [7] 
=> 5 [8] => 5 [9] => 5 [10] => 5 [11] => 5 [12] => 5 [13] => 5 [14] => 5 
[15] => 5 [16] => 0 [17] => 20 ) Array ( [0] => 2009-03-05 10:44:18 [1] => 
2009-02-26 10:44:18 [2] => 2009-02-19 10:44:18 [3] => 2009-02-12 10:44:18 
[4] => 2009-02-05 10:44:18 [5] => 2009-01-29 10:44:18 [6] => 2009-01-22 
10:44:18 [7] => 2009-01-15 10:44:18 [8] => 2009-01-08 10:44:18 [9] => 
2009-01-01 10:44:18 [10] => 2008-12-25 10:44:18 [11] => 2008-12-18 10:44:18 
[12] => 2008-12-11 10:44:18 [13] => 2008-12-04 10:44:18 [14] => 2008-11-27 
10:44:18 [15] => 2008-11-20 10:44:18 [16] => 2009-07-10 15:19:23 [17] => 
2013-01-15 14:36:11 )

So data point [0] would have the plot 5 on the Y axis and date of 2009-03-05 10:44:18 on the X axis. This isn't in any order at the moment but will be in ascending order of date.

Answer:

The issue is the source data isn't in the correct form. After data retreved from MySQL, you need to "paired" them this way:

e.g.
$line1=array(array('2009-09-30', 5), array('2009-10-30', 6), array('2009-11-30', 9), array('2009-12-30', 11), array('2009-01-30', 8));

Once the data is formed correctly, you can just copy and rest from the rotating exmaple: http://phpchart.org/examples/rotating-labels/

Feedback and Knowledge Base