[英]HighStock “xRange” chart is not working with older version library(HighStock)
[英]Highstock chart - JSON input not working
我正在尝试使用Highcharts的Highstock创建图表,但无法弄清楚如何从PHP文件中提供正确的JSON数据。
这是我的HTML文件。 用于获取getJSON
的数据的原始URL是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无效。 该标记适用于JSONP,因为highchart正在从其他域请求数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.