1 | <?php // content="text/plain; charset=utf-8" |
---|
2 | // |
---|
3 | // Basic example on how to use custom tickmark feature to have a label |
---|
4 | // at the start of each month. |
---|
5 | // |
---|
6 | require_once ('jpgraph/jpgraph.php'); |
---|
7 | require_once ('jpgraph/jpgraph_line.php'); |
---|
8 | require_once ('jpgraph/jpgraph_utils.inc.php'); |
---|
9 | |
---|
10 | // |
---|
11 | // Create some random data for the plot. We use the current time for the |
---|
12 | // first X-position |
---|
13 | // |
---|
14 | $datay = array(); |
---|
15 | $datax = array(); |
---|
16 | $ts = time(); |
---|
17 | $n=15; // Number of data points |
---|
18 | for($i=0; $i < $n; ++$i ) { |
---|
19 | $datax[$i] = $ts+$i*700000; |
---|
20 | $datay[$i] = rand(5,60); |
---|
21 | } |
---|
22 | |
---|
23 | // Now get labels at the start of each month |
---|
24 | $dateUtils = new DateScaleUtils(); |
---|
25 | list($tickPositions,$minTickPositions) = $dateUtils->GetTicks($datax); |
---|
26 | |
---|
27 | // We add some grace to the end of the X-axis scale so that the first and last |
---|
28 | // data point isn't exactly at the very end or beginning of the scale |
---|
29 | $grace = 400000; |
---|
30 | $xmin = $datax[0]-$grace; |
---|
31 | $xmax = $datax[$n-1]+$grace; |
---|
32 | |
---|
33 | // |
---|
34 | // The code to setup a very basic graph |
---|
35 | // |
---|
36 | $graph = new Graph(400,200); |
---|
37 | |
---|
38 | // |
---|
39 | // We use an integer scale on the X-axis since the positions on the X axis |
---|
40 | // are assumed to be UNI timestamps |
---|
41 | $graph->SetScale('intlin',0,0,$xmin,$xmax); |
---|
42 | $graph->title->Set('Basic example with manual ticks'); |
---|
43 | $graph->title->SetFont(FF_ARIAL,FS_NORMAL,12); |
---|
44 | |
---|
45 | // |
---|
46 | // Make sure that the X-axis is always at the bottom of the scale |
---|
47 | // (By default the X-axis is alwys positioned at Y=0 so if the scale |
---|
48 | // doesn't happen to include 0 the axis will not be shown) |
---|
49 | $graph->xaxis->SetPos('min'); |
---|
50 | |
---|
51 | // Now set the tic positions |
---|
52 | $graph->xaxis->SetTickPositions($tickPositions,$minTickPositions); |
---|
53 | |
---|
54 | // The labels should be formatted at dates with "Year-month" |
---|
55 | $graph->xaxis->SetLabelFormatString('My',true); |
---|
56 | |
---|
57 | // Use Ariel font |
---|
58 | $graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,9); |
---|
59 | |
---|
60 | // Add a X-grid |
---|
61 | $graph->xgrid->Show(); |
---|
62 | |
---|
63 | // Create the plot line |
---|
64 | $p1 = new LinePlot($datay,$datax); |
---|
65 | $p1->SetColor('teal'); |
---|
66 | $graph->Add($p1); |
---|
67 | |
---|
68 | // Output graph |
---|
69 | $graph->Stroke(); |
---|
70 | |
---|
71 | ?> |
---|
72 | |
---|
73 | |
---|