我已按照 previous answer 中的提示进行操作编写代码行以通过其 id 获取 HighCharts 系列。不幸的是,我无法做到。 考虑以下玩具示例 ( on Jfiddle ):
$(function () {
var cseries=[{
id: 'Tokyo',
name: 'Tokyo',
data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
}, {
id: 'New York',
name: 'New York',
data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
}];
$('#container').highcharts({
series: cseries
});
var chart=$('#container').highcharts();
alert(chart.get('Tokyo').id);
});
当我剪切最后一行时,脚本运行良好。当我不剪切最后一行时,警报返回 undefined
。为什么?
最佳答案
您没有在警报中关闭足够的括号:
alert(chart.get('Tokyo');
应该是:
alert(chart.get('Tokyo'));
而且我认为您最好执行 console.log(chart.get('Tokyo'));
并在浏览器控制台中查看输出,因为它输出的对象不能显示在警报中。
根据后期编辑更新:
chart.get('Tokyo')
没有 id
属性
要获取系列 ID,请尝试 console.log(chart.get('Tokyo').userOptions.id);
关于javascript - 无法在 HighCharts 图表中获取系列 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31642426/