mtatsky
Posts: 12
Joined: Mon May 30, 2016 6:29 pm

Get Chart by id

Is there way to get chart by id not like this

Code: Select all

var chart = $('#myChart').highcharts();
As far as I understand HighCharts is plugin free library.
But this sample show that the only way to get chart is using jquery.
I tried to find solution, but I did not find on stackoverflow and others resources.

I think it should be like this

Code: Select all

var chart = Highcharts.Chart("testDivId"); //access from id
pawel_d
Posts: 1910
Joined: Thu Jun 02, 2016 10:28 am

Re: Get Chart by id

Hi mtatsky,

You can always assign created chart to a variable or get it by Highcharts object. Please, look at the example posted below.

Example:
http://jsfiddle.net/d_paul/93L20xg4/

Regards.
Paweł Dalek
Highcharts Developer
mtatsky
Posts: 12
Joined: Mon May 30, 2016 6:29 pm

Re: Get Chart by id

Let me clearify.

I use HighCharts in not usual way.
I did integration of HighCharts with product of my company.
And in some cases I have troubles and bugs on clients.
Since there is not way to get chart without jQuery.
Reference also does not suit since my product read data from chart.
pawel_d
Posts: 1910
Joined: Thu Jun 02, 2016 10:28 am

Re: Get Chart by id

Hi mtatsky,

What do you mean by 'in not usual way'? In previous example I showed you how you can get chart without using jQuery at all (by Highcharts object). Highcharts.charts is an array of charts, so if you need to get a specific chart, you can simply get it by appropriate index. In the example below I also get rid of jQuery $( document ).ready() function, for demo to be fully jQuery free.

API Reference:
https://learn.jquery.com/using-jquery-c ... ent-ready/

Example:
http://jsfiddle.net/d_paul/93L20xg4/1/

Regards.
Paweł Dalek
Highcharts Developer
mtatsky
Posts: 12
Joined: Mon May 30, 2016 6:29 pm

Re: Get Chart by id

Product of company is FancyGrid
http://fancygrid.com

I need internally read data from any chart.
There is no way to store reference on chart.
I have only id inside.

If on page it is included jQuery than all is partly ok.
If jQuery is not included than I have problem and bug.
pawel_d
Posts: 1910
Joined: Thu Jun 02, 2016 10:28 am

Re: Get Chart by id

Hi mtatsky,

You can cycle through all charts and check if id is the same as the name of rendering div. This way you can simply get chart which you want having only an id.

Code: Select all

charts = Highcharts.charts;
charts.forEach(function(chart, index) {
    if (chart.renderTo.id === chartsId) {
        chosenChart = chart;
    }
});
Example:
http://jsfiddle.net/d_paul/93L20xg4/2/

Regards.
Paweł Dalek
Highcharts Developer
lowcharts
Posts: 2
Joined: Thu Apr 08, 2021 12:52 pm

Re: Get Chart by id

Thank you!
Four years later this is just what I was looking for.

With ES6 you can do it in one line:

Code: Select all

chosenChart = Highcharts.charts.find(c => c.renderTo.id == chartsId);
Although I should point out for anyone else that in the version I'm using (8.2.2 and probably anything higher) I had to use subscript notation as that property wasn't found by my IDE. (But this worked instead.)

Code: Select all

c['renderTo'].id
User avatar
sebastian.h
Posts: 1734
Joined: Fri Aug 07, 2020 7:08 am

Re: Get Chart by id

Hi lowcharts!

Thanks for sharing your solution, I appreciate this.

You can reach us any time if need help.
Best regards.
Sebastian Hajdus
Highcharts Developer

Return to “Highcharts Usage”