我想将表中的数据放入highcharts饼图中。
首先加载表格,然后在关闭之前在底部加载,我会将数据导入高级图表进行显示。
我试过了,但只是没有工作,我认为语法错误...
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="../../includes/js/highcharts/highcharts.js"></script>
<script type="text/javascript" src="../../includes/js/highcharts/modules/exporting.js"></script>
</head>
<body>
bla bla bla
bla bla bla
bla bla bla
<table class="dataTbl">
<tbody>
<tr><td>fruit</td><td>qty</td></tr>
<tr><td>apple</td><td>1</td></tr>
<tr><td>banana</td><td>5</td></tr>
<tr><td>durian</td><td>3</td></tr>
</tbody>
</table>
//to load pie chart at the last, data obtain from table
<div id="container" style="width: 400px; height: 300px; margin: 0 auto"></div>
<script type="text/javascript">
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: 'Top fruits'
},
tooltip: {
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
}
}
}
},
series: [{
type: 'pie',
name: 'Browser share',
data: [
['Firefox', 45.0],
['IE', 26.8],
['Chrome', 12.8],
['Safari', 8.5],
['Opera', 6.2],
['Others', 0.7]
]
}]
//default series data sample given by highchart, how to override this?
});
$(".dataTbl > tbody > tr").each(function (index) {
if (index != 0) {
var chartnumbervalue = parseInt($(this).find("td:first").next('td').text());
var charttextvalue = jQuery.trim($(this).find("td:first").text());
//chart.series[0].data.push([charttextvalue, chartnumbervalue]);
//i want to push the data into series: data: but fail...
}
});
});
</script>
</body>
答案 0 :(得分:1)
我提出了一个解决方案并将其放在jsfiddle:http://jsfiddle.net/CAKQH/21189/
因此,您必须要了解的主要事实是,实际图表的系列“部分”是一个可以采用几种不同形式的对象数组。您可以在此处详细了解:http://www.highcharts.com/ref/#series
因此,我的方法和您在代码中描述的方法的最大区别在于,我在实际创建图表之前继续为高级图表准备数据。
var piechartinfo = {
type: 'pie',
name: 'Custom Info',
data: []
}
我将数据数组留空,因为你的jquery循环会在之后填充它。
//chart.series[0].data.push([charttextvalue, chartnumbervalue]);
piechartinfo.data.push([charttextvalue, chartnumbervalue]);
然后,当实例化实际图表时,我添加了piechartinfo对象
series: [piechartinfo]