我正在制作体重图表。我通过 JSON 获取图表的数据,但问题是我没有 xAxis 的数据,只有日期。这是我得到的数据示例:
[
{"Date":"2011-03-02","y":"180"},
{"Date":"2011-03-03","y":"250"},
{"Date":"2011-03-09","y":"185"},
{"Date":"2011-03-03","y":"300"}
]
以下是我获取数据和呈现图表的方式:
<script type="text/javascript">
var chart;
function requestData() {
$.getJSON('<?php echo Dispatcher::baseUrl();?>/logs/feed_data',
function (data) {
var series = chart.series[0];
series.setData(data);
}
);
}
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'contain',
defaultSeriesType: 'line',
marginRight: 130,
marginBottom: 25,
events: { load: requestData }
},
title: {
text: 'Your Weight History',
x: -20 //center
},
subtitle: {
text: 'Source: Fitness',
x: -20
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: '-weight -Pounds '
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y +' -Pounds';
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: [{
name: 'Data1',
data: []
}]
});
});
</script>