echarts php异步加载数据
echarts官网:http://echarts.baidu.com/
工作中需要用到图表直观展示数据信息,百度的echarts工具的地图等图表展示功能更符合业务需求,这里展示下柱状图的基本展示效果:
html
<div class="main-chart" id="line-chart" style="width: 600px;height:400px;"></div>
js部分
var myChart = echarts.init(document.getElementById('line-chart'));
// 显示标题,图例和空的坐标轴
var options = {
tooltip: {
trigger: 'axis'
},
toolbox: {
feature: {
// dataView: {show: true, readOnly: false},
magicType: {show: true, type: ['line', 'bar']},
restore: {show: true},
saveAsImage: {show: true}
}
},
legend: {
data:['成交客户','名单数','回款金额']
},
xAxis: [
{
type: 'category',
data: []
}
],
yAxis: [
{
type: 'value',
name: '客户数',
min: 0,
max: 80,
interval: 4,
axisLabel: {
formatter: '{value}'
}
},
{
type: 'value',
name: '回款金额',
min: 0,
max: 300000,
interval: 30000,
axisLabel: {
formatter: '{value}'
}
}
],
series: [
{
name: '成交客户',
type: 'bar',
data: []
},
{
name: '名单数',
type: 'bar',
data: []
},
{
name: '回款金额',
type: 'line',
yAxisIndex: 1,
data: []
}
]
};
// 异步加载数据
$.ajax({
url: "{:U('Index/ajax_get_data')}",
dataType:'json',
success :function(res){
// 填入数据
options.xAxis[0].data = eval(res.category);
options.series[0].data = eval(res.active);
options.series[1].data = eval(res.counts);
options.series[2].data = eval(res.money);
myChart.setOption(options);
}
});
php部分
public function ajax_get_data(){
$this->ajaxReturn(array("category"=>"['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']","active"=>"[2, 4, 7, 5, 6, 7, 13, 2, 3, 2, 4, 3]",'counts'=>"[23, 45, 48, 52, 30, 41, 64, 16, 48, 46, 52, 13]",'money'=>"[200010, 65000, 320000, 32000, 18000, 230000, 21000, 18000, 52300, 45000, 120000, 170000]"));
}