php利用Libchart庫繪圖

7shad0w發表於2019-02-16

Libchart是一個免費繪製圖表的PHP庫,很好用,由於是老外寫的,所以對中文支援不好,在圖表裡顯示為亂碼。
官網地址:https://naku.dohcrew.com

亂碼解決辦法:
1、自己寫的使用Libchart庫生成圖表的php檔案以utf-8編碼儲存;
2、找幾個中文字型庫,比如華文行楷、宋體等等,複製到libchartfonts目錄下;
3、修改libchartclasses目錄下的text.php檔案
第47、48行

$this->fontCondensed = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed.ttf";
$this->fontCondensedBold = dirname(__FILE__) . "/../fonts/DejaVuSansCondensed-Bold.ttf";
改為
$this->fontCondensed = dirname(__FILE__) . "/../fonts/msyh.ttf";  #去自己的fonts目錄把微軟雅黑拖到程式目錄下就好了
$this->fontCondensedBold = dirname(__FILE__) . "/../fonts/msyhbd.ttf";

去掉版權資訊:
將libchartclassesviewchartChart.php 第61行

$this->plot->setLogoFileName(dirname(__FILE__) . “/../../../images/PoweredBy.png”);
遮蔽掉或者替換成相應的Logo圖片。

簡單的使用示例:
1.餅狀圖

<?php
//Pie chart demonstration (image not saved to disk)
include "../libchart/classes/libchart.php";
header("Content-type: image/png");
$chart = new PieChart(500, 260);
$dataSet = new XYDataSet();
$dataSet->addPoint(new Point("新浪微博", 80));
$dataSet->addPoint(new Point("騰訊微博", 50));
$dataSet->addPoint(new Point("網易微博", 10));
$chart->setDataSet($dataSet);
$chart->setTitle("本資料僅用於內部測試使用,不表示實際情況");
$chart->render();
?>

2.橫向的柱狀圖

<?php
//Horizontal bar chart demonstration(image not saved to disk)
include "../libchart/classes/libchart.php";
header("Content-type: image/png");
$chart = new HorizontalBarChart(600, 170);
$dataSet = new XYDataSet();
$dataSet->addPoint(new Point("新浪微博", 80));
$dataSet->addPoint(new Point("騰訊微博", 50));
$dataSet->addPoint(new Point("網易微博", 10));
$chart->setDataSet($dataSet);
$chart->getPlot()->setGraphPadding(new Padding(5, 30, 20, 140));
$chart->setTitle("本資料僅用於內部測試使用,不表示實際情況");
$chart->render();
?>

3.縱向的柱狀圖

<?php
//Vertical bar chart demonstration
include "../libchart/classes/libchart.php";
header("Content-type: image/png");    
$chart = new VerticalBarChart();
$dataSet = new XYDataSet();
$dataSet->addPoint(new Point("Jan 2013", 273));
$dataSet->addPoint(new Point("Feb 2013", 421));
$dataSet->addPoint(new Point("March 2013", 642));
$dataSet->addPoint(new Point("April 2013", 800));
$dataSet->addPoint(new Point("May 2013", 1200));
$dataSet->addPoint(new Point("June 2013", 1500));
$dataSet->addPoint(new Point("July 2013", 2600));
$chart->setDataSet($dataSet);
$chart->setTitle("本資料僅用於內部測試使用,不表示實際情況");
$chart->render();
?>

4.線性圖

<?php    
// Line chart demonstration(image not saved to disk)
include "../libchart/classes/libchart.php";
header("Content-type: image/png");
$chart = new LineChart();
$dataSet = new XYDataSet();
$dataSet->addPoint(new Point("05-01", 273));
$dataSet->addPoint(new Point("05-02", 421));
$dataSet->addPoint(new Point("05-03", 642));
$dataSet->addPoint(new Point("05-04", 799));
$dataSet->addPoint(new Point("05-05", 1009));
$dataSet->addPoint(new Point("05-05", 1405));
$dataSet->addPoint(new Point("05-07", 1820));
$dataSet->addPoint(new Point("05-08", 2511));
$dataSet->addPoint(new Point("05-09", 2832));
$dataSet->addPoint(new Point("05-10", 3550));
$dataSet->addPoint(new Point("05-11", 4143));
$dataSet->addPoint(new Point("05-12", 4715));
$chart->setDataSet($dataSet);
$chart->setTitle("本資料僅用於內部測試使用,不表示實際情況");
$chart->render();
?>

以上示例是libchart最簡單的呼叫方式,如果需要在磁碟上生成圖片,供其他地方呼叫,方法很簡單:
只需要將

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

遮蔽掉,並且將

$chart->render();

修改成

$chart->render("生成圖片的路徑");

HTML中呼叫如下:

<img src="生成圖片的路徑" />

相關文章