Using Jpgraph to generate a chart in PHP

Source: Internet
Author: User

jpgraph is a PHP chart class library that generates charts for line, histogram, pie charts, and more. If you want to use PHP to generate statistics, it's convenient to use it.

If you want to use the GD library to write, then I can only worship the great God (I will not haha ha).

Anyway, first you have to make sure that your PHP has opened the GD library, in the php.ini configuration file found Extension=php_gd2.dll confirmation is not; That means that GD has been opened.

Then you can go to the online next jpgraph compression package. The extract is saved to an arbitrary folder, which then references the file, so it's best to save it to a folder you're familiar with.

1) Create a line chart

<?PHPrequire_once("jpgraph/src/jpgraph.php"); require_once("jpgraph/src/jpgraph_line.php"); $data 1=Array(523,634,371,278,685,587,490,256,398,545,367,577);//array of the first curve   $graph=NewGraph (500,300); $graph->setscale ("Textlin"); $graph-Setshadow (); $graph->img->setmargin (60,30,30,70);//Set Image Margins   $graph->graph_theme =NULL;//set subject to NULL, otherwise value->show (); Invalid   $lineplot 1=NewLinePlot ($data 1);//Create a set of two curve objects$lineplot 1->value->setcolor ("Red"); $lineplot 1->value->Show (); $graph->add ($lineplot 1);//Place a curve on an image   $graph->title->set (Iconv("UTF-8", "Gb2312//ignore", "Blog Information Statistics"));//Set Image title$graph->xaxis->title->set (Iconv("UTF-8", "Gb2312//ignore", "month"));//Set the axis name$graph->yaxis->title->set (Iconv("UTF-8", "Gb2312//ignore", "Flow")); $graph->title->setmargin (10); $graph->xaxis->title->setmargin (10); $graph->yaxis->title->setmargin (10); $graph->title->setfont (Ff_simsun,fs_bold);//Set Font$graph->yaxis->title->setfont (Ff_simsun,fs_bold); $graph->xaxis->title->setfont (Ff_simsun,fs_bold); $graph->xaxis->setticklabels ($gDateLocale-getshortmonth ()); $graph->stroke ();//Output Image

Effect

2) Bar chart

<?PHPrequire_once("jpgraph/jpgraph.php"); require_once("jpgraph/jpgraph_bar.php"); $data=Array(19,23,34,38,45,67,71,78,85,87,96,145); $ydata=Array("One", "two", "three", "four", "five", "six", "seven", "eight", "Nine", "Ten", "11", "12"); $graph=NewGraph (500,300);//Create a new graph object$graph->setscale ("Textlin");//scale Style$graph->setshadow ();//Set Shadow$graph->img->setmargin (40,30,40,50);//Set Margins  $graph->graph_theme =NULL;//set subject to NULL, otherwise value->show (); Invalid  $barplot=NewBarplot ($data);//Create a Barplot object$barplot->setfillcolor (' Blue ');//Set Color$barplot->value->show ();//Set Display numbers$graph->add ($barplot);//add a column chart to an image   $graph->title->set ("CDN Traffic Map"); $graph->xaxis->title->set ("month");//set title and X-Y axis titles$graph->yaxis->title->set ("Flow (Mbits)"); $graph->title->setcolor ("Red"); $graph->title->setmargin (10); $graph->xaxis->title->setmargin (5); $graph->xaxis->setticklabels ($ydata); $graph->title->setfont (Ff_simsun,fs_bold);//Set Font$graph->yaxis->title->setfont (Ff_simsun,fs_bold); $graph->xaxis->title->setfont (Ff_simsun,fs_bold); $graph->xaxis->setfont (Ff_simsun,fs_bold); $graph->stroke ();

Effect

3) Pie chart

require_once("jpgraph/src/jpgraph.php"); require_once("jpgraph/src/jpgraph_pie.php"); require_once("jpgraph/src/jpgraph_pie3d.php"); $data=Array(19,23,34,38,45,67,71,78,85,87,90,96); $graph=NewPiegraph (550,500); $graph-Setshadow (); $graph->title->set ("CDN Traffic Ratio"); $graph->title->setfont (Ff_simsun,fs_bold); $pieplot=NewPieplot3d ($data);//Create a Pieplot3d object$pieplot->setcenter (0.4, 0.5);//set the location of the center of the pie chart$pieplot->setlegends ($gDateLocale->getshortmonth ());//Set the legend  $graph->add ($pieplot); $graph->stroke ();

Effect

I do not know you crossing found no, looks like the histogram and pie chart text is not normal Chinese. The line chart is correct, this is because jpgraph default to convert the string to UTF8 format, if your file php file itself is UTF8 format that long equivalent to convert 2 times, so it generated garbled.

It is necessary to use the conversion encoding method Iconv ("UTF-8", "gb2312", $x), the need to convert Chinese character coding to gb2312, so that Chinese characters can be displayed normally. Refer to the first paragraph of code to get the line.

The following is a more detailed description of the blog address (thanks to technology sharing): http://blog.csdn.net/aoshilang2249/article/details/46956163

&& http://w3note.com/web/181.html && http://blog.csdn.net/liupengcheng201/article/details/44593713

Using Jpgraph to generate a chart in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.