javascript - Dynamic Highcharts is not updating -
here use case. using flume , spark streaming count number of events generated server. in output of spark timestamp , number of events. after every 10 second old value erased , new value written in file in hdfs.
i have created spring framework pull data hdfs convert json , push highcharts. trying generate live spline chart show timestamp in x-axis , number of count in y-axis , update gets new data spark output after every 10 seconds. x-axis not shifting , can see 10 0 values in y-axis. unable updated y value. sure missing in java script. because console of spring can see events updating after every 10 seconds. not reflecting in chart.
i stuck here past 2 weeks , desperately need help. appreciated. thanks.
<!doctype html> <html> <head> <meta charset="utf-8"> <title>insert title here</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script type="text/javascript" src="http://code.highcharts.com/highcharts.js" ></script> <script type="text/javascript" src="http://code.highcharts.com/maps/modules/exporting.js" ></script> <script type="text/javascript" src="http://code.highcharts.com/modules/drilldown.js"></script> <script src="http://code.highcharts.com/modules/exporting.js"></script> <script type="text/javascript" src="js/app/appstart.js" ></script> <link type="text/css" href="css/app.css" rel="stylesheet"> <script src="http://code.highcharts.com/stock/modules/exporting.js"></script> </head> <body> <div class="chart" id="statuschart" > </div> </body> $(function() { var url = 'statistics'; $.ajax(url).done(function(data) { loadstatuschart(data); }); }); function updatestatusseries(series){ var url = 'statistics'; $.ajax(url).done(function(data) { var statuscode = data.statuscode[0]; series.addpoint([statuscode.label, statuscode.value], true, true); }); } function loadstatuschart(statistics) { var statuscode = statistics.statuscode; var statusseries = []; $.each(statuscode, function(index, item) { var timeinseconds = item.label/1000; for(var = 10; >= 1; i--){ statusseries.push({ x : (timeinseconds - (i*10))*1000, y : 0 }); } statusseries.push({ x : item.label, y : item.value }); }) var x = statuscode.length; $("#statuschart").highcharts({ chart : { type : 'spline', //animation : highcharts.svg, marginright : 10, events : { load : function(){ //set updating of chart each second var series = this.series[0]; setinterval(function (){ updatestatusseries(series); }, 10000); } } }, title: { text: 'live data' }, xaxis: { type: 'datetime' }, yaxis: { title: { text: 'number of counts' }, plotlines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { formatter: function () { return '<b>' + this.series.name + '</b><br/>' + highcharts.dateformat('yyyy/mm/dd hh:mm:ss', this.x) + '<br/>' + highcharts.numberformat(this.y, 2); } }, legend: { enabled: false }, exporting: { enabled: false }, series: [{ data: statusseries }] }); };
first, you're getting leading zeroes in chart because of code:
for(var = 10; >= 1; i--){ // <----- 10 statusseries.push({ x : (timeinseconds - (i*10))*1000, y : 0 // <----- 0 values }); }
second, chart isn't advancing because in part
events : { load : function(){ //set updating of chart each second var series = this.series[0]; setinterval(function (){ updatestatusseries(series); // <----- not updating series }, 10000); } }
updatestatusseries(series)
doesn't update series
, chart doesn't new data , stays on same spot.
to fix can either
create global
chart
object , use access series, described here: http://www.highcharts.com/docs/working-with-data/live-datamodify
updatestatusseries()
uses callback passed it, shown here: http://www.in-example.com/?p=316
also, date formatting (in tooltip.formatter
) should done so:
highcharts.dateformat('%y-%m-%d %h:%m:%s', this.x)
Comments
Post a Comment