我想从Highcharts的Highstock创建图表,但无法弄清楚如何从PHP文件提供正确的JSON数据。Highstock图表 - JSON输入不工作
这是我的HTML文件。用于获取getJSON
中的数据的原始网址是http://www.highcharts.com/samples/data/jsonp.php?filename=' + name.toLowerCase() + '-c.json&callback=?
正在进行三种不同的呼叫。例如,一个是: http://www.highcharts.com/samples/data/jsonp.php?filename=goog-c.json&callback=?
<html><head>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head><body>
<div id="container" style="height: 400px; min-width: 310px"></div>
<script>
$(function() {
var seriesOptions = [],
seriesCounter = 0,
names = ['MSFT', 'AAPL', 'GOOG'];
/**
* Create the chart when all data is loaded
* @returns {undefined}
*/
function createChart() {
Highcharts.stockChart('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',
showInNavigator: true
}
},
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
valueDecimals: 2,
split: true
},
series: seriesOptions
});
}
$.each(names, function (i, name) {
console.log('name: '+name);
$.getJSON('http://localhost/projects/AGF/testobject.php', function (data) {
console.log(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 += 1;
if (seriesCounter === names.length) {
createChart();
}
});
});
});
</script>
</body></html>
我只是从该PHP文件返回从我自己的PHP文件中复制的内容,并呼应它testobject.php
testobject.php:
<?php
echo "?([
[1258934400000,290.88],
[1259020800000,291.25])";
?>
我将JSON缩短为2个对象并删除了评论。 Highstock需要第一个?
,它在原始URL中作为回调参数添加。每个对象中的第一个数字是日期的整数值。
最终我会查询数据库中的数据并以此格式输出。
我的问题是为什么这不工作,如果答案基本相同?
感谢。
那个问号不是highstock所要求的既不是'()'所以你的json是无效的 – bassxzero
我的噢我的...修复了它。那么为什么当原始的PHP使用?和()?你可以张贴它作为答案,所以我可以接受和关闭。谢谢。 – fractal5
谷歌jsonp,你会发现为什么 – bassxzero