PGraph, a powerful drawing component, can draw any graph based on your needs. You only need to provide data to automatically call the drawing function process and draw the processed data input. JpGraph provides multiple methods to create statistical charts, including line charts, column charts, and pie charts. JpGraph is a class library fully written in PHP and can be applied in any PHP environment.
Introduction
JpGraph is actually a chart class library, which makes it very convenient for developers to plot. Just a few lines of code can be used to draw a very dazzling chart.
JpGraph is an open-source PHP statistical chart generation library. It is built based on the PHP GD2 graphics library and encapsulates operations related to generating statistical charts, hiding some complex operations, it makes it easier to output statistical charts on the PHP page.
The official JpGraph website is.
Installation and configuration
JpGraph configuration is very simple. First, it requires the support of the GD Library. By default, the PHP base package does not support the GD Library. Open php. ini file: Open the php_gd2 extension. Note that the php file path in examples is incorrect for officially downloaded files. You need to move the file. I did this: remove the Examples file from the outer layer.
Change the SRC file name to JpGraph, and then move the JpGraph file to Examples. In this way, you can view many Examples in Examples. You only need to change some parameters to find the desired file in Examples.
Note: JpGraph requires the support of the GD Library. If you want the JpGraph class library to be valid only for the current site, you only need to copy all the files in the src folder under the Jpgraph compressed package to the folder where the website is located, you can call the specified file in the src folder for use. These content will be explained in the following typical examples.
Simple example
We can use JpGraph to quickly draw the following statistical chart.
The PHP code for the above chart is as follows:
Require_once ('examples/jpgraph. Php'); // load the basic class
Require_once ('examples/jpgraph/jpgraph_bar.php '); // load the column category class
$ Datay = array (300,230,400); // the data you want to display
$ Graph = new Graph (400,250); // create a graph. The parameter is width and height.
$ Graph-> SetScale ("textlin ");
$ Graph-> img-> SetMargin (,); // you can specify the icon margin.
$ Graph-> title-> SetFont (FF_SIMSUN, FS_BOLD, 24); // set the font, type, and size.
$ Graph-> title-> Set ('www .111cn.net '); // The title of the image header.
$ Graph-> title-> SetColor ('red'); // title font color
$ Graph-> xaxis-> SetFont (FF_FONT1); // set the English font.
$ Graph-> yaxis-> SetFont (FF_FONT1 );
$ Bplot = new BarPlot ($ datay );
$ Bplot-> SetWidth (0.4); // The width of the column
$ Bplot-> SetFillGradient ("navy", "lightsteelblue", GRAD_MIDVER );
$ Bplot-> SetColor ("navy ");
$ Graph-> Add ($ bplot );
$ Graph-> Stroke ();
Chinese font support
Note the font settings for special tables. For example, if it is set to FF_FONT1, Chinese characters cannot be correctly displayed. The method for setting Chinese fonts is as follows:
$ Graph-> title-> Set ("www.111cn.net ");
$ Graph-> title-> SetFont (FF_FONT1, FS_BOLD); // English font
$ Graph-> title-> SetFont (FF_SIMSUN, FS_BOLD); // set the font, type, and size.
$ Graph-> title-> SetColor ('red'); // you can specify the font color.
$ Graph-> title-> SetFont (FF_SIMSUN, FS_BOLD); // set the Chinese font of the title
$ Graph-> legend-> SetFont (FF_SIMSUN, FS_BOLD); // you can specify the font of a line.
$ Graph-> yaxis-> title-> SetFont (FF_SIMSUN, FS_BOLD); // Set the y axis line to indicate the font.
$ Graph-> xaxis-> title-> SetFont (FF_SIMSUN, FS_BOLD); // Set the x axis line to indicate the font.
Set the default format for generating images based on the image formats supported in the current PHP environment. You can modify the DEFAULT_GFORMAT settings to configure the default image format of JpGraph.
By default, auto indicates that JpGraph searches supported Image formats in the order of PNG, GIF, and JPEG. DEFINE ("DEFAULT_GFORMAT", "auto"); Note: If you are using JpGraph 2.3, you do not need to reconfigure it.
JpGraph help document: http://jpgraph.net/download/manuals/