Lucky's Graphing Library

  • Description
  • Using the library
  • Examples
License: GNU General Public License v3
Status: In Development

Tag Tags: php gd graph library

Lucky's Graphing library is a set of classes to easily create graphs. It's written in PHP and uses the GD library to draw the images. It can currently generate bar and pie charts.

Lucky's Graphing Library is designed to be very flexible, extendable and configurable. You can change a lot of settings, subclass the base graph class to create your own charts and a lot more.

It has a very simple API and all classes are fully documented, so it should be easy to start with. Also check the examples tab here. :)

Download
Lucky's Graphing Library is hosted on Launchpad. You can also request features and report bugs there. The launchpad page is here:
https://launchpad.net/lucky-graphing

You can find the download here:
https://launchpad.net/lucky-graphing/trunk

Example Charts
Bar Chart


Pie Chart
First you have to download the library, see the first tab where to find it. Extract it to a directory.

Before you can create your charts, the folder where you extracted the library, should be in your include path. You can do this is the following way:

  1.  
  2. <?php
  3. set_include_path('{PATH}'.PATH_SEPARATOR.get_include_path());
  4.  
  5. // Rest of your script
  6.  


Replace {PATH} with the full path to the directory where you extracted it.

For example you extracted the graph library in D:\webserver\charts\include, then your code will be:

  1.  
  2. <?php
  3. set_include_path('D:/webserver/charts/include/'.PATH_SEPARATOR.get_include_path());
  4.  


Or on Linux: you extracted the library in /var/www/charts/include

then your code will be:
  1.  
  2. <?php
  3. set_include_path('/var/www/charts/include/'.PATH_SEPARATOR.get_include_path());
  4.  


Now you can create the rest of your script, see Examples!
Creating a bar chart

  1.  
  2. // Include the library
  3. include_once 'Lucky/Graph/Type/Bar.php';
  4. // Create a Bar graph with height 200 (width will depend on how many bars you create)
  5. $bar = new Lucky_Graph_Type_Bar(200, 200);
  6.  
  7. // Add an item
  8. $bar -> setItem('Cars', 2500);
  9. $bar -> setItem('Bicycles', 4000);
  10. $bar -> setItem('Trucks', 2000);
  11.  
  12. // Include a legend
  13. $bar -> setLegendPosition(Lucky_Graph_Type_Bar::LEGEND_BOTTOM);
  14.  
  15. // Let the library generate the graph
  16. $bar -> draw();
  17.  
  18. // Output to screen
  19. header("Content-Type: image/png");
  20. $bar -> showImage();
  21.  
  22. // Or Save to file
  23. $bar -> saveToFile('bar.png');
  24.  


Creating a Pie chart
Creating a pie chart goes in almost the same way as a bar chart, this time you only use the Lucky_Graph_Type_Pie class.

  1.  
  2. // Include the library
  3. include_once 'Lucky/Graph/Type/Pie.php';
  4. // Create a pie graph with width 200 and height 200
  5. $pie = new Lucky_Graph_Type_Pie(200, 200);
  6.  
  7. // Add an item
  8. $pie -> setItem('Cars', 2500);
  9. $pie -> setItem('Bicycles', 4000);
  10. $pie -> setItem('Trucks', 2000);
  11.  
  12. // Include a legend
  13. $pie -> setLegendPosition(Lucky_Graph_Type_Pie::LEGEND_BOTTOM);
  14.  
  15. // Let the library generate the graph
  16. $pie -> draw();
  17.  
  18. // Output to screen
  19. header("Content-Type: image/png");
  20. $pie -> showImage();
  21.  
  22. // Or Save to file
  23. $pie -> saveToFile('pie.png');
  24.  


Documentation

For more functions: see the API reference:
http://www.return1.net/docs/graphlib