Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 739
Philipp Avatar asked Apr 15 '14 11:04

Philipp


People also ask

How do you redraw a Highchart graph?

To redraw the chart you can simply use redraw() method (https://api.highcharts.com/class-refere ... art#redraw). You might also want to check update() method (https://api.highcharts.com/class-refere ... art#update).

Which config can be used to update the chart properties after the chart has been rendered?

Highcharts - Updating a chart's option after initial render.

How do I change the width and height of a Highchart?

use chart. setSize(width, height, doAnimation = true); in your actual resize function to set the height and width dynamically.

How do I destroy Highcharts?

destroy () - Removes the chart and purges memory. This method should be called before writing a new chart into the same container. It is called internally on window unload to prevent leaks. var hc_options = { chart: { renderTo: 'container' }, series: [{ name: 'USD to EUR', data: usdeur }] }; var chart=new Highcharts.


1 Answers

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]
}]
});
like image 51
Chetan Singhal Avatar answered Oct 06 '22 08:10

Chetan Singhal