source: trunk/xgraph/jpgraph/Examples/combgraphex1.php

Last change on this file was 42, checked in by marrucho, 10 years ago
File size: 2.7 KB
Line 
1<?php // content="text/plain; charset=utf-8"
2require_once ('jpgraph/jpgraph.php');
3require_once ('jpgraph/jpgraph_line.php');
4require_once ('jpgraph/jpgraph_bar.php');
5require_once ('jpgraph/jpgraph_utils.inc.php');
6require_once ('jpgraph/jpgraph_mgraph.php');
7
8//------------------------------------------------------------------
9// Create some random data for the plot. We use the current time for the
10// first X-position
11//------------------------------------------------------------------
12$datay = array();
13$datax = array();
14$ts = time();
15$n=70; // Number of data points
16for($i=0; $i < $n; ++$i ) {
17    $datax[$i] = $ts+$i*150000; 
18    $datay[$i] = rand(5,60);
19    $datay2[$i] = rand(1,8);
20}
21
22// Now get labels at the start of each month
23$dateUtils = new DateScaleUtils();
24list($tickPositions,$minTickPositions) = $dateUtils->getTicks($datax,DSUTILS_MONTH1);
25
26// Now create the real graph
27// Combine a line and a bar graph
28
29// We add some grace to the end of the X-axis scale so that the first and last
30// data point isn't exactly at the very end or beginning of the scale
31$grace = 400000;
32$xmin = $datax[0]-$grace;
33$xmax = $datax[$n-1]+$grace;;
34
35// Overall width of graphs
36$w = 450;
37// Left and right margin for each graph
38$lm=25; $rm=15; 
39
40//----------------------
41// Setup the line graph
42//----------------------
43$graph = new Graph($w,250);
44$graph->SetScale('linlin',0,0,$xmin,$xmax);
45$graph->SetMargin($lm,$rm,10,30);
46$graph->SetMarginColor('white');
47$graph->SetFrame(false);
48$graph->SetBox(true);
49$graph->title->Set('Example of combined graph');
50$graph->title->SetFont(FF_ARIAL,FS_NORMAL,14);
51$graph->xaxis->SetTickPositions($tickPositions,$minTickPositions);
52$graph->xaxis->SetLabelFormatString('My',true);
53$graph->xgrid->Show();
54$p1 = new LinePlot($datay,$datax);
55$graph->Add($p1);
56
57//----------------------
58// Setup the bar graph
59//----------------------
60$graph2 = new Graph($w,110);
61$graph2->SetScale('linlin',0,0,$xmin,$xmax);
62$graph2->SetMargin($lm,$rm,5,10);
63$graph2->SetMarginColor('white');
64$graph2->SetFrame(false);
65$graph2->SetBox(true);
66$graph2->xgrid->Show();
67$graph2->xaxis->SetTickPositions($tickPositions,$minTickPositions);
68$graph2->xaxis->SetLabelFormatString('My',true);
69$graph2->xaxis->SetPos('max');
70$graph2->xaxis->HideLabels();
71$graph2->xaxis->SetTickSide(SIDE_DOWN);
72$b1 = new BarPlot($datay2,$datax);
73$b1->SetFillColor('teal');
74$b1->SetColor('teal:1.2');
75$graph2->Add($b1);
76
77//-----------------------
78// Create a multigraph
79//----------------------
80$mgraph = new MGraph();
81$mgraph->SetMargin(2,2,2,2);
82$mgraph->SetFrame(true,'darkgray',2);
83$mgraph->Add($graph);
84$mgraph->Add($graph2,0,240);
85$mgraph->Stroke();
86
87?>
88
89
Note: See TracBrowser for help on using the repository browser.