Redrawing canvas (deleting and creating again) and responsiveness
I am using chartjs with bootstrap. In my nodejs app, i am getting the date range and sending a post request through jquery. Before sending a post req i am deleting the canvas in the div and adding it again from jquery. I am doing this because after redrawing in the same canvas, while i hover sometimes it shows the old graph also. Now all worked fine with responsiveness set to true. now with the same page if i give another input and send a post req, all works fine but responsiveness is lost. Inspect element shows this error ( Cannot read property 'clientWidth' of null). New graphs are coming out well and the only problem is that its nor re sizing when i change the browser size.
I suspect this is because i am deleting the canvas and adding it again and drawing on it. If that's true help me to find a way to clear the canvas and redraw it. Note: Re sizing also works fine for the first post request. Subsequent post requests looses the responsiveness.
Jquery: $(document).ready(function(){
$("#fire1").click(function(){ $.post("postreq", { date:$("#fromdate").val(), todate:$("#todate").val() }, function(data,status){
$('#myChart4').remove(); // this is my <canvas> element
$('#div13').append('<canvas id="myChart4" width="150" height="200"><canvas>');
$('#myChart3').remove(); // this is my <canvas> element
$('#div12').append('<canvas id="myChart3" width="150" height="200"><canvas>');
var ctx = $("#myChart3").get(0).getContext("2d");
var chartdata1 = data.chart1;
var ctx1 = $("#myChart4").get(0).getContext("2d");
var chartdata2 = data.chart2;
var myDoughnutChart = new Chart(ctx).Doughnut(chartdata1 , { animateScale: true, animationSteps : 150, responsive: true, scaleIntegersOnly: true, maintainAspectRatio: true, tooltipEvents: ["mousemove", "touchstart", "touchmove", "onload"] });
var myDoughnutChart1 = new Chart(ctx1).Doughnut(chartdata2 , { animateScale: true, animationSteps : 150, responsive: true, scaleIntegersOnly: true, maintainAspectRatio: true, tooltipEvents: ["mousemove", "touchstart", "touchmove", "onload"] });
});
});
});
Please help me with this issue.
@jagadeeshops Take a look at: destroy() method; that'll let you destroy the old chart and then you can create a new one; without having to create and remove canvas elements!
i have the same problem, i use the next code var cxt = document.getElementById("myChartLine").getContext("2d");
var options = {
bezierCurve: true,
datasetFill:false,
scaleLineWidth: 2,
datasetStroke : true,
datasetLabel: true,
responsive: true,
maintainAspectRatio: true,
scaleGridLineColor : "rgba(0,0,0,0.2)",
scaleIntegersOnly: false,
scaleLabel : "<%= Number(value).toFixed(2).replace() + ' %'%>"
};
var myLine = new Chart(cxt).Line(JSON.parse(datos), options);
myLine.destroy()
myLine = new Chart(cxt).Line(JSON.parse(datos), options);
when i draw various chart in the same html do not work destroy(). Thank a lot
destroy() is the correct answer.
It is very inconvenient to have global variable for myLine to destroy it where i want.