php利用Libchart库绘图

puddingpp 2019-06-25

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="生成图片的路径" />

相关推荐

lzqlawrence / 0评论 2011-03-30