First, let's introduce the PHP libchart we use in this article. This is a foreigner (a person of non-Chinese nationality, known as a foreigner !) Free Class for writing! Non-
It is often simple. As you can see, we want to use a PHP program written by a foreigner to create a bar chart, a pie chart, and a linear graph class. If you want to learn how to write this class, you can also download this class and look at the ideas of foreigners!
Let's get started. First download this class and click here:
Libchart
, Decompress!
In the first example, we create a vertical bar chart:
First, we include this class file in our program and instantiate this class. The program is as follows:
Requir_once ('./libchart/classes/libchart. php ');
$ Chart = new verticalbarchart (500,250); // The parameter indicates the width and height of the image to be created.
$ Dataset = new xydataset (); // instantiate an XY axis data object
// Add four sets of data for this object. The first parameter of the Point Object indicates the X axis coordinate, and the second parameter indicates the Y axis coordinate.
$ Dataset-> addpoint (new point ("Jan 2005", 273 ));
$ Dataset-> addpoint (new point ("Feb 2005", 321 ));
$ Dataset-> addpoint (new point ("March 2005", 442 ));
$ Dataset-> addpoint (new point ("Hangzhou l 2005", 711 ));
// Pass the data set to the graphic object
$ Chart-> setdataset ($ dataset );
// Set the title of the image and render it as a png file
$ Chart-> setTitle ("Monthly usage for www.example.com ");
$ Chart-> render ("generated/demo1.png"); // a path and file name are required here.
Just a simple bar chart as beautiful as it is.
PHP bar chart
The process of creating a pie chart in PHP is similar to that in the bar chart mentioned above.
The specific instance code is as follows:
$chart = new PieChart(500, 250);
$dataSet = new XYDataSet();
$dataSet->addPoint(new Point("Mozilla Firefox (80)", 80));
$dataSet->addPoint(new Point("Konqueror (75)", 75));
$dataSet->addPoint(new Point("Other (50)", 50));
$chart->setDataSet($dataSet);
$chart->setTitle("User agents for www.example.com");
$chart->render("generated/demo2.png");
Create a pie chart
It is worth mentioning that it is the Point parameter. The first parameter is the name of the occupied proportion, and the second parameter is the data. There is no XY sit
As shown in the following figure:
Pie Chart
Create a linear graph,
This is a bit different from the two above, that is, the data set. We also see the XYDataSet data set,
In addition, this class also has a data set XYSeriesDataSet (), which is actually a larger set than XYDataSet. Its element is
XYDataSet, let's take a look at the example and the Program for creating a PHP linear graph. You will surely understand:
Include "../libchart/classes/libchart. php ";
$ Chart = new LineChart (500,250 );
$ Serie1 = new XYDataSet ();
$ Serie1-> addPoint (new Point ("06-01", 273 ));
$ Serie1-> addPoint (new Point ("06-02", 421 ));
$ Serie1-> addPoint (new Point ("06-03", 642 ));
$ Serie1-> addPoint (new Point ("06-04", 799 ));
$ Serie1-> addPoint (new Point ("06-05", 1009 ));
$ Serie1-> addPoint (new Point ("06-06", 1106 ));
$ Serie2 = new XYDataSet ();
$ Serie2-> addPoint (new Point ("06-01", 280 ));
$ Serie2-> addPoint (new Point ("06-02", 300 ));
$ Serie2-> addPoint (new Point ("06-03", 212 ));
$ Serie2-> addPoint (new Point ("06-04", 542 ));
$ Serie2-> addPoint (new Point ("06-05", 600 ));
$ Serie2-> addPoint (new Point ("06-06", 850 ));
// These seem no different from the previous two. The key lies in the following section and you need to understand it yourself:
$ DataSet = new XYSeriesDataSet ();
$ DataSet-> addSerie ("Product 1", $ serie1 );
$ DataSet-> addSerie ("Product 2", $ serie2 );
$ Chart-> setDataSet ($ dataSet );
$ Chart-> setTitle ("Sales for 2006 ");
$ Chart-> render ("generated/demo3.png ");
Draw a picture like this:
Linear chart
Then, the data is handed over to the graph instance, and then the graph is rendered, which is the same as creating a bar chart and a linear graph. In fact, many bar charts and pie charts are created abroad.
And some
A lot of NB, such as combining flash with web css, is relatively simple to use, even though it is easy to use!