javascript - Updating chart.js in ajax doesn't render the graph properly -


i trying plot line chart using chart.js ajax chart not plotted when data comes server. chart plotting fine on window onload event ajax failed render properly. issue? here code -

index.html:

<!doctype html> <html> <head> <meta charset="utf-8"> <title>ajax json chart</title> <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script> <script type="text/javascript" src="js/chart.min.js"></script> <script type="text/javascript"> var randomscalingfactor = function(){ return math.round(math.random()*100)}; var linechartdata = {     labels : ["january","february","march","april","may","june","july"],     datasets : [         {             label: "my first dataset",             fillcolor : "rgba(220,220,220,0.2)",             strokecolor : "rgba(220,220,220,1)",             pointcolor : "rgba(220,220,220,1)",             pointstrokecolor : "#fff",             pointhighlightfill : "#fff",             pointhighlightstroke : "rgba(220,220,220,1)",             data : [randomscalingfactor(),randomscalingfactor(),randomscalingfactor(),randomscalingfactor(),randomscalingfactor(),randomscalingfactor(),randomscalingfactor()]         }     ]  }  window.onload = function(){ var ctx = document.getelementbyid("canvas").getcontext("2d"); window.myline = new chart(ctx).line(linechartdata, {     responsive: false }); }  // ajax part of chart // $(document).ready(function(e) {     $("#frm1").submit(function(){     var canvas = document.getelementbyid('canvas');     ctx = canvas.getcontext('2d');      var btnchart = $("#btnchart").attr("name") + "=" + $("#btnchart").val();         $.ajax({             //beforesend: function(){ sendrequest("canvas"); },             cache: false,             type: "post",             datatype: "json",             url: "ajax/chart.php",             data: $(this).serialize() + "&" + btnchart,             success: function(data){                 //alert(data.labels);                 //alert(data.points);                 rendergraph(data.labels, data.points, ctx);             },         });     return false;     }); });  var rendergraph = function (labels, points, ctx) {          var linechartdata = {                         labels: "[" +  labels + "]",                         datasets: [                             {                                 label: "recent orders",                                 fillcolor: "rgba(220,220,220,0.2)",                                 strokecolor: "rgba(220,220,220,1)",                                 pointcolor: "rgba(220,220,220,1)",                                 pointstrokecolor: "#fff",                                 pointhighlightfill: "#fff",                                 pointhighlightstroke: "rgba(220,220,220,1)",                                 data: "[" +  points + "]",                              }                         ]                     };           var mychart = new chart(ctx)                                 .line(linechartdata, {                                         responsive: false,                                         animation: false                                         });                                         mychart.update(linechartdata);      }  function sendrequest(id) {     $("#"+id).html('<div style="width:100px; margin:0 auto;"><img id="loader-img" alt="" src="images/preloader.gif" width="100" height="100" /></div>'); }   </script> </head> <body> <div style="width:30%">     <div>         <canvas id="canvas" height="450" width="960"></canvas>     </div> </div> <form id="frm1" name="frm1" method="post" action=""> <input type="submit" name="btnchart" id="btnchart" value="draw chart" /> </form> </body> </html> 

chart.php:

<?php $labels = array('29 april 2015', '30 april 2015', '1 may 2015', '2 may 2015', '3 may 2015', '4 may 2015', '5 may 2015'); $points = array('100', '250', '10', '35', '73', '0', '25'); echo json_encode(array('labels' => $labels, 'points' => $points)); ?> 

on first time page loads, output is:

enter image description here

after clicking button "draw chart", output comes through ajax is:

enter image description here

but when hover mouse on image, automatically converts first image i.e. first correct graph when window loaded. strange!!!


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -