0

Is there a method which I can run on a jquery element to detect if a chart is already rendered in the element? For example this code will always return true as .highcharts() is defined, what method or property should I check to see if a chart has been rendered and is this the correct approach to re-draw a chart with new data?

function draw (element, chartData) {
        if( $(element).highcharts() ) {
            $(element).highcharts().destroy();
        }
    $(element).highcharts( chartData);
}
LiamRyan
  • 1,892
  • 4
  • 32
  • 61
  • possible duplicate of [How can i get access to a Highcharts chart through a DOM-Container](http://stackoverflow.com/questions/13049977/how-can-i-get-access-to-a-highcharts-chart-through-a-dom-container) – Jeroen Feb 20 '15 at 13:34
  • sorry you're right I did not find the linked question as it's phrased quite differently. Should I delete this question or vote to close as duplicate as someone has answered? – LiamRyan Feb 20 '15 at 14:50
  • It's okay. Duplicates are helpful, if anyone uses your search terms and finds this one, the duplicate-link will also give them info on this issue. If you agree and your rep's high enough you can add a close vote too. – Jeroen Feb 20 '15 at 15:03

1 Answers1

4

Just call highcharts() on the element without any parameters. E.g.:

$(function () {
    $('#container').highcharts({
        series: [{
            type: 'pie',
            data: [1, 2, 3]
        }]
    });

    console.log($('#container').highcharts());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>
Jeroen
  • 60,696
  • 40
  • 206
  • 339