graphpite
[ class tree: graphpite ] [ index: graphpite ] [ all elements ]
Prev Next

Graph example

The Image_Graph Tutorial

Graph example

Here is a full PHP code example on how to create a graph using Image_Graph, that displays 2 plots (a smoothed area chart and a linechart).


1 include("graphpite.inc");
2
3 // create the graph
4 $Graph =& new Image_Graph(800, 600);
5
6
7 // add a TrueType font
8 $Arial =& $Graph->addFont(new FontTTF("c:\windows\fonts\arial.ttf"));
9 // set the font size to 15 pixels
10 $Arial->setSize(15);
11
12 // create the plotarea and title in a vertical layout (i.e. one on top of the other)
13 $Graph->add(
14 new Image_Graph_Layout_Vertical(
15 new Title("Image_Graph - Sample", $Arial),
16 $Plotarea =& new Image_Graph_Plotarea(),
17 10
18 )
19 );
20
21 // create a blue color with alpha 100
22 $BLUE =& $Graph->newColor(IMAGE_GRAPH_LIGHTBLUE, 100);
23
24 // create a Y grid
25 $GridY =& $Plotarea->addGridY(new GridBars());
26 // that is light gray in color
27 $GridY->setFillColor(IMAGE_GRAPH_LIGHTGRAY);
28
29 // create the 1st dataset
30 $Dataset1 =& new Image_Graph_Dataset_Random(10, 20, 100, true);
31 // create the 1st plot as smoothed area chart using the 1st dataset
32 $Plot1 =& $Plotarea->addPlot(new Image_Graph_Plot_Smoothed_Area($Dataset1));
33 // create a vertical gradient fill using red and yellow, ie bottom of
34 // graph will be yellow and the "higher" the value the more red it will
35 // be, ie a "fire" effect
36 $Plot1->setFillStyle(new Image_Graph_Fill_Gradient(GRAD_VERTICAL, IMAGE_GRAPH_RED, IMAGE_GRAPH_YELLOW, 100));
37
38 // create a Y data value marker
39 $Marker =& $Plot1->add(new Image_Graph_Marker_Value(IMAGE_GRAPH_PCT_Y_MAX));
40 // create a pin-point marker type
41 $PointingMarker =& $Plot1->add(new Image_Graph_Marker_Pointing_Angular(20, $Marker));
42 // and use the marker on the 1st plot
43 $Plot1->setMarker($PointingMarker);
44 // format value marker labels as percentage values
45 $Marker->setDataPreProcessor(new Image_Graph_DataPreprocessor_Formatted("%0.1f%%"));
46
47 // create the 2nd dataset
48 $Dataset2 =& new Image_Graph_Dataset_Random(10, 20, 100, true);
49 // create the 2nd plot as line chart using the 2nd dataset
50 $Plot2 =& $Plotarea->addPlot(new Image_Graph_Plot_Line($Dataset2));
51 // create a dashed line style
52 $Linestyle =& $Graph->add(new Image_Graph_Line_Dashed(IMAGE_GRAPH_BLACK, IMAGE_GRAPH_TRANSPARENT));
53 // set the linecharts line style to the dashed style
54 $Plot2->setLineStyle($Linestyle);
55
56 // Create a cross marker
57 $Marker =& new Image_Graph_Marker_Cross();
58 $Plot2->setMarker($Marker);
59 // Create a red line
60 $Linestyle =& new SolidLine(IMAGE_GRAPH_RED);
61 // which is bold
62 $Linestyle->setThickness(5);
63 // and make the marker use this line style
64 $Marker->setLineStyle($Linestyle);
65
66 // create a 3rd dataset
67 $Dataset3 =& new Image_Graph_Dataset_Random(8, 10, 120, false);
68 // create the 3rd plot as line chart using the 2nd dataset
69 $Plot3 =& $Plotarea->addPlot(new Image_Graph_Plot_Bar($Dataset3));
70 // set the fill style of the barchart to the almost transparent blue
71 $Plot3->setFillStyle($BLUE);
72
73 // Show arrow heads on the axis
74 $AxisX =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X);
75 $AxisX->showArrow();
76 $AxisY =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y);
77 $AxisY->showArrow();
78
79 // create formatting label data based on an array
80 $ArrayData =& new Image_Graph_DataPreprocessor_Array(
81 array(
82 1=>"A Point",
83 2=>"Another point",
84 6=>"After a space, another point"
85 )
86 );
87 // use the data label array on the X axis
88 $AxisX->setDataPreprocessor($ArrayData);
89
90 // create the graph as "thumbnail of the original",
91 // best result using GD2
92 $Graph->Thumbnail(400, 300);
93
94 // output the Graph
95 $Graph->Done();

See the Image_Graph Sample Page for the result of this script.

Prev Up Next
Using Legends How to use Image_Graph Image_Graph aka GraPHPite

Documentation generated on Wed, 15 Sep 2004 10:30:03 +0200 by phpDocumentor 1.2.3