update bar graph and legend without re-drawing - Javascript highcharts

Javascript examples for highcharts:Bar Chart

Description

update bar graph and legend without re-drawing

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>Spider update animation</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
      <script type="text/javascript">
   $(function () {/*from w  ww .  jav  a 2  s.c o m*/
    $('#container').highcharts({
        chart: {
        type: 'bar',
            animation: {
                duration: 1000
            }
        },
        plotOptions: {
            series: {
            stacking: 'normal'
            }
        },
         legend: {
                    symbolHeight: '0',
                    layout: 'horizontal',
                    verticalAlign: 'top',
                    horizontalAlign: 'right',
                     labelFormatter: function() {
                        return '<span>' + this.name + ': ' + this.yData[ 0 ] + '</span>';
                    },
                    y: -15,
                    useHTML: true
                },
        xAxis: {
            categories: []
        },
        series: [{
            data: [29.9],
            name: 'one'
        },
        {            data: [39.9],
        name: 'two'
}]
    });
    var i = 1;
    $('#update').click(function () {
        var chart = $('#container').highcharts();
        var newData = [{
            data: [39.9],
            name: 'one'
        },
        {            data: [49.9],
        name: 'two'
}];
         var ctr,
                    count,
                    len = chart.series.length,
                    len2 = newData.length;
                for ( ctr = 0; ctr < len; ctr = ctr + 1 ) {
                    for ( count = 0; count < len2; count = count + 1 ) {
                        if ( newData[ count ].name === chart.series[ ctr].name ) {
                            chart.series[ ctr ].update({ data: newData[ count ].data[ 0 ] });
                            delete newData[ count ];
                            break;
                        }
                    }
                }
    });
});

      </script> 
   </head> 
   <body> 
      <script src="https://code.highcharts.com/highcharts.js"></script> 
      <div id="container" style="height: 300px"></div> 
      <button id="update">Update chart</button>  
   </body>
</html>

Related Tutorials