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 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: |
Copy code |
/* 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: |
Copy code |
<? Php 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: |
Copy code |
Public function setTitle ($ title ){ $ This-> plot-> setTitle ($ title ); }
|
Changed:
The Code is as follows: |
Copy code |
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: |
Copy code |
$ This-> fontCondensed = dirname (_ FILE _). "/../fonts/DejaVuSansCondensed. ttf "; $ This-> fontCondensedBold = dirname (_ FILE _). "/../fonts/DejaVuSansCondensed-Bold.ttf "; Change $ 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: |
Copy code |
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.
The main thing is the basic operation, and then there are some questions. According to the above, it should be enough. Thanks again for the enthusiastic netizens who have provided these methods. Let's talk a lot!