请帮我解决多个系列的问题。
我只用了一个系列就做到了,结果很好。但我需要另一个系列来比较。
我为这项工作使用了 Highstock 基本编码,我从互联网上找到了这些。
$(function() {
var seriesOptions = [],
yAxisOptions = [],
seriesCounter = 0,
names = ['Freight', 'DBNchaus'],
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() {
$('#container').highcharts('StockChart', {
chart: {
},
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
制作 json 数据的方法。
如果我删除第二个查询,我只能看到一个系列。
$host = "localhost";
$username = "root";
$password = "";
$db_name = "northwind";
$con = mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name") or die("cannot select DB");
$sql = "select unix_timestamp(OrderDate) as datetime, Freight from orders";
$result = mysql_query($sql);
$data = array();
while ($row = mysql_fetch_array($result)) {
extract($row);
$datetime *= 1000; // convert from Unix timestamp to JavaScript time
$datetime += 19800000;
$data[] = array(
$datetime,
floatval($Freight)
);
}
$sql = "select unix_timestamp(OrderDate) as datetime, DBNchaus from orders";
$result = mysql_query($sql);
$data1 = array();
while ($row1 = mysql_fetch_assoc($result)) {
extract($row1);
$datetime *= 1000; // convert from Unix timestamp to JavaScript time
$datetime += 19800000;
$data1[] = array(
$datetime,
floatval($DBNchaus)
);
}
echo json_encode($data), "\n";
echo json_encode($data1);
非常感谢您的帮助。我被困住了。:(