PHP generated Graphics (Libchart) instance _php instance

Source: Internet
Author: User
Statistical graphs we will often to the data graph, if three arrays to graphic display or real estate to graph to us will need to use graphics, below I introduce a PHP libchart graph generation Class bar, very useful for the needs of friends to reference.
Simple all-digital or English can directly use the following class (Libchart class can be self-Baidu download)
Copy CodeThe code is 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, 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 would set 1 or 2 or 3 or 4
$k =new KKK ();
$k->dataarray=array ("+" and "+", "2001" and "+", "2002" and "+", "2003" and "A", "2004" and "70", "2005" = > "20020", "//style==1" and "all"); or style=2 or style=4
$k->dataarray=array ("+" and "+", "2001" and "+", "2002" and "+", "2003" and "A", "2004" and "70", "2005 "=", "20020" and "//style==3";
$k->dataarray=array ("Yi" =>array ("the" "" "and" "", "2001" and "+", "2002" and "+", "2004" and "" ")," er "= >array ("2003" and "12", "2004" and "he", "2005" and "he", "20020" and "All"), "San" =>array ("45" = "", "" "," "" and "" "," "" "(") "," (")"),//style==2 and the years would show first array to block. (It'll be Show 2000 2001 2002 2004)
$k->title= "the Sheet title";
$n->draw ($k);
?>

The red font is called. The 1,2,4 method is the same array. 3 is a linear graph, there may be two lines or multiple lines of comparison (can also be single-line).
If you want to use Chinese may find libchart Chinese garbled, the following find a way

Our application main source code is as follows:
Copy the Code code as follows:
Header ("Content-type:image/png");
Require_once (' libchart/classes/libchart.php ');

$chart = new Verticalbarchart (500, 250); Parameters represent 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 to this object, and the first parameter of the point object represents the X-axis coordinate,
The second represents the Y-axis coordinate
$str = ' February ';

$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));
Addpoint (New Point ("April 2005", 600), $dataSet);
Pass this data collection to the drawing object
$chart-Setdataset ($dataSet);
Set the title of the graphic and render it as a PNG file
$chart-Settitle ("Statistical chart");

$chart, Render ("Demo/generated/demo1.png");
A path and file name is required here
It's just that simple, like, a pretty bar chart.
$chart, Render ();
?>

The place to mark the Scarlet Letter is to solve the Chinese garbled.
2, the title garbled:
The default display of Chinese is garbled, this is due to the reason for encoding, make the following changes:
First, change the libchar/libchart/classes/view/chart/chart.php to find the following:
Copy CodeThe code is as follows:
Public Function Settitle ($title) {
$this->plot->settitle ($title);
}

Change to:
Copy CodeThe code is as follows:
Public Function Settitle ($title) {
$title = mb_convert_encoding ($title, "html-entities", "utf-8");
$this->plot->settitle ($title);
}

Step three: That's what one of the above blogs mentions:
1. Write your own PHP file using the Libchart library to generate the chart, save it in Utf-8 encoding
2, find a few Chinese font library, such as Chinese Xingkai, song body and so on, copied to the Libchart fonts directory
3. Modify the text.php file under the Libchart classes directory
47th, 48 lines

Copy CodeThe code is as follows:
$this->fontcondensed = dirname (__file__). "/.. /fonts/dejavusanscondensed.ttf ";
$this->fontcondensedbold = dirname (__file__). "/.. /fonts/dejavusanscondensed-bold.ttf ";

Switch
Copy CodeThe code is 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 the 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 I find the founder imitation Simplified Chinese Library, I changed the name to that look, in fact, not change is also possible.

  • 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.