【发布时间】:2014-10-31 13:41:22
【问题描述】:
在谈到 javascript 时,我仍然很困惑。我需要一些帮助,向 highstock 图表添加类似于收入的辅助轴,该图表还使用 $.getJSON (JSONP) 来获取 json 文件以填充数据。
查看 JSFiddle 示例 here。这是播放with的样本数据集。最后是 Highcharts 中secondary axis 的示例。
$(function () {
var seriesOptions = [],
seriesCounter = 0,
names = ['MSFT', 'AAPL', 'GOOG'],
// create the chart when all data is loaded
createChart = function () {
$('#container').highcharts('StockChart', {
rangeSelector: {
selected: 4
},
yAxis: {
labels: {
formatter: function () {
return (this.value > 0 ? ' + ' : '') + this.value + '%';
}
},
plotLines: [{
value: 0,
width: 2,
color: 'silver'
}]
},
yAxis: {
floor: 0
},
plotOptions: {
series: {
compare: 'value'
}
},
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
valueDecimals: 2
},
series: seriesOptions
});
};
$.each(names, function (i, name) {
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=' + name.toLowerCase() + '-c.json&callback=?', 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 += 1;
if (seriesCounter === names.length) {
createChart();
}
});
});
});
非常感谢任何帮助和解释(以便我可以学习)。我还在尝试学习如何将 jsonp 和图表结合在一起,尤其是多个系列的数据和 json 文件。
【问题讨论】:
-
辅助 Y 轴?如果是这样,您只需要使用一个数组。例如:
yAxis:[{..axis1 properties..},{..axis 2 properties..}]。请注意,只要没有序列附加到轴上,它就不会显示标签。只需将属性“yAxis:n”添加到您的系列中,其中n是轴键(第一个是 0,第二个是 1...) -
因此,理想情况下,我需要添加一个新的轴标签和附加到该标签的系列。我之前确实尝试过添加一个新标签,但没有附加任何系列,所以什么也没显示。我想这就是原因。
-
正如@MorKadosh 所说,您需要有一个轴对象数组,然后在系列对象中,请参阅particualr yAxis。
标签: javascript jquery json highcharts highstock