search
HomeBackend DevelopmentPHP TutorialPHP statistical graphics LIbchart class usage example_PHP tutorial

Statistical graphs are data graphs that we often see. If three arrays are displayed graphically or real estate is displayed graphically, we will use graphs. Let me introduce a php LIbchart graph generation class, which is very useful. Friends in need can refer to it.

For simple full numbers or English, you can directly use the following class (you can download the libchart class from Baidu)

 代码如下 复制代码

 /*
  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 fonts are calls. Methods 1, 2, and 4 are the same array. 3 is a linear graph, there may be two lines or multiple lines for comparison (it can also be a single line).

If you want to use Chinese, you may find that the Chinese characters in libchart are garbled. Here is a solution


Our application main source code is as follows:

The code is as follows
 代码如下 复制代码

   header("content-type:image/png");  

   require_once('libchart/classes/libchart.php');
  
   $chart = new VerticalBarChart( 500 , 250 ) ; // 参数表示需要创建的图像的宽和高

   $dataSet = new XYDataSet() ; // 实例化一个 XY 轴数据对象


     // 为这个对象增加四组数据集合, Point 对象的第一个参数表示 X 轴坐标,
// 第二个表示 Y 轴坐标

   $str = '二月';
   
$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( "April 2005" , 600 )) ;

     // 把这个数据集合传递给图形对象

    $chart -> setDataSet ( $dataSet ) ;

    // 设置图形的标题,并把它作为一个 png 文件渲染

    $chart -> setTitle ( "统计图" ) ;
   
    //$chart -> render ( "demo/generated/demo1.png" ) ;
    // 这里需要一个路径和文件名称
    //就这么简单一个像下图一样美丽的柱状图就出来了

    $chart -> render () ; 

?>

Copy code
header("content-type:image/png"); require_once('libchart/classes/libchart.php'); $chart = new VerticalBarChart(500, 250); //The parameters represent the width and height of the image to be created $dataSet = new XYDataSet(); // Instantiate an XY axis data object //Add four sets of data sets to this object. The first parameter of the Point object represents the X-axis coordinate, // The second one 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 )) ; $dataSet -> addPoint ( new Point( "April 2005" , 600 )) ; // Pass this data collection to the graphics 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" ) ; //This requires a path and file name //It’s that simple and a beautiful histogram like the picture below comes out $chart -> render () ; ?>

The places marked in red are to solve the problem of Chinese garbled characters.

2. Garbled title:

The default display of Chinese characters is garbled. This is due to encoding. Make the following modifications:
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);
        }
Copy code

public function setTitle($title) {      
 代码如下 复制代码

public function setTitle($title) {
        $title = mb_convert_encoding($title, "html-entities","utf-8" );

           $this->plot->setTitle($title);

}

                 $this->plot->setTitle($title);

}




changed to:

The code is as follows

Copy code
 代码如下 复制代码

$this->fontCondensed = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed.ttf";
$this->fontCondensedBold = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed-Bold.ttf";

改为


$this->fontCondensed = dirname(__FILE__) . "/../fonts/你找来的中文字体";
$this->fontCondensedBold = dirname(__FILE__) . "/../fonts/你找来的中文字体";

public function setTitle($title) {
​​​​ $title = mb_convert_encoding($title, "html-entities","utf-8" );

 代码如下 复制代码

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";
   }

$this->plot->setTitle($title);

}

The third step: It is what is mentioned in a blog above: 1. The php file that I wrote using the Libchart library to generate the chart is saved in utf-8 encoding ​ ​ 2. Find several Chinese font libraries, such as Chinese Xingkai, Songti, etc., and copy them to the libchart fonts directory ​ ​ 3. Modify the text.php file in the libchart classes directory Lines 47 and 48
The code is as follows Copy code
$this->fontCondensed = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed.ttf"; $this->fontCondensedBold = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed-Bold.ttf"; changed to $this->fontCondensed = dirname(__FILE__) . "/../fonts/The Chinese font you found"; $this->fontCondensedBold = dirname(__FILE__) . "/../fonts/The Chinese font you found"; What I 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 This file is the FANGZHENG FANGSONG simplified font I found. I changed the Chinese name to look like that. In fact, it’s okay not to change it. Mainly the basic operations, and then if you have any questions, follow the instructions above and it should be fine. Thank you again to the enthusiastic netizens who provided these methods. Please share more!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632896.htmlTechArticleStatistical graphics are the data graphics we often see. If three arrays are displayed graphically or real estate trends are displayed graphically, We all need to use graphics. Let me introduce a php LIbchart graphics student...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.