JChops
JChops

Reputation: 69

Highcharts: Cannot read property 'chart' of undefined

So I have this chart built out and it works except it keeps throwing the error Cannot read property 'chart' of undefined. I am reloading the chart on window resize so that the html labels reload in correct positions.

It's a double donut chart and shows/hides content based on the selected slice.

Any help would be greatly appreciated.

Upvotes: 2

Views: 9770

Answers (1)

Paweł Fus
Paweł Fus

Reputation: 45079

First of all, you define var chart variable in createChart function, so it always will be undefined - use only one definition (the one on top), later just assign chart to that variable. Anyway, I see two solutions:

  • use setTimeout() in resize event, to render chart with a delay. Not a reliable solution, because user can play around with width of the browser and something may be broken again

  • wrap initReflow method to check if chart exists, like this:

    (function(H, HA) {
      H.wrap(H.Chart.prototype.initReflow = function () {
        var chart = this,
            reflow = function (e) {
                if(chart && chart.options) {
                    chart.reflow(e);
                }
            };
    
    
        HA.addEvent(window, 'resize', reflow);
        HA.addEvent(chart, 'destroy', function () {
            HA.removeEvent(window, 'resize', reflow);
        });
      });
    })(Highcharts, HighchartsAdapter)
    

Working demo: https://jsfiddle.net/dpsn9gx8/7/

Edit: Since Highcharts 4.1.10 Highcharts adapter is built-in, so remove it and use Highcharts: https://jsfiddle.net/dpsn9gx8/11/

Upvotes: 1

Related Questions