Here is a full PHP code example on how to create a graph using GraPHPite, that displays
2 plots (a smoothed area chart and a linechart).
- include("graphpite.inc");
-
- // create the graph
- $Graph =& new GraPHP(800, 600);
- // create the plotarea
- $Graph->add(new PlotArea(), "PlotArea");
-
- // create some colors
- $Graph->addColor(new Color(255, 0, 0), "Red");
- $Graph->addColor(new Color(255, 255, 0), "Yellow");
- $Graph->addColor(new Color(0, 0, 0), "Black");
- $Graph->addColor(new Color(255, 255, 255), "White");
- $Graph->addColor(new Color(0xee, 0xee, 0xee), "LightGray");
- $Graph->addColor(new Color(0, 0, 127), "Blue");
- $Graph->addColor(new Color(10, 10, 10), "Transparent");
-
- // set the blue color to almost transparent, requires GD2
- $Blue->setAlpha(100);
-
- // set the transparent to TRANSPARENT, requires GD2
- $Transparent->setAlpha(255);
-
- // create a Y grid
- $PlotArea->addGridY(new GridBars(), "GridY");
- // that is light gray in color
- $GridY->setFillStyle($LightGray);
-
- // create the 1st dataset
- $DataSet1 =& new RandomDataSet(10, 20, 100, true);
- // create the 1st plot as smoothed area chart using the 1st dataset
- $PlotArea->addPlot(new SmoothedAreaChart($DataSet1), "Plot1");
- // create a vertical gradient fill using red and yellow, ie bottom of
- // graph will be yellow and the "higher" the value the more red it will
- // be, ie a "fire" effect
-
- $Plot1->setFillStyle(new GradientFill(GRAD_VERTICAL, $Red, $Yellow, 100));
-
- // create a Y data value marker
- $Plot1->add(new ValueMarker(PCT_Y_MAX), "Marker");
- // fill it with white
- $Marker->setFillStyle($White);
- // and use black border
- $Marker->setBorderStyle($Black);
- // create a pin-point marker type
- $Plot1->add(new AngularPointingMarker(20, $Marker), "PointingMarker");
- // and use the marker on the 1st plot
- $Plot1->setMarker($PointingMarker);
- // format value marker labels as percentage values
- $Marker->setDataPreProcessor(new FormattedData("%0.1f%%"));
-
- // create the 2nd dataset
- $DataSet2 =& new RandomDataSet(10, 20, 100, true);
- // create the 2nd plot as line chart using the 2nd dataset
- $PlotArea->addPlot(new LineChart($DataSet2), "Plot2");
- // create a dashed line style
- $Graph->add(new DashedLine($Black, $Transparent), "LineStyle");
- // set the linecharts line style to the dashed style
- $Plot2->setLineStyle($LineStyle);
- // Create a cross marker
- $Plot2->setMarker(new CrossMarker(), "Marker");
- // Create a red line
- $LineStyle =& new SolidLine($Red);
- // which is bold
- $LineStyle->setThickness(5);
- // and make the marker use this line style
- $Marker->setLineStyle($LineStyle);
-
- // create a 3rd dataset
- $DataSet3 =& new RandomDataSet(8, 10, 120, false);
- // create the 3rd plot as line chart using the 2nd dataset
- $PlotArea->addPlot(new BarChart($DataSet3), "Plot3");
- // set the fill style of the barchart to the almost transparent blue
- $Plot3->setFillStyle($Blue);
-
- // add a TrueType font
- $Graph->addFont(new FontTTF("c:\windows\fonts\arial.ttf"), "Arial");
- // set the font size to 15 pixels
- $Arial->setSize(15);
- // add a title using the created font
- $Graph->add(new Title("GraPHPite - Sample", $Arial));
-
- // Show arrow heads on the axis
- $PlotArea->AxisX->showArrow();
- $PlotArea->AxisY->showArrow();
-
- // create formatting label data based on an array
- $ArrayData =& new ArrayData(
- array(
- 1=>"A Point",
- 2=>"Another point",
- 6=>"After a space, another point"
- )
- );
- // use the data label array on the X axis
- $PlotArea->AxisX->setDataPreprocessor($ArrayData);
-
- // create the graph as "thumbnail of the original",
- // best result using GD2
-
- $Graph->Thumbnail(400, 300);
-
- // output the Graph
- $Graph->Done();