Philipp
Philipp

Reputation: 807

Highchart - Redraw Chart after Window is resized

on my webpage is a chart drawn with Highchart. Now, if i resize my whole window i want this chart to adjusts its size (especially the width). The Code looks similar to this:

window.onresize=function(){resized();}

function resized()
{
   $("#container").highcharts().redraw();
}

If I overwrite the redraw event (with something like "alert("resized");") I will get a result so the method should be called - but the chart doesn't changes its size. I also tried to set the charts size manually with

$("#container").highcharts().setSize(width, height, false);

But both ways didn't work. Is there any other solution?

Upvotes: 7

Views: 16329

Answers (1)

Chetan Singhal
Chetan Singhal

Reputation: 124

Did you try code in this manner so it will automatically handle chart resizing on window resize.

 // create the chart
var chart = Highcharts.chart('container', {
chart: {
    events: {
        redraw: function () {
            var label = this.renderer.label('The chart was just redrawn', 100, 120)
                .attr({
                    fill: Highcharts.getOptions().colors[0],
                    padding: 10,
                    r: 5,
                    zIndex: 8
                })
                .css({
                    color: '#FFFFFF'
                })
                .add();

            setTimeout(function () {
                label.fadeOut();
            }, 1000);
        }
    }
},
xAxis: {
    categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},

series: [{
    data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});

Upvotes: 2

Related Questions