如何通过 DOM-Container 访问 Highcharts 图表? [英] How can I get access to a Highcharts chart through a DOM-Container?

查看:13
本文介绍了如何通过 DOM-Container 访问 Highcharts 图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将 highcharts-chart 渲染到 div 容器时,如何通过 div-Container 访问图表对象?

我不想将图表变量设为全局.

 var chart = new Highcharts.Chart({图表: {renderTo: "testDivId",...

我想像这样(伪代码)访问上面上下文之外的图表,以调用函数:

var chart = Highcharts.Chart(testDivId");//从id访问图表重绘();

解决方案

Highcharts 3.0.1

用户可以使用 highcharts 插件

var chart=$("#container").highcharts();

Highcharts 2.3.4

Highcharts.charts 数组中读取,适用于 2.3.4 及更高版本,图表的索引可以从

上的数据中找到

 var index=$("#container").data('highchartsChart');var chart=Highcharts.charts[index];

所有版本

通过容器 id 跟踪全局对象/地图中的图表

var window.charts={};函数 foo(){new Highcharts.Chart({...},function(chart){window.charts[chart.options.chart.renderTo] = 图表;});}功能栏(){var chart=window.charts["containerId"];}

阅读模式@Highcharts Tips - 从 a 访问图表对象容器 ID

附注

自撰写此答案以来,在较新版本的 Highcharts 中添加了一些内容,并且摘自 @davertron、@Moes 和 @Przy 的答案,请为他们的评论/答案投票,因为他们值得称赞 对于这些.在这里添加它们,因为如果没有这些,这个接受的答案将是不完整的

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

Users can use the highcharts plugin

var chart=$("#container").highcharts();

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];

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"];
}

Read Mode @ Highcharts Tips - Accessing Chart Object From a Container ID

P.S.

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-Container 访问 Highcharts 图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