search
HomeBackend DevelopmentPHP TutorialPHP generates graphics (Libchart) 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)

Copy the code 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 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 libchart Chinese characters are garbled. Here is a solution

The main source code of our application is as follows:

Copy the code The code is as follows:

header("content-type:image/png");
require_once('libchart/classes/libchart.php');

$chart = new VerticalBarChart(500, 250); // Parameter representation 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 parameter 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 set to the graph Object
$chart -> setDataSet ( $dataSet ) ;
// Set the title of the graph and render it as a png file
$chart -> setTitle ( "Statistical Chart" ) ;

//$chart -> render ( "demo/generated/demo1.png" ) ;
// You need a path and file name here
// It’s as simple as the picture below The beautiful histogram comes out
$chart -> render () ;
?>


The place marked with red letters is to solve the problem of Chinese garbled characters.
2. The title is garbled:
The default Chinese display 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:
Copy code The code is as follows:

public function setTitle($title) {                                                        plot->setTitle($title);
}

changed to:

Copy code The code is as follows:
public function setTitle($title) {
$title = mb_convert_encoding($title, "html-entities","utf-8" );
$this->plot- >setTitle($title);
}

The third step: It is mentioned in a blog above:
1. PHP that I wrote using the Libchart library to generate charts The file 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


Copy code The code is as follows:
$this->fontCondensed = dirname( __FILE__) . "/../fonts/DejaVuSansCondensed.ttf";
$this->fontCondensedBold = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed-Bold.ttf";

" /../fonts/The Chinese font you found";
$this->fontCondensedBold = dirname(__FILE__) . "/../fonts/The Chinese font you found";

What I modified:
Copy code

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825094.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
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in LaravelNotifications in LaravelMar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)