libchart php图表组件的使用
在组件使用中我们发现这个默认颜色太单调了,例子怎么只有一种颜色,这里就来动手调整试试吧
这个使用的libchart版本为1.3,下载地址
https://naku.dohcrew.com/libchart/pages/download/
前端调整
查资料得知 $chart->getConfig()->setUseMultipleColor(true);即可使图表变得多姿多彩
<?php
include "../libchart/classes/libchart.php";
$chart = new VerticalBarChart();
$dataSet = new XYDataSet();
$dataSet->addPoint(new Point("就绪", 10));
$dataSet->addPoint(new Point("示忙", 2));
$dataSet->addPoint(new Point("排队", 5));
$dataSet->addPoint(new Point("振铃", 5));
$dataSet->addPoint(new Point("通话", 3));
$dataSet->addPoint(new Point("外呼", 3));
$chart->setDataSet($dataSet);
//使用多种颜色
$chart->getConfig()->setUseMultipleColor(true);
$chart->setTitle("示例图表");
$chart->render("generated/demo1.png");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Libchart vertical bars demonstration</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15" />
</head>
<body>
<img alt="Vertical bars chart" src="generated/demo1.png" style="border: 1px solid gray;"/>
</body>
</html>
libchart/libchart/classes/model/ChartConfig.php
public function ChartConfig() {
//$this->useMultipleColor = false;
$this->showPointCaption = true;
$this->sortDataPoint = true;
}
如果觉得组件自带的颜色配置不好看,也可以像下面配置
$chart = new VerticalBarChart();
$chart->getPlot()->getPalette()->setBarColor(array(
new Color(0, 128, 0),
new Color(255, 0, 0),
new Color(42, 71, 181),
new Color(247, 247, 0),
new Color(168, 82, 254),
new Color(224, 198, 165),
new Color(239, 238, 218),
new Color(40, 72, 59),
new Color(71, 112, 132),
new Color(167, 192, 199),
new Color(218, 233, 202)
));
一劳永逸
如果组件都只要这一个样式就可以了,不想每次都去设置,可以找到 libchart/libchart/classes/view/color/Palette.php 在里面自行进行调整下
在构造函数Palette()找到
$this->setBarColor(array(
new Color(42, 71, 181),
new Color(243, 198, 118),
new Color(128, 63, 35),
new Color(195, 45, 28),
new Color(224, 198, 165),
new Color(239, 238, 218),
new Color(40, 72, 59),
new Color(71, 112, 132),
new Color(167, 192, 199),
new Color(218, 233, 202)
));
改为
$this->barColorSet = new ColorSet(array(
new Color(0, 128, 0),
new Color(255, 0, 0),
new Color(42, 71, 181),
new Color(247, 247, 0),
new Color(168, 82, 254),
new Color(224, 198, 165),
new Color(239, 238, 218),
new Color(40, 72, 59),
new Color(71, 112, 132),
new Color(167, 192, 199),
new Color(218, 233, 202)
), 1);