Php generate a graph (Libchart) instance _ PHP

Source: Internet
Author: User
Statistical graphs are the data graphs we often see. if three arrays are displayed in graphs or the buildings are directed at graphs, we will use graphs, next we will introduce a phpLIbchart graph generation statistical graph that we will often see as a data graph. if three arrays are displayed in graphs or the real estate is directed at graphs, we will use graphs, next I will introduce a php LIbchart graph generation class. For more information, see.
The following class can be used directly if it is simple and full of numbers or English (libchart class can be downloaded by Baidu)
The code is as follows:
/*
Update by Leo
It's draw the pic of Sheet, and it will take all the num on the pic.
*/
Require "./libchart/classes/libchart. php ";
Class drawPic {
Var $ chart;
Var $ style;
Function drawPic ($ style = "1", $ width = "500", $ height = "250 "){
$ This-> style = $ style;
If ($ style = 1 ){
// Cylinder
$ This-> chart = new VerticalBarChart ($ width, $ height );
} Else if ($ style = 2 ){
// Line
$ This-> chart = new LineChart ($ width, $ height );
} Else if ($ style = 3 ){
// Lump
$ This-> chart = new PieChart ($ width, $ height );
} Else {
// Cross
$ This-> chart = new HorizontalBarChart ($ width, $ height );
}
}

Function draw ($ obj ){

If ($ this-> style = 1 | $ this-> style = "1 "){
// Cylinder
$ DataSet = new XYDataSet ();
$ This-> chart-> setTitle ($ obj-> title); // title
$ Arr = array ();
$ Arr = $ obj-> dataArray;
Foreach ($ arr as $ key => $ val ){
$ DataSet-> addPoint (new Point ($ key, $ val ));
}
$ This-> chart-> setDataSet ($ dataSet );
$ This-> chart-> render ();
} Else if ($ this-> style = 2 | $ this-> style = "2 "){
// Line
$ This-> chart-> setTitle ($ obj-> title); // title
$ Arr = array ();
$ Arr = $ obj-> dataArray;
$ I = 0;
$ DataSet = new XYSeriesDataSet ();
Foreach ($ arr as $ key => $ val ){
$ Serie {$ I} = new XYDataSet ();
Foreach ($ val as $ k =>$ v ){
$ Serie {$ I}-> addPoint (new Point ($ k, $ v ));
}
$ DataSet-> addSerie ($ key, $ serie {$ I });
$ I = $ I + 1;
}
$ This-> chart-> setDataSet ($ dataSet );
$ This-> chart-> render ();
} Else if ($ style = 3 ){
// Lump
$ DataSet = new XYDataSet ();
$ This-> chart-> setTitle ($ obj-> title); // title
$ Arr = array ();
$ Arr = $ obj-> dataArray;
Foreach ($ arr as $ key => $ val ){
$ DataSet-> addPoint (new Point ($ key. "($ val)", $ val ));
}
$ This-> chart-> setDataSet ($ dataSet );
$ This-> chart-> render ();
} Else {
// Cross
$ DataSet = new XYDataSet ();
$ This-> chart-> setTitle ($ obj-> title); // title
$ Arr = array ();
$ Arr = $ obj-> dataArray;
Foreach ($ arr as $ key => $ val ){
$ DataSet-> addPoint (new Point ($ key, $ val ));
}
$ This-> chart-> setDataSet ($ dataSet );
$ This-> chart-> render ();
}
}

}
Class kkk {};
$ N = new drawPic ("4"); // it will set 1 or 2 or 3 or 4
$ K = new kkk ();
$ K-> dataArray = array ("2000" => "30", "2001" => "40", "2002" => "50 ", "2003" => "60", "2004" => "70", "2005" => "80", "20020" => "90 "); // style = 1 or style = 2 or style = 4
// $ K-> dataArray = array ("2000" => "30", "2001" => "40", "2002" => "50 ", "2003" => "60", "2004" => "70", "2005" => "80", "20020" => "90 "); // style = 3
// $ K-> dataArray = array ("yi" => array ("2000" => "30", "2001" => "40 ", "2002" => "50", "2004" => "60"), "er" => array ("2003" => "60 ", "2004" => "70", "2005" => "80", "20020" => "90 "), "san" => array ("33" => "12", "45" => "56", "89" => "45 ", "86" => "49"); // style = 2 and the years will show first array to block. (it will be show 2000 2001 2002 2004)
$ K-> title = "The Sheet title ";
$ N-> draw ($ k );
?>

