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

Graph example

The GraPHPite Tutorial

Graph example

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).

  1. include("graphpite.inc");
  2. // create the graph
  3. $Graph =& new GraPHP(800, 600);
  4. // create the plotarea
  5. $Graph->add(new PlotArea(), "PlotArea");
  6.  
  7. // create some colors
  8. $Graph->addColor(new Color(255, 0, 0), "Red");
  9. $Graph->addColor(new Color(255, 255, 0), "Yellow");
  10. $Graph->addColor(new Color(0, 0, 0), "Black");
  11. $Graph->addColor(new Color(255, 255, 255), "White");
  12. $Graph->addColor(new Color(0xee, 0xee, 0xee), "LightGray");
  13. $Graph->addColor(new Color(0, 0, 127), "Blue");
  14. $Graph->addColor(new Color(10, 10, 10), "Transparent");
  15.  
  16. // set the blue color to almost transparent, requires GD2
  17. $Blue->setAlpha(100);
  18.  
  19. // set the transparent to TRANSPARENT, requires GD2
  20. $Transparent->setAlpha(255);
  21.  
  22. // create a Y grid
  23. $PlotArea->addGridY(new GridBars(), "GridY");
  24. // that is light gray in color
  25. $GridY->setFillStyle($LightGray);
  26. // create the 1st dataset
  27. $DataSet1 =& new RandomDataSet(10, 20, 100, true);
  28. // create the 1st plot as smoothed area chart using the 1st dataset
  29. $PlotArea->addPlot(new SmoothedAreaChart($DataSet1), "Plot1");
  30. // create a vertical gradient fill using red and yellow, ie bottom of
  31. // graph will be yellow and the "higher" the value the more red it will
  32. // be, ie a "fire" effect
  33.  
  34. $Plot1->setFillStyle(new GradientFill(GRAD_VERTICAL, $Red, $Yellow, 100));
  35.  
  36. // create a Y data value marker
  37. $Plot1->add(new ValueMarker(PCT_Y_MAX), "Marker");
  38. // fill it with white
  39. $Marker->setFillStyle($White);
  40. // and use black border
  41. $Marker->setBorderStyle($Black);
  42. // create a pin-point marker type
  43. $Plot1->add(new AngularPointingMarker(20, $Marker), "PointingMarker");
  44. // and use the marker on the 1st plot
  45. $Plot1->setMarker($PointingMarker);
  46. // format value marker labels as percentage values
  47. $Marker->setDataPreProcessor(new FormattedData("%0.1f%%"));
  48. // create the 2nd dataset
  49. $DataSet2 =& new RandomDataSet(10, 20, 100, true);
  50. // create the 2nd plot as line chart using the 2nd dataset
  51. $PlotArea->addPlot(new LineChart($DataSet2), "Plot2");
  52. // create a dashed line style
  53. $Graph->add(new DashedLine($Black, $Transparent), "LineStyle");
  54. // set the linecharts line style to the dashed style
  55. $Plot2->setLineStyle($LineStyle);
  56. // Create a cross marker
  57. $Plot2->setMarker(new CrossMarker(), "Marker");
  58. // Create a red line
  59. $LineStyle =& new SolidLine($Red);
  60. // which is bold
  61. $LineStyle->setThickness(5);
  62. // and make the marker use this line style
  63. $Marker->setLineStyle($LineStyle);
  64. // create a 3rd dataset
  65. $DataSet3 =& new RandomDataSet(8, 10, 120, false);
  66. // create the 3rd plot as line chart using the 2nd dataset
  67. $PlotArea->addPlot(new BarChart($DataSet3), "Plot3");
  68. // set the fill style of the barchart to the almost transparent blue
  69. $Plot3->setFillStyle($Blue);
  70. // add a TrueType font
  71. $Graph->addFont(new FontTTF("c:\windows\fonts\arial.ttf"), "Arial");
  72. // set the font size to 15 pixels
  73. $Arial->setSize(15);
  74. // add a title using the created font
  75. $Graph->add(new Title("GraPHPite - Sample", $Arial));
  76.  
  77. // Show arrow heads on the axis
  78. $PlotArea->AxisX->showArrow();
  79. $PlotArea->AxisY->showArrow();
  80. // create formatting label data based on an array
  81. $ArrayData =& new ArrayData(
  82. array(
  83. 1=>"A Point",
  84. 2=>"Another point",
  85. 6=>"After a space, another point"
  86. )
  87. );
  88. // use the data label array on the X axis
  89. $PlotArea->AxisX->setDataPreprocessor($ArrayData);
  90.  
  91. // create the graph as "thumbnail of the original",
  92. // best result using GD2
  93.  
  94. $Graph->Thumbnail(400, 300);
  95. // output the Graph
  96. $Graph->Done();

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

Prev Up Next
Using Legends How to use GraPHPite GraPHPite

Documentation generated on Fri, 12 Nov 2004 08:24:41 +0100 by phpDocumentor 1.3.0RC3