javascript - dynamic highcharts with json data -
i m trying figure out possible make json data fetched dynamically database of php , mysql , can plotted highcharts dynamic auto updating? appreciated.
following code have tried , not working , want implement the website 10 lines.
<html> <head> <title>highchart example</title> <script type="text/javascript"src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script src="http://code.highcharts.com/highcharts.js"></script> <script src="http://code.highcharts.com/modules/exporting.js"></script> <script type="text/javascript"> var chart; function requestdata() { $.ajax({ url: 'live-server-data.php', success: function(point) { var series = chart.series[0], shift = series.data.length > 20; // shift if series // longer 2 // add point chart.series[0].addpoint(point, true, shift); // call again after 1 second settimeout(requestdata1, 1000); }, cache: false, }); } function requestdata1() { $.ajax({ url: 'live-server-data.php', success: function(point) { var series2 = chart.series[1], shift = series2.data.length > 20; // shift if series // longer 20 // add point chart.series[1].addpoint(point, true, shift); // call again after 1 second settimeout(requestdata, 1000); }, cache: false, }); } $(function () { $(document).ready(function() { chart = new highcharts.chart({ chart: { renderto: 'container', defaultseriestype: 'spline', events: { load: requestdata } }, title: { text: 'live random data' }, xaxis: { type: 'datetime', tickpixelinterval: 150, maxzoom: 20 * 1000 }, yaxis: { minpadding: 0.2, maxpadding: 0.2, title: { text: '', margin: 80 } }, series: [ { name: 'random data', data: [] }, { name: ' hahaha', data: [] } ], }); }); }); </script> </head> <body> <div id="container" style="min-width: 728px; height: 400px; margin: 0 auto"></div> </body> </html> *** live-server-data.php followed: <?php // set json header header("content-type: text/json"); // x value current javascript time, unix time multiplied // 1000. $x = time() * 1000; // y value random number $y = rand(48,52); // create php array , echo json $ret = array($x, $y); echo json_encode($ret); ?>
you can try
var options = { chart: { renderto: 'chart', }, credits: { enabled: false }, title: { text: 'impression/click overview', x: -20 }, xaxis: { categories: [{}] }, tooltip: { formatter: function() { var s = '<b>'+ this.x +'</b>'; $.each(this.points, function(i, point) { s += '<br/>'+point.series.name+': '+point.y; }); return s; }, shared: true }, series: [{},{}] }; $.ajax({ url: "json.php", data: 'show=impression', type:'post', datatype: "json", success: function(data){ options.xaxis.categories = data.categories; options.series[0].name = 'impression'; options.series[0].data = data.impression; options.series[1].name = 'click'; options.series[1].data = data.clicks; var chart = new highcharts.chart(options); } });
Comments
Post a Comment