我在尝试通过 Json 将多个数据传递到 Highstock 图表时遇到问题,这是我目前的代码:
$(function() {
var seriesOptions = [],
yAxisOptions = [],
seriesCounter = 0,
names = ['MSFT', 'AAPL'],
colors = Highcharts.getOptions().colors;
$.each(names, function(i, name) {
$.getJSON('data.php', function(data) {
seriesOptions[i] = {
name: name,
data: data
};
// As we're loading the data asynchronously, we don't know what order it will arrive. So
// we keep a counter and create the chart when all the data is loaded.
seriesCounter++;
if (seriesCounter == names.length) {
createChart();
}
});
});
// create the chart when all data is loaded
function createChart() {
chart = new Highcharts.StockChart({
chart: {
renderTo: 'container'
},
rangeSelector: {
selected: 4
},
yAxis: {
labels: {
formatter: function() {
return (this.value > 0 ? '+' : '') + this.value + '%';
}
},
plotLines: [{
value: 0,
width: 2,
color: 'silver'
}]
},
plotOptions: {
series: {
compare: 'percent'
}
},
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
valueDecimals: 2
},
series: seriesOptions
});
}
});
这是我的 data.php 文件:
$query = "SELECT tiempo,Fp1Ref,F7Ref
FROM GraficaEEG limit 10";
$data = mysqli_query($dbc, $query);
$i=0;
while($row = mysqli_fetch_array($data)) {
$rows[$i]=array((float)$row['tiempo'],(float)$row['Fp1Ref']);
$rows1[$i]=array((float)$row['tiempo'],(float)$row['F7Ref']);
$i++;
}
echo json_encode($rows), "\n";
echo json_encode($rows1);
我以这种形式接收数据:
[[0,3001],[0.005,4937.22],[0.01,4130.55],[0.015,4213.15],[0.02,4010.61],[0.025,3914.34],[0.03,3785.33],[0.035,3666.13],[0.04,3555.25],[0.045,3447.77]]
[[0,2995.12],[0.005,4931.59],[0.01,4125.99],[0.015,4203.38],[0.02,4005.27],[0.025,3911.41],[0.03,3777.35],[0.035,3659.15],[0.04,3552.75],[0.045,3442.12]]
我不知道我做错了什么以及如何将两组数据传递给 Highstock 图表,如果我只传递一个 $rows 它可以工作,但它在三行中给了我相同的数据,我会将非常感谢任何帮助。