shaaaa
shaaaa

Reputation: 435

highchart chart redraw method is not refreshing the chart

I am curious to know why button 2 is not working in this example

In this jsfiddle example 'update' method attached to button 1 is working, but in my original code 'update' is also not working. It shows error: "object# has no method 'update'"

$(function () {
    var options= {
        chart: {
            renderTo: 'container',
            height: 400
        },
        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]        
        }]
    };
   
    var chart = new Highcharts.Chart(options);
    buttonhandle1();
    buttonhandle2()
});

function buttonhandle1(){
    $("#b1").click(function(){
            var chart = $('#container').highcharts();
            chart.yAxis[0].update({
                min:0,
            max: 5,
            tickInterval : 1,
        });
    });
}

function buttonhandle2(){
    var chart = $('#container').highcharts();
    $("#b2").click(function(){
        chart.yAxis[0].min= 0;
        chart.yAxis[0].max= 5;
        chart.yAxis[0].tickInterval = 1;
        chart.redraw();
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
<button id="b1"> button 1</button>
<button id="b2"> button 2</button>

Upvotes: 4

Views: 32277

Answers (2)

Sebastian Bochan
Sebastian Bochan

Reputation: 37578

Because when you use chart.redraw() you should also set isDirty flag as true

http://jsfiddle.net/uKWQ9/3/

chart.yAxis[0].isDirty = true;

Upvotes: 5

Mr.Hunt
Mr.Hunt

Reputation: 4851

Update works for me on button 2 as well, chart property can be accessed by closure.

function buttonhandle2(){
    var chart = $('#container').highcharts();
    $("#b2").click(function(){
        chart.yAxis[0].update({
            min:0,
            max: 5,
            tickInterval : 1
        });
    });
}

another option is to set isDirty on axis.

function buttonhandle3(){
    var chart = $('#container').highcharts();
    $("#b3").click(function(){
        chart.yAxis[0].min= 0;
        chart.yAxis[0].max= 5;
        chart.yAxis[0].tickInterval = 1;
        chart.yAxis[0].isDirty = true;
        chart.redraw();
    });
}

Code in fiddle. http://jsfiddle.net/MrHunt/uKWQ9/5/

Better to pass chart as argument to handlers instead of creating separate vars. Updated fiddle here: http://jsfiddle.net/MrHunt/uKWQ9/6/

Upvotes: 2

Related Questions