我如何通过DOM容器访问Highcharts图表 [英] How can i get access to a Highcharts chart through a DOM-Container

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

问题描述

当我将一个高图表呈现给一个div容器时,我如何通过div-Container访问图表对象?
i不想让图表变量全局。

When i render a highcharts-chart to a div container, how can i get access to the chart object through the div-Container? i dont 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



Highcharts.charts 数组,对于版本2.3.4及更高版本,图表的索引可以从< 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屋!

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