Jpgraph + php + mysql generate a bar chart (including attachments)

Source: Internet
Author: User
Very detailed comments. I hope to be helpful to anyone who is just getting started, with the code: CREATEDATABASE 'jpgraph '; // create a database USE 'jpgraph '; // create the Test table DROPTABLEIFEXISTS 'jpg _ temp '; CREATETABL... "> <LINKhref =" http

Very detailed comments, hope to be helpful to friends who are just getting started like me, with code:

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

// Database link file

  

/*

* ProjectName: jpgraph

* CreateDate: 2011-5-12

* Author: Wilr

* Description: Database link

*/

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

?>

// Generate chart class

  

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

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

Include_once ('Db _ config. 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 ('wilr 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

?>

 

Attachment:. zip (25 K)

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.