Statistical graphics We will often go to the data graphics, if three arrays to graphic display or real estate to the graphics, we will use graphics, I would like to introduce a PHP Libchart graphics generation class bar, very useful for the need of friends can refer to.
Simple full number or English can directly use the following categories (Libchart class everyone can download their own Baidu)
Copy Code code as follows:
?
/*
Update by Leo
It ' s draw the pic of Sheet,and it'll take all the num on the pic.
*/
Require "./libchart/classes/libchart.php";
Class drawpic{
var $chart;
var $style;
function Drawpic ($style = "1", $width = "+", $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 ($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 would set 1 or 2 or 3 or 4
$k =new KKK ();
$k->dataarray=array ("=>"), "2001" => "," "2002" => "," "2003" => "," "=>" "70", "2005" = > "20020", "=>");//style==1 or style=2 or style=4
$k->dataarray=array ("=>"), "2001" => "," "2002" => "," "2003" => "," "=>" "70", "2005 "=>", "20020" => "the");//style==3
$k->dataarray=array ("Yi" =>array ("=>"), "2001" => "," "2002" => "," "=>" "," "ER" = >array ("2003" => "," "=>", "the" "=>" "," "20020" => "," "San" =>array ("" "=>" 12 "," 45 " => "," "=>" "," "=>" "" ")"//style==2 and the years would show the "the". (It'll be Show 2000 2001 2002 2004)
$k->title= "the Sheet title";
$n->draw ($k);
?>
The red font is called. Method 1,2,4 to the same array. 3 is a linear graph, there may be two lines or multiple lines of comparison (also can be single line).
If you want to use Chinese may find libchart Chinese garbled, the following found a way
Our application main source code is as follows:
Copy Code code as follows:
<?php
Header ("Content-type:image/png");
Require_once (' libchart/classes/libchart.php ');
$chart = new Verticalbarchart (500, 250); Parameter represents the width and height of the image that needs to be created
$dataSet = new Xydataset (); Instantiate an XY axis data object
Adds four sets of data for this object, the first parameter of the point object represents the X axis coordinates,
The second represents the Y-axis coordinates
$str = ' February ';
$str = mb_convert_encoding ($str, "html-entities", "utf-8");
$dataSet-> Addpoint (New Point ("2005", 273);
$dataSet-> Addpoint (New Point ("$str", 120);
$dataSet-> Addpoint (New Point ("March 2005", 442);
$dataSet-> Addpoint (New Point ("April 2005", 600);
Pass this data collection to the drawing object
$chart-> Setdataset ($dataSet);
Set the caption of the graphic and render it as a PNG file
$chart-> Settitle ("Statistical map");
$chart-> render ("Demo/generated/demo1.png");
Here you need a path and file name
It's just so simple. A histogram as beautiful as the one below is coming out.
$chart-> render ();
?>
The place marked the red Word is to solve the Chinese garbled.
2, the title garbled:
The default display of Chinese is garbled, this is because of the coding reasons, make the following modifications:
First, change the libchar/libchart/classes/view/chart/chart.php and find the following:
Copy Code code as follows:
Public Function Settitle ($title) {
$this->plot->settitle ($title);
}
Change to:
Copy Code code as follows:
Public Function Settitle ($title) {
$title = mb_convert_encoding ($title, "html-entities", "utf-8");
$this->plot->settitle ($title);
}
Step three: That's what the above blog says:
1. Write your own PHP file using the Libchart library to generate the chart and save it in Utf-8 encoding
2, find a few Chinese font library, such as Chinese Xingkai, XXFarEastFont-Arial, and so on, copied to the Libchart fonts directory
3, modify the Libchart classes directory text.php files
Line 47th and 48
Copy Code code as follows:
$this->fontcondensed = dirname (__file__). "/.. /fonts/dejavusanscondensed.ttf ";
$this->fontcondensedbold = dirname (__file__). "/.. /fonts/dejavusanscondensed-bold.ttf ";
To
Copy Code code as follows:
$this->fontcondensed = dirname (__file__). "/.. /fonts/you find the Chinese font ";
$this->fontcondensedbold = dirname (__file__). "/.. /fonts/you find the Chinese font ";
I modified the:
Copy Code code 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 This file is the founder of The Imitation I find the library, I changed the Chinese name to that look, in fact, it is also possible to change.