如何通过DOM容器访问Highcharts图表? [英] How can I get access to a Highcharts chart through a DOM-Container?
问题描述
当我向div容器渲染高图表时,如何通过div-container访问图表对象?
When I render a highcharts-chart to a div container, how can I get access to the chart object through the div-Container?
我不想使图表变量成为全局变量.
I don't want to make the chart variable global.
var chart = new Highcharts.Chart({
chart: {
renderTo: "testDivId",
...
我想像这样(伪代码)在上面的上下文之外访问图表,以调用函数:
I want to access the chart outside of the context above like this (pseudocode), to call functions:
var chart = Highcharts.Chart("testDivId"); //access from id
chart.redraw();
推荐答案
Highcharts 3.0.1
用户可以使用highcharts插件
Highcharts 3.0.1
Users can use the highcharts plugin
var chart=$("#container").highcharts();
Highcharts 2.3.4
对于版本2.3.4和更高版本,请从 Highcharts.charts 数组中读取,图表的索引可以从<div>
Highcharts 2.3.4
Read from the Highcharts.charts array, for version 2.3.4 and later, the index of the chart can be found from the data on the <div>
var index=$("#container").data('highchartsChart');
var chart=Highcharts.charts[index];
所有版本
通过容器ID在全局对象/地图中跟踪图表
All versions
Track charts in a global object/map by container id
var window.charts={};
function foo(){
new Highcharts.Chart({...},function(chart){
window.charts[chart.options.chart.renderTo] = chart;
});
}
function bar(){
var chart=window.charts["containerId"];
}
读取模式@ Highcharts技巧-从图表中访问图表对象容器ID
自撰写此答案以来,在较新版本的Highcharts中进行了一些补充,并摘自@ davertron,@ Moes和@Przy的答案,请对他们的评论/答案给予应有的评价 >对于这些.没有这些,将它们添加到此处将是不完整的
Some additions were made in the newer versions of Highcharts since writing this answer and have been taken from answers from @davertron, @Moes and @Przy, please upvote their comments/answers as they deserve the credit for these. Adding them here as this accepted answer would be incomplete without these
这篇关于如何通过DOM容器访问Highcharts图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!