php 生成饼状图,折线图,条形图 通用类 2

生成饼状图,折线图,条形图通用的php类,这里使用的是国外的 HighCharts,前台页面别忘了调用HighCahrt  js

HighCharts中文网站  http://www.hcharts.cn/index.php

<?php
/**
* Created by PhpStorm.
* User: WANG
* Date: 2015/7/26
* Time: 11:19
*/
namespace Org\Wechat;
class HighCharts
{
/**
* 获得js生成图表的报表
* @param $id dom元素id
* @param array $data 图表数据
* @param $type 图表类型 线形图:line 饼图:pie 条形图:column
* @param string $title 图表主标题
* @param string $subtitle 副标题
* @return string
*/
public function getHighCharts($id, array $data, $type, $title = '', $subtitle = '', $yTitle = '')
{
$series = '';
if (empty($data)) {
$data = array(
'categories' => array(
'data' => array('-')
),
'xaxis' => array(
'type' => 'category',
'boundaryGap' => 'false',
'data' => array('')
),
'series' => array(
array(
'name' => '-',
'type' => 'line',
'itemStyle' => "{normal: {areaStyle: {type: 'default'}}}",
'data' => array()
),
)
);
}
foreach ($data as $key => $value) {
switch ($key) {
case 'categories':
$categories = '[';
foreach ($value as $k => $v) {
// categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun','Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
$categories = $categories . json_encode($v, JSON_UNESCAPED_UNICODE) . ',';
}
$categories = $categories . ']';
break;
case 'xaxis':
foreach ($value as $k => $v) {
switch ($k) {
case 'type':
$xaxis[] = $k . ":'" . $v . "'";
break;
case 'data':
$xaxis[] = $k . ':' . json_encode($v, JSON_UNESCAPED_UNICODE);
break;
}
}
$xaxis = '{' . implode(', ', $xaxis) . '}';
break;
//{ name: 'Tokyo', data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6 ]}
case 'series':
foreach ($value as $list) {
$tmp = array();
foreach ($list as $k => $v) {
switch ($k) {
case 'name':
case 'type':
$tmp[] = $k . ":'" . $v . "'";
break;
case 'itemStyle':
$tmp[] = $k . ':' . $v;
break;
case 'data':
$tmp[] = $k . ':' . json_encode($v, JSON_UNESCAPED_UNICODE);
}
}
$series[] = '{' . implode(', ', $tmp) . '}';
}
$series = implode(', ', $series);
break;
}
}
if ($type !== 'pie') {
$str = "xAxis:{categories:$categories},";
} else {
$str = '';
} $script = <<<EOT
<script type="text/javascript">
$(function () {
$('#$id').highcharts({
title: {
text: '$title',
x: -20 //center
},
subtitle: {
text: '$subtitle',
x: -20
},
credits: {
enabled: false
},
$str
yAxis: {
title: {
text: '$yTitle'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: '%'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: [$series]
});
});
</script>
EOT;
return $script;
}
}
上一篇:关闭SSL证书验证


下一篇:[转] - C++程序启动过程