Jpgraph + PHP + MySQL generate a bar chart (including code)

Source: Internet
Author: User
Tags add time

Jpgraph + PHP + MySQL generates a bar chart (including code) and has a detailed explanation!

I. first create a database and a test table:

Create Database 'jpgraph'; // create a database

Use 'jpgraph ';

// Create a test table

Drop table if exists 'jpg _ temp ';

Create Table 'jpg _ temp '(

'Year' int (11) not null,

'Money' float not null,

'Number' int (11) 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), (100, 2013,140,180), (2014,150,200), (2015,130,110), (2016,110.85, 150), (2020,180,230 ), (2050,200,210 );

Ii. MySQL. php

<? PHP

// Database link file

$ Dbconn = mysql_connect ("localhost", "root", "SYC ");

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 );

?>

3. Generating chart index. php

<? PHP

Date_default_timezone_set ("PRC"); // add time to prevent error

// Generate chart class

Require_once ('jpgraph/jpgraph. php'); // load the basic class

Require_once ('jpgraph/jpgraph_bar.php '); // load the column chart

Include_once ('mysql. php'); // load the data processing file

$ Graph = New Graph (900,500); // create a chart and specify the size.

$ Graph-> setscale ("textlin"); // sets the coordinate scale type.

$ Graph-> IMG-> setmargin (40,180,); // you can specify the left, right, top, and bottom margins of a Statistical Chart.

// $ Graph-> setmargincolor ("lightblue"); // you can specify the background color of the canvas.

// $ Graph-> setbackgroundimage('stship.jpg ', bgimg_copy); // set the background image

// $ Graph-> IMG-> setangle (45); // you can specify the image angle.

// Set the title information

$ Graph-> title-> set ('syc test Report'); // you can specify the title of a Statistical Chart.

$ Graph-> title-> setfont (ff_simsun, fs_bold, 20); // set the title Font

$ Graph-> title-> setmargin (3); // you can specify the margin of the title.

// Set the 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); // scale font size on the X axis

$ Graph-> xaxis-> setcolor ('black'); // scale Color of the X axis

$ Graph-> xaxis-> setticklabels ($ datax); // you can specify the X axis marker.

$ Graph-> xaxis-> setlabelangle (0); // you can specify the angle of the value displayed on 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 @ 8080'); // X, Y cross tabulation color and transparency @ is the degree value

$ Graph-> yaxis-> Scale-> setgrace (0); // you can set the flexibility of the Y axis value)

// Set Data

$ Bplot1 = new barplot ($ datay );

$ Bplot2 = new barplot ($ number );

// Set the color and transparency of the bar chart

$ Bplot1-> setfillcolor ('Orange @ 100 ');

$ Bplot2-> setfillcolor ('brown @ 100 ');

// Set the value display

$ Bplot1-> value-> show (); // display the value

$ Bplot1-> value-> setfont (ff_simsun, fs_bold, 10); // display the font size

$ Bplot1-> value-> setangle (90); // display Angle

$ Bplot1-> value-> setformat ('% 0.2f'); // display format: 0.2f: accurate to the last two digits of the minor shard

$ Bplot2-> value-> show ();

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

$ Bplot2-> value-> setangle (90 );

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

// Set the label of the graphic column

$ Graph-> legend-> setfillcolor ('lightblue @ 000000'); // you can specify the background color and transparency of the label.

$ Graph-> legend-> pos (0.01, 0.12, "right", "center"); // location

$ Graph-> legend-> setfont (ff_simsun, fs_normal, 10); // display the font size

$ Bplot1-> setlegend ('consumption amount (unit: 10 million RMB )');

$ Bplot2-> setlegend ('number of people (unit: 10 thousand times )');

// Set the color and shadow transparency of each bar chart

$ Bplot1-> setshadow ('black @ 100 ');

$ Bplot2-> setshadow ('black @ 100 ');

// Generate a chart Column

$ Gbarplot = new groupbarplot (Array ($ bplot1, $ bplot2 ));

$ Gbarplot-> setwidth (0.5); // The width of the column

$ Graph-> Add ($ gbarplot );

$ Graph-> stroke (); // output image

?>

4. generated charts

 

 

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.