score:144
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, @Nerdroid and @Frzy, please upvote their comments/answers as they deserve the credit for these. Adding them here as this accepted answer would be incomplete without these
score:1
@elo's answer is correct and upvoted, though I had to tidy it a little to make it clearer:
const myChartEl = document.getElementById('the-id-name');
const myChart = Highcharts.charts[myChartEl.getAttribute('data-highcharts-chart')];
myChart
then becomes a live Highcharts object that exposes all current props present in the chart that's rendered in the myChartEl
. Since myChart
is a Highcharts object, one can chain prototype methods right after it, extend it or refer to it.
myChart.getTable();
myChart.downloadXLS();
setTimeout(() => Highcharts.fireEvent(myChart, "redraw"), 10);
One can also get myChart
through .highcharts()
, which is a jQuery
plugin:
var myChart = $("#the-id-name").highcharts();
The jQuery
plugin approach above requires jQuery
to be loaded before the plugin is used, and of course the plugin itself. It was the absence of this plugin that got me into looking for alternative ways to accomplish the same with pure vanilla JavaScript.
By using the pure JS approach I was able to do what I needed (the second code snippet) without having to rely on jQuery
:
score:2
... and with the help of a colleague... a better way to do it is...
getChartReferenceByClassName(className) {
var foundChart = $('.' + className + '').eq(0).parent().highcharts();
return foundChart;
}
score:4
var chart1; // globally available
$(document).ready(function() {
chart1 = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'bar'
},
title: {
text: 'Fruit Consumption'
},
xAxis: {
categories: ['Apples', 'Bananas', 'Oranges']
},
yAxis: {
title: {
text: 'Fruit eaten'
}
},
series: [{
name: 'Jane',
data: [1, 0, 4]
}, {
name: 'John',
data: [5, 7, 3]
}]
});
});
The var chart1 is global so you can use to access de highchart object doesnt matter wich is the container
chart1.redraw();
score:5
Without jQuery (vanilla js):
let chartDom = document.getElementById("testDivId");
let chart = Highcharts.charts[Highcharts.attr(chartDom, 'data-highcharts-chart')]
score:9
I found another way of doing it... mainly because I'm using Highcharts that are embedded in OutSystems Platform, and I don't have a way to control the way charts are created.
The way that I found was the following:
Give an identifying class to the chart using
className
attributechart: { className: 'LifeCycleMasterChart' }
Define an auxiliary function to get the chart by class name
function getChartReferenceByClassName(className) { var cssClassName = className; var foundChart = null; $(Highcharts.charts).each(function(i,chart){ if(chart.container.classList.contains(cssClassName)){ foundChart = chart; return; } }); return foundChart;
}
Use the auxiliary function wherever you need it
var detailChart = getChartReferenceByClassName('LifeCycleDetailChart');
Hope it helps you!
score:10
Simply with pure JS :
var obj = document.getElementById('#container')
Highcharts.charts[obj.getAttribute('data-highcharts-chart')];
score:22
var $chartCont = $('#container').highcharts({...}),
chartObj = Highcharts.charts[$chartCont.data('highchartsChart')];
chartCont is jQuery Object. chartObj is Highchart Chart Object.
This is using Highcharts 3.01
score:45
Source: stackoverflow.com
Related Query
- How can I get access to a Highcharts chart through a DOM-Container?
- How can I get a chart only with legends using highcharts
- How can I get highcharts Donut chart to auto calculate percentages
- How can I get a data label in my Highcharts Pie Chart that is a font-awesome icon?
- How can I get HighCharts column chart to scale the yAxis to not have so much whitespace?
- How can I get proper object through loop using javascript for Bubble Charts of Highcharts
- Highcharts - how can a chart with multi pies change container height automatic?
- How to invert a chart in Highcharts through code?
- how to get chart object inside a point event function in Highcharts
- Highcharts - How can I remove starting and ending padding from area chart
- How get data name in Highcharts pie chart legend instead of "Slice" using array of values?
- How can I get around "mixed active content" highcharts exporting error?
- How can I access the 'this' field in plotOptions of highcharts
- How to have a highcharts chart in a container of a fixed width, and with a horizontal scrollbar?
- How can I make milestone lines with a GANTT chart using the highcharts library?
- Highcharts Bubble Chart - How to get the size of the point
- Highcharts - How can I get the nearest point on click event
- How can I display crosshair labels on top of the chart with Highcharts
- How do I get Highcharts chart redraw to move faster?
- Highcharts tips ? How can I add a class or an id to an Hichcharts DOM component?
- How can i load external json data in highcharts to show the bar chart
- How can I prepare a Group Stacked Bar Chart in Highcharts using multiple and different types of data?
- How to get access to the chart from HighchartsReact component to ResponsiveGridLayout component when I'm using functional react component
- How can I reverse the series order of a pyramid chart in Highcharts
- how to get index of chart in multiple highchart container
- How can I change highcharts events after the chart has been initialized
- How can I get highcharts barChart bars to always fill the container?
- Highcharts Chart Bar - How can I display in the chart, only one column from my HTML table?
- How can I replace the term "Week" in highcharts Gantt Chart Timeline?
- how to make chart real time with 2 line and get data from php with highcharts
More Query from same tag
- Highcharts multiple axis with custom labels
- not able ot show date in x axis in highcharts
- how to present json data on Highchart
- Highcharts : Export multiple charts to pdf in custom layout
- Highcharts: How to make startRow and endRow work again when data is stored in Google Spreadsheet?
- Highcharts custom aggregate methods
- Tool tip pointed arrow pie highchart
- Highchart : get image url after exporting
- Highstock works vary slow when data sorted by string
- stacked bar chart using angular-highchart
- Highcharts datepicker - multiple series
- Remove title based on viewport width
- Sankey Diagram highchart Issue
- Use different markers in the same series of an irregular time highchart
- How to use HighChart's column-stacked-and-grouped graph dynamic date range in x-axis?
- Use JSON with HighCharts and AJAX
- High charts tooltip location issues
- Highcharts - update chart using ajax
- How to set xAxis only with time in Highstock step graph
- use material icons in highcharts
- Highcharts single row stacked horizontal bar: labels overlap --- bar segments too small to read "dataLabels" --- (change "distance" of "dataLabels"?)
- Set line opacity in highchart line chart
- Set additional information in highcharts tooltip
- highcharts tooltip wrong date
- How to add new chart(any charts, highcharts or d3 charts) in Serenity admin dashboard
- Highchart org chart type error with ionic application
- How can i have the same radius for each point of series with hover?
- Highcharts need parse data for showing points?
- Disable marker on certain points on highcharts
- Add non chart content in highchart pdf