The red font is called. Methods 1, 2, and 4 are the same arrays. 3 is a linear graph, which may have two or more lines of comparison (or single line ).
If you want to use Chinese characters, you may find that libchart Chinese characters are garbled. here is a solution.

The main source code of our application is as follows:
The code is as follows:
Header ("content-type: image/png ");
Require_once ('libchart/classes/libchart. php ');

$ Chart = new VerticalBarChart (500,250); // The parameter indicates the width and height of the image to be created.
$ DataSet = new XYDataSet (); // instantiate an XY axis data object

// Add four sets of data for this object. The first parameter of the Point object indicates the X axis coordinate,
// The second represents the Y axis coordinate.
$ Str = 'August 11 ';

$ Str = mb_convert_encoding ($ str, "html-entities", "UTF-8 ");

$ DataSet-> addPoint (new Point ("Jan 2005", 273 ));
$ DataSet-> addPoint (new Point ("$ str", 120 ));
$ DataSet-> addPoint (new Point ("March 2005", 442 ));
$ DataSet-> addPoint (new Point ("Hangzhou L 2005", 600 ));
// Pass the data set to the graphic object
$ Chart-> setDataSet ($ dataSet );
// Set the title of the image and render it as a png file
$ Chart-> setTitle ("statistical chart ");

// $ Chart-> render ("demo/generated/demo1.png ");
// A path and file name are required.
// It's as simple as a beautiful bar chart.
$ Chart-> render ();
?>

The red text is used to solve Chinese garbled characters.
2. garbled Title:
By default, Chinese characters are garbled. this is due to encoding reasons. make the following changes:
First, change libchar/libchart/classes/view/chart/Chart. php and find the following content:
The code is as follows:
Public function setTitle ($ title ){
$ This-> plot-> setTitle ($ title );
}

Changed:
The code is as follows:
Public function setTitle ($ title ){
$ Title = mb_convert_encoding ($ title, "html-entities", "UTF-8 ");
$ This-> plot-> setTitle ($ title );
}

Step 3: As mentioned in the previous blog:
1. write your own php file that uses the Libchart library to generate charts and save them in UTF-8 encoding.
2. find several Chinese character libraries, such as 文 and, and copy them to the libchart fonts directory.
3. modify the text. php file in the libchart classes directory.
Rows 47th and 48

The code is as follows:
$ This-> fontCondensed = dirname (_ FILE _). "/../fonts/DejaVuSansCondensed. ttf ";
$ This-> fontCondensedBold = dirname (_ FILE _). "/../fonts/DejaVuSansCondensed-Bold.ttf ";

Change
The code is as follows:
$ This-> fontCondensed = dirname (_ FILE _). "/../fonts/Chinese font you found ";
$ This-> fontCondensedBold = dirname (_ FILE _). "/../fonts/Chinese font you have found ";

My modified:
The code is as follows:
Public function Text (){
$ BaseDir = dirname (_ FILE __)."/../../../";

// Free low-res fonts based on Bitstream Vera $ This-> fontCondensed = $ baseDir. "fonts/FANGZHENGFANGSONG. ttf ";
$ This-> fontCondensedBold = $ baseDir. "fonts/FANGZHENGFANGSONG. ttf ";
}

FANGZHENGFANGSONG. ttf is the simplified font of 正. I changed the Chinese name to that one. In fact, it is okay not to change it.

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.