highchart接收后台数据用法
最近做数据分析的时候使用了highchart这个插件,从后台中接收数据的时候出了一些问题,记录下来免得以后忘了。
$(function () { var list = {$weeklist}; var weekobj1 = eval(list); var week=""; //alert(obj1.length); for(var i=0;i<weekobj1.length;i++) { week = week+"\'"+weekobj1[i].weekname+"\',"; } //console.log(week); week = "["+week+"]"; var weekobj = eval(week); //缺勤次数 var count1 = {$weekcount}; var weekcountobj1 = eval(count1); var weekcount=""; for(var i=0;i<weekcountobj1.length;i++) { weekcount = weekcount+weekcountobj1[i].count+","; } console.log(weekcount); weekcount = "["+weekcount+"]"; var weekcountobj = eval(weekcount); $('#container').highcharts({ chart: { type: 'column' }, title: { text: '{$name}各周缺勤次数' }, xAxis: { categories: weekobj }, yAxis: { min: 0, title: { text: '次数' } }, tooltip: { headerFormat: '<span style="font-size:10px">{point.x}:</span><table>', pointFormat: '<tr><td style="padding:0"><b>{point.y:.if}</b></td></tr>', footerFormat: '</table>', shared: true, useHTML: true }, plotOptions: { column: { pointPadding: 0.2, borderWidth: 0 } }, credits: { enabled: false }, series: [{ name: '123', id:'test', data: weekcountobj }] }); var chart = $('#container').highcharts(); $(".highcharts-legend-item").hide();
问题其实很简单,highchart接收数据时候只能是javascript对象不是javascript字符串,所以要使用eval进行转换。标红部分