1 | <?php // content="text/plain; charset=utf-8"
|
---|
2 | require_once ('jpgraph/jpgraph.php');
|
---|
3 | require_once ('jpgraph/jpgraph_scatter.php');
|
---|
4 |
|
---|
5 | // Each ballon is specificed by four values.
|
---|
6 | // (X,Y,Size,Color)
|
---|
7 | $data = array(
|
---|
8 | array(1,12,10,'orange'),
|
---|
9 | array(3,41,15,'red'),
|
---|
10 | array(4,5,19,'lightblue'),
|
---|
11 | array(5,70,22,'yellow')
|
---|
12 | );
|
---|
13 |
|
---|
14 |
|
---|
15 |
|
---|
16 | // We need to create X,Y data vectors suitable for the
|
---|
17 | // library from the above raw data.
|
---|
18 | $n = count($data);
|
---|
19 | for( $i=0; $i < $n; ++$i ) {
|
---|
20 |
|
---|
21 | $datax[$i] = $data[$i][0];
|
---|
22 | $datay[$i] = $data[$i][1];
|
---|
23 |
|
---|
24 | // Create a faster lookup array so we don't have to search
|
---|
25 | // for the correct values in the callback function
|
---|
26 | $format[strval($datax[$i])][strval($datay[$i])] = array($data[$i][2],$data[$i][3]);
|
---|
27 |
|
---|
28 | }
|
---|
29 |
|
---|
30 |
|
---|
31 | // Callback for markers
|
---|
32 | // Must return array(width,border_color,fill_color,filename,imgscale)
|
---|
33 | // If any of the returned values are '' then the
|
---|
34 | // default value for that parameter will be used (possible empty)
|
---|
35 | function FCallback($aYVal,$aXVal) {
|
---|
36 | global $format;
|
---|
37 | return array($format[strval($aXVal)][strval($aYVal)][0],'',
|
---|
38 | $format[strval($aXVal)][strval($aYVal)][1],'','');
|
---|
39 | }
|
---|
40 |
|
---|
41 | // Setup a basic graph
|
---|
42 | $graph = new Graph(450,300,'auto');
|
---|
43 | $graph->SetScale("intlin");
|
---|
44 | $graph->SetMargin(40,40,40,40);
|
---|
45 | $graph->SetMarginColor('wheat');
|
---|
46 |
|
---|
47 | $graph->title->Set("Example of ballon scatter plot with X,Y callback");
|
---|
48 | $graph->title->SetFont(FF_ARIAL,FS_BOLD,12);
|
---|
49 | $graph->title->SetMargin(10);
|
---|
50 |
|
---|
51 | // Use a lot of grace to get large scales since the ballon have
|
---|
52 | // size and we don't want them to collide with the X-axis
|
---|
53 | $graph->yaxis->scale->SetGrace(50,10);
|
---|
54 | $graph->xaxis->scale->SetGrace(50,10);
|
---|
55 |
|
---|
56 | // Make sure X-axis as at the bottom of the graph and not at the default Y=0
|
---|
57 | $graph->xaxis->SetPos('min');
|
---|
58 |
|
---|
59 | // Set X-scale to start at 0
|
---|
60 | $graph->xscale->SetAutoMin(0);
|
---|
61 |
|
---|
62 | // Create the scatter plot
|
---|
63 | $sp1 = new ScatterPlot($datay,$datax);
|
---|
64 | $sp1->mark->SetType(MARK_FILLEDCIRCLE);
|
---|
65 |
|
---|
66 | // Uncomment the following two lines to display the values
|
---|
67 | $sp1->value->Show();
|
---|
68 | $sp1->value->SetFont(FF_FONT1,FS_BOLD);
|
---|
69 |
|
---|
70 | // Specify the callback
|
---|
71 | $sp1->mark->SetCallbackYX("FCallback");
|
---|
72 |
|
---|
73 | // Add the scatter plot to the graph
|
---|
74 | $graph->Add($sp1);
|
---|
75 |
|
---|
76 | // ... and send to browser
|
---|
77 | $graph->Stroke();
|
---|
78 |
|
---|
79 | ?>
|
---|
80 |
|
---|
81 |
|
---|