JavaScript how can i reset chartjs object all instances and recreate it from scratch?

i am using backbonejs framework for front-end and using chartjs for charts, so to keep it simple :

  1. i have a view that renders tabs perfectly and in every tab there is a chart in it

  2. first time chart loads perfectly and every thing went great

  3. when i click on the some tab again it re-renders all it content including the chart from scratch

problem : "when switching back to tabs chart does not show"

my function is like :

createChart: function(collection){
var that = this;

/* this is for the collection that i coming from an API and i am just    spliting it into two arrays */ 
var data1 = [],
data2 = [];
collection.each(function(model){
data1.push(model.get('count'));
data2.push(model.get('title'));
}) 



// chartjs code
var data = {
labels: data2,
datasets: [
{
label: "My Second dataset",
fillColor: "#007B9D",
strokeColor: "#00BFF3",
pointColor: "#00BFF3",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: data1
}
]};

//"this" is the reference to above declared variable 
this.ctx = document.getElementById("user-activity- chart").getContext("2d");  
this.myBarChart = new Chart(that.ctx).Bar(data);

console.log(this.ctx);
console.log(this.myBarChart);

},

i logged first time values into console and it is as following

ctx log : CanvasRenderingContext2D {textBaseline: "alphabetic", textAlign: "start", font: "12px "Helvetica Neue", Helvetica, Arial, sans-serif", lineDashOffset: 0, miterLimit: 10…}

myBarchart log: Chart.Type.extend.ChartType {options: Object, chart: Chart, id: "chart-0", ScaleClass: function, datasets: Array[1]…}

i logged second time(after switching tab) values into console and it is as following

ctx log: CanvasRenderingContext2D {textBaseline: "alphabetic", textAlign: "start", font: "12px "Helvetica Neue", Helvetica, Arial, sans-serif", lineDashOffset: 0, miterLimit: 10…}

myBarChart log: Chart.Type.extend.ChartType {options: Object, chart: Chart, id: "chart-1", ScaleClass: function, datasets: Array[1]…}

i have tried

myBarchart.destroy()
myBarchart.clear()
myBarchart.update()

all these methods

and also tried doing solution over here : https://gist.github.com/skhisma/5689383

please donot judge me as i am new here in backbonejs thanks in advance

Answer:1

In Magento I have a savings box, which calculates how much you save based on the difference between the regular and discount price. Now I want that box to increase the saving amount based on the ...

In Magento I have a savings box, which calculates how much you save based on the difference between the regular and discount price. Now I want that box to increase the saving amount based on the ...

I am using the following code to create a kendo numeric textbox. Html <input type='text' name='num_wks_or_mons' id='num_wks_or_mons' tabindex='5'/> Jquery $("#num_wks_or_mons").width(295)....

I am using the following code to create a kendo numeric textbox. Html <input type='text' name='num_wks_or_mons' id='num_wks_or_mons' tabindex='5'/> Jquery $("#num_wks_or_mons").width(295)....

I am using the Incapsuala Web Application Firewall. It allows me to display the Incapsula seal on the bottom left of my pages. However, I have a few pop up pages that I need to generate for some PDF ...

I am using the Incapsuala Web Application Firewall. It allows me to display the Incapsula seal on the bottom left of my pages. However, I have a few pop up pages that I need to generate for some PDF ...

I'm using the Google visualization API barchart. I need to be able to do a few things with the following code: Display Percentages along the bottom. If there is 213 total, 81 should show 38% http:/...

I'm using the Google visualization API barchart. I need to be able to do a few things with the following code: Display Percentages along the bottom. If there is 213 total, 81 should show 38% http:/...

  1. google.visualization.numberformat percentage