admin管理员组文章数量:1394585
I render a Highcharts chart in a div with id container
like so:
new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'line',
zoomType: 'x'
},
xAxis: {
categories: ['Jan', 'Feb']
},
series: [{
data: [29.9, 71.5]
}]
});
<div id="container" class="chart"></div>
Notice that I haven't captured a reference to the chart object in a variable. At some point after the chart has been rendered is it possible to get a reference to the chart object from the ID of the element it has been rendered to (container
in this case)?
I render a Highcharts chart in a div with id container
like so:
new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'line',
zoomType: 'x'
},
xAxis: {
categories: ['Jan', 'Feb']
},
series: [{
data: [29.9, 71.5]
}]
});
<div id="container" class="chart"></div>
Notice that I haven't captured a reference to the chart object in a variable. At some point after the chart has been rendered is it possible to get a reference to the chart object from the ID of the element it has been rendered to (container
in this case)?
2 Answers
Reset to default 5With jQuery
var Mychart=$("#container").data('highchartsChart');
or
var Mychart=$("#container").highcharts();
jsfiddle
or via the Highcharts' charts array ([0] if your chart is the first)
var Mychart = Highcharts.charts[0];
You can access it through the Highcharts object. Every chart created with Highcharts will be pushed into Highcharts' charts
array. If this is the first chart you've created, you can get a reference to it like so:
var chart = Highcharts.charts[0];
Your element itself will contain a reference to the index of the above array within it's dataset
property. Again, if this is the first chart you've created, you'd see something like the following:
document.getElementById('container').dataset;
> DOMStringMap { highchartsChart: "0" }
This highchartsChart
property holds the relevant array index, which you can then use to ensure you have the correct chart selected:
var index = document.getElementById('container').dataset.highchartsChart,
chart = Highcharts.charts[index];
本文标签: javascriptGet a reference to a Highcharts chartStack Overflow
版权声明:本文标题:javascript - Get a reference to a Highcharts chart - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744094155a2589973.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论