Jpgraph+php+mysql to generate a histogram (including accessories)

Source: Internet
Author: User
Tags array mysql set background create database

Very detailed comments, I hope to be like me just getting started with the help of Friends, with code:

CREATE DATABASE ' jpgraph '; Creating a Database

Use ' jpgraph ';

To create a test table

DROP TABLE IF EXISTS ' jpg_temp ';

CREATE TABLE ' Jpg_temp ' (

' Year ' int (one) is not NULL,

' Money ' float is not NULL,

' Number ' int (one) not NULL

) Engine=innodb DEFAULT charset=latin1;

Test data

Insert INTO ' jpg_temp ' ("Year", ' money ', ' number ') values (2007,150.78,120), (2008,100,90), (2009,120,100), (2010,100,78 ), (2011,70.56,60), (2012,90,100), (2013,140,180), (2014,150,200), (2015,130,110), (2016,110.85,150), (2020,180,230), (2050,200,210);

Database link File

  

/*

* Projectname:jpgraph

* CREATEDATE:2011-5-12

* AUTHOR:WILR

* Description: Database links

*/

$dbConn =mysql_connect ("127.0.0.1", "root", "root");

if (! $dbConn)

Echo ' database communication failed ';

mysql_select_db ("Jpgraph");

mysql_query ("Set names ' UTF8 '", $dbConn);

$sql = "SELECT * from Jpg_temp";

$result =mysql_query ($sql, $dbConn);

$rowCount =mysql_num_rows ($result);

$datay =array ();

$datax =array ();

$number =array ();

while ($row =mysql_fetch_array ($result)) {

$datay []= $row ["money"];

$datax []= $row ["Year"];

$number []= $row ["number"];

}

Echo each ($datay);

Print_r ($datay);

Mysql_close ($dbConn);

?>

To generate a chart class

  

Require_once (' jpgraph/jpgraph.php '); Load base class

Require_once (' jpgraph/jpgraph_bar.php '); Load Column Chart

Include_once (' db_config.php '); Loading data processing files

$graph =new graph (900,500); Create a chart to specify the size

$graph->setscale ("Textlin"); To set a coordinate scale type

$graph->img->setmargin (40,180,30,40)//SET Statistics chart margins left, right, top, bottom

$graph->setmargincolor ("LightBlue");//Set canvas background color light blue

$graph->setbackgroundimage (' stship.jpg ', bgimg_copy); Set background picture

$graph->img->setangle (45); Set the angle of a graphic in an image

Set Header information

$graph->title->set (' WILR test Report '); Set the chart title

$graph->title->setfont (ff_simsun,fs_bold,20); Set Title font

$graph->title->setmargin (3);//Set the margin of the caption

Set X axis Information

$graph->xaxis->title->set (' (Unit: Year) '); Title

$graph->xaxis->title->setfont (ff_simsun,fs_bold,10); Title Font Size

$graph->xaxis->title->setcolor (' black ');//Color

$graph->xaxis->setfont (ff_simsun,fs_bold,10);//x axis tick font size

$graph->xaxis->setcolor (' black ');//x axis tick color

$graph->xaxis->setticklabels ($datax); Set X-axis markers

$graph->xaxis->setlabelangle (0);//Set the angle of the display value of the x axis;

Set the y-axis information

$graph->yaxis->setfont (ff_simsun,fs_bold,10);//title

$graph->yaxis->setcolor (' black ');//Color

$graph->ygrid->setcolor (' black@0.9 ');//x,y cross table color and transparency @ as the degree value

$graph->yaxis->scale->setgrace (0)//Set Y axis display value flexibility (explain a bit of a problem oh forgive)

Set data

$bplot 1 = new Barplot ($datay);

$bplot 2 = new Barplot ($number);

Set columnar column color and transparency

$bplot 1->setfillcolor (' orange@0.4 ');

$bplot 2->setfillcolor (' brown@0.4 ');

Set Value display

$bplot 1->value->show (); Display values

$bplot 1->value->setfont (ff_simsun,fs_bold,10);//Display Font size

$bplot 1->value->setangle (90); Display angle

$bplot 1->value->setformat ('%0.2f '); Display format 0.2f: 2 digits after the decimal points

$bplot 2->value->show ();

$bplot 2->value->setfont (ff_simsun,fs_bold,10);

$bplot 2->value->setangle (90);

$bplot 2->value->setformat ('%0.0f ');

Set up diagram column labels

$graph->legend->setfillcolor (' lightblue@0.9 ');//Set Chart column label background color and transparency

$graph->legend->pos (0.01,0.12, "right", "center");//Position

$graph->legend->setfont (ff_simsun,fs_normal,10);//Display Font size

$bplot 1->setlegend (' Consumption amount (unit: million) ');

$bplot 2->setlegend (' number of passengers per person ');

Set the color and shadow transparency for each bar chart

$bplot 1->setshadow (' black@0.4 ');

$bplot 2->setshadow (' black@0.4 ');

To generate a chart column

$gbarplot = new Groupbarplot (Array ($bplot 1, $bplot 2));

$gbarplot->setwidth (0.5); Width of column

$graph->add ($gbarplot);

$graph->stroke (); Output image

?>

Attachment: effect map. zip (K)



Related Article

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.