How do I destroy/update Chart Data in this chart.js code example?
Happy New Year!
I'm tussling with the need to destroy or update chart data, so that on reload of the chart it doesn't show the presence of the previous data on mouseover.
I've seen the example on Chart.js's API page, and looked over numerous examples on SO, but none seem to match how my code was written by another developer. Could someone show me how using myLineChart.destroy(); or myLineChart.update(); can be applied?
The code:
// Display the chart
showChart = function (data, chartPosition) {
console.log(chartPosition)
new Chart(document.getElementById("line-chart-" + chartPosition), {
type: 'line',
data: data,
options: {
legend: {
position: 'bottom',
fillStyle: Color,
strokeStyle: Color,
labels: {
usePointStyle: true,
fontSize: 12,
boxWidth: 10,
}
},
title: {
display: true,
text: 'Main Title'
},
tooltips: {
callbacks: {
title: function(tooltipItems, data) {
return data.datasets[tooltipItems[0].datasetIndex].label;
},
label: function(tooltipItem, data) {
return "$" + Number(tooltipItem.yLabel).toFixed(0).replace(/./g, function(c, i, a) {
return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c;
});
}
}
},
scales: {
xAxes: [{
ticks: {
callback: function(value, index, values) {
return value + ' years';
}
}
}],
yAxes: [{
ticks: {
callback: function (value, index, values) {
return '$' + value.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
}
}
}]
}
}
});
};
javascript chart.js
add a comment |
Happy New Year!
I'm tussling with the need to destroy or update chart data, so that on reload of the chart it doesn't show the presence of the previous data on mouseover.
I've seen the example on Chart.js's API page, and looked over numerous examples on SO, but none seem to match how my code was written by another developer. Could someone show me how using myLineChart.destroy(); or myLineChart.update(); can be applied?
The code:
// Display the chart
showChart = function (data, chartPosition) {
console.log(chartPosition)
new Chart(document.getElementById("line-chart-" + chartPosition), {
type: 'line',
data: data,
options: {
legend: {
position: 'bottom',
fillStyle: Color,
strokeStyle: Color,
labels: {
usePointStyle: true,
fontSize: 12,
boxWidth: 10,
}
},
title: {
display: true,
text: 'Main Title'
},
tooltips: {
callbacks: {
title: function(tooltipItems, data) {
return data.datasets[tooltipItems[0].datasetIndex].label;
},
label: function(tooltipItem, data) {
return "$" + Number(tooltipItem.yLabel).toFixed(0).replace(/./g, function(c, i, a) {
return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c;
});
}
}
},
scales: {
xAxes: [{
ticks: {
callback: function(value, index, values) {
return value + ' years';
}
}
}],
yAxes: [{
ticks: {
callback: function (value, index, values) {
return '$' + value.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
}
}
}]
}
}
});
};
javascript chart.js
You need to be saving your chart to a variable. So wherenew Chart
is being called, set it to something likevar myChart = new Chart
. You can read about class constructors (Chart is the class, constructor is what happens when you callnew
) here. After that you'll be able to perform operations on your variable (likemyChart.destroy
).
– sheng
Jan 2 at 20:43
Thanks. Do I set the variable ahead of the existing showChart line or does it need to be set within the showChart function?
– ReindeerHorns
Jan 2 at 20:56
It needs to bet set wherever you initialize your chart, so in this case it would be inside theshowChart
function --showChart = function() { var myChart = new Chart... }
. Hope that helps.
– sheng
Jan 2 at 21:01
Thanks. I've set the var myChart. Where would it be best to implement the myChart.destroy(); ?
– ReindeerHorns
Jan 2 at 21:41
I can't exactly determine that without seeing more of your code (CodePen or JSFiddle would be nice) but you'll probably want to set your variable outside of the function and then clear as needed:var myChart = null; showChart = function(...) { if (myChart !== null) { myChart.destroy(); } myChart = ... }
– sheng
Jan 2 at 22:17
add a comment |
Happy New Year!
I'm tussling with the need to destroy or update chart data, so that on reload of the chart it doesn't show the presence of the previous data on mouseover.
I've seen the example on Chart.js's API page, and looked over numerous examples on SO, but none seem to match how my code was written by another developer. Could someone show me how using myLineChart.destroy(); or myLineChart.update(); can be applied?
The code:
// Display the chart
showChart = function (data, chartPosition) {
console.log(chartPosition)
new Chart(document.getElementById("line-chart-" + chartPosition), {
type: 'line',
data: data,
options: {
legend: {
position: 'bottom',
fillStyle: Color,
strokeStyle: Color,
labels: {
usePointStyle: true,
fontSize: 12,
boxWidth: 10,
}
},
title: {
display: true,
text: 'Main Title'
},
tooltips: {
callbacks: {
title: function(tooltipItems, data) {
return data.datasets[tooltipItems[0].datasetIndex].label;
},
label: function(tooltipItem, data) {
return "$" + Number(tooltipItem.yLabel).toFixed(0).replace(/./g, function(c, i, a) {
return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c;
});
}
}
},
scales: {
xAxes: [{
ticks: {
callback: function(value, index, values) {
return value + ' years';
}
}
}],
yAxes: [{
ticks: {
callback: function (value, index, values) {
return '$' + value.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
}
}
}]
}
}
});
};
javascript chart.js
Happy New Year!
I'm tussling with the need to destroy or update chart data, so that on reload of the chart it doesn't show the presence of the previous data on mouseover.
I've seen the example on Chart.js's API page, and looked over numerous examples on SO, but none seem to match how my code was written by another developer. Could someone show me how using myLineChart.destroy(); or myLineChart.update(); can be applied?
The code:
// Display the chart
showChart = function (data, chartPosition) {
console.log(chartPosition)
new Chart(document.getElementById("line-chart-" + chartPosition), {
type: 'line',
data: data,
options: {
legend: {
position: 'bottom',
fillStyle: Color,
strokeStyle: Color,
labels: {
usePointStyle: true,
fontSize: 12,
boxWidth: 10,
}
},
title: {
display: true,
text: 'Main Title'
},
tooltips: {
callbacks: {
title: function(tooltipItems, data) {
return data.datasets[tooltipItems[0].datasetIndex].label;
},
label: function(tooltipItem, data) {
return "$" + Number(tooltipItem.yLabel).toFixed(0).replace(/./g, function(c, i, a) {
return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c;
});
}
}
},
scales: {
xAxes: [{
ticks: {
callback: function(value, index, values) {
return value + ' years';
}
}
}],
yAxes: [{
ticks: {
callback: function (value, index, values) {
return '$' + value.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
}
}
}]
}
}
});
};
javascript chart.js
javascript chart.js
edited Jan 2 at 20:45
ReindeerHorns
asked Jan 2 at 20:12
ReindeerHornsReindeerHorns
11
11
You need to be saving your chart to a variable. So wherenew Chart
is being called, set it to something likevar myChart = new Chart
. You can read about class constructors (Chart is the class, constructor is what happens when you callnew
) here. After that you'll be able to perform operations on your variable (likemyChart.destroy
).
– sheng
Jan 2 at 20:43
Thanks. Do I set the variable ahead of the existing showChart line or does it need to be set within the showChart function?
– ReindeerHorns
Jan 2 at 20:56
It needs to bet set wherever you initialize your chart, so in this case it would be inside theshowChart
function --showChart = function() { var myChart = new Chart... }
. Hope that helps.
– sheng
Jan 2 at 21:01
Thanks. I've set the var myChart. Where would it be best to implement the myChart.destroy(); ?
– ReindeerHorns
Jan 2 at 21:41
I can't exactly determine that without seeing more of your code (CodePen or JSFiddle would be nice) but you'll probably want to set your variable outside of the function and then clear as needed:var myChart = null; showChart = function(...) { if (myChart !== null) { myChart.destroy(); } myChart = ... }
– sheng
Jan 2 at 22:17
add a comment |
You need to be saving your chart to a variable. So wherenew Chart
is being called, set it to something likevar myChart = new Chart
. You can read about class constructors (Chart is the class, constructor is what happens when you callnew
) here. After that you'll be able to perform operations on your variable (likemyChart.destroy
).
– sheng
Jan 2 at 20:43
Thanks. Do I set the variable ahead of the existing showChart line or does it need to be set within the showChart function?
– ReindeerHorns
Jan 2 at 20:56
It needs to bet set wherever you initialize your chart, so in this case it would be inside theshowChart
function --showChart = function() { var myChart = new Chart... }
. Hope that helps.
– sheng
Jan 2 at 21:01
Thanks. I've set the var myChart. Where would it be best to implement the myChart.destroy(); ?
– ReindeerHorns
Jan 2 at 21:41
I can't exactly determine that without seeing more of your code (CodePen or JSFiddle would be nice) but you'll probably want to set your variable outside of the function and then clear as needed:var myChart = null; showChart = function(...) { if (myChart !== null) { myChart.destroy(); } myChart = ... }
– sheng
Jan 2 at 22:17
You need to be saving your chart to a variable. So where
new Chart
is being called, set it to something like var myChart = new Chart
. You can read about class constructors (Chart is the class, constructor is what happens when you call new
) here. After that you'll be able to perform operations on your variable (like myChart.destroy
).– sheng
Jan 2 at 20:43
You need to be saving your chart to a variable. So where
new Chart
is being called, set it to something like var myChart = new Chart
. You can read about class constructors (Chart is the class, constructor is what happens when you call new
) here. After that you'll be able to perform operations on your variable (like myChart.destroy
).– sheng
Jan 2 at 20:43
Thanks. Do I set the variable ahead of the existing showChart line or does it need to be set within the showChart function?
– ReindeerHorns
Jan 2 at 20:56
Thanks. Do I set the variable ahead of the existing showChart line or does it need to be set within the showChart function?
– ReindeerHorns
Jan 2 at 20:56
It needs to bet set wherever you initialize your chart, so in this case it would be inside the
showChart
function -- showChart = function() { var myChart = new Chart... }
. Hope that helps.– sheng
Jan 2 at 21:01
It needs to bet set wherever you initialize your chart, so in this case it would be inside the
showChart
function -- showChart = function() { var myChart = new Chart... }
. Hope that helps.– sheng
Jan 2 at 21:01
Thanks. I've set the var myChart. Where would it be best to implement the myChart.destroy(); ?
– ReindeerHorns
Jan 2 at 21:41
Thanks. I've set the var myChart. Where would it be best to implement the myChart.destroy(); ?
– ReindeerHorns
Jan 2 at 21:41
I can't exactly determine that without seeing more of your code (CodePen or JSFiddle would be nice) but you'll probably want to set your variable outside of the function and then clear as needed:
var myChart = null; showChart = function(...) { if (myChart !== null) { myChart.destroy(); } myChart = ... }
– sheng
Jan 2 at 22:17
I can't exactly determine that without seeing more of your code (CodePen or JSFiddle would be nice) but you'll probably want to set your variable outside of the function and then clear as needed:
var myChart = null; showChart = function(...) { if (myChart !== null) { myChart.destroy(); } myChart = ... }
– sheng
Jan 2 at 22:17
add a comment |
2 Answers
2
active
oldest
votes
Chart.js provides a helper class from which you can access all the chart instances on a page.
If you want to destroy all the instances before update/reload use the following code:
Chart.helpers.each(Chart.instances, function (instance) {
instance.destroy();
});
If you want to destroy a particular chart using the canvas id, use the following code:
Chart.helpers.each(Chart.instances, function (instance) {
if (instance.chart.canvas.id === "yourChartId") {
instance.destroy();
return;
}
});
Also, this method avoids the overhead of storing a chart in a variable on creation.
– Kunal Khivensara
Jan 3 at 4:03
Not a bad solution but there are other ways to update the chart
– Aditya Gupta
Jan 3 at 4:04
Yes, we can use the same helper class to update a particular chart or all, very handy.
– Kunal Khivensara
Jan 3 at 4:05
add a comment |
You can modify the data used by the chart directly.
From there documentation
function addData(chart, label, data) {
chart.data.labels.push(label);
chart.data.datasets.forEach((dataset) => {
dataset.data.push(data);
});
chart.update();
}
function removeData(chart) {
chart.data.labels.pop();
chart.data.datasets.forEach((dataset) => {
dataset.data.pop();
});
chart.update();
}
Here you can pass the new data with the reference to the chart object. You can save the reference wwhen you create new Chart(...)
.
This is helpful to render the charts faster and in a smooth manner.
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012560%2fhow-do-i-destroy-update-chart-data-in-this-chart-js-code-example%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Chart.js provides a helper class from which you can access all the chart instances on a page.
If you want to destroy all the instances before update/reload use the following code:
Chart.helpers.each(Chart.instances, function (instance) {
instance.destroy();
});
If you want to destroy a particular chart using the canvas id, use the following code:
Chart.helpers.each(Chart.instances, function (instance) {
if (instance.chart.canvas.id === "yourChartId") {
instance.destroy();
return;
}
});
Also, this method avoids the overhead of storing a chart in a variable on creation.
– Kunal Khivensara
Jan 3 at 4:03
Not a bad solution but there are other ways to update the chart
– Aditya Gupta
Jan 3 at 4:04
Yes, we can use the same helper class to update a particular chart or all, very handy.
– Kunal Khivensara
Jan 3 at 4:05
add a comment |
Chart.js provides a helper class from which you can access all the chart instances on a page.
If you want to destroy all the instances before update/reload use the following code:
Chart.helpers.each(Chart.instances, function (instance) {
instance.destroy();
});
If you want to destroy a particular chart using the canvas id, use the following code:
Chart.helpers.each(Chart.instances, function (instance) {
if (instance.chart.canvas.id === "yourChartId") {
instance.destroy();
return;
}
});
Also, this method avoids the overhead of storing a chart in a variable on creation.
– Kunal Khivensara
Jan 3 at 4:03
Not a bad solution but there are other ways to update the chart
– Aditya Gupta
Jan 3 at 4:04
Yes, we can use the same helper class to update a particular chart or all, very handy.
– Kunal Khivensara
Jan 3 at 4:05
add a comment |
Chart.js provides a helper class from which you can access all the chart instances on a page.
If you want to destroy all the instances before update/reload use the following code:
Chart.helpers.each(Chart.instances, function (instance) {
instance.destroy();
});
If you want to destroy a particular chart using the canvas id, use the following code:
Chart.helpers.each(Chart.instances, function (instance) {
if (instance.chart.canvas.id === "yourChartId") {
instance.destroy();
return;
}
});
Chart.js provides a helper class from which you can access all the chart instances on a page.
If you want to destroy all the instances before update/reload use the following code:
Chart.helpers.each(Chart.instances, function (instance) {
instance.destroy();
});
If you want to destroy a particular chart using the canvas id, use the following code:
Chart.helpers.each(Chart.instances, function (instance) {
if (instance.chart.canvas.id === "yourChartId") {
instance.destroy();
return;
}
});
answered Jan 3 at 4:01
Kunal KhivensaraKunal Khivensara
759414
759414
Also, this method avoids the overhead of storing a chart in a variable on creation.
– Kunal Khivensara
Jan 3 at 4:03
Not a bad solution but there are other ways to update the chart
– Aditya Gupta
Jan 3 at 4:04
Yes, we can use the same helper class to update a particular chart or all, very handy.
– Kunal Khivensara
Jan 3 at 4:05
add a comment |
Also, this method avoids the overhead of storing a chart in a variable on creation.
– Kunal Khivensara
Jan 3 at 4:03
Not a bad solution but there are other ways to update the chart
– Aditya Gupta
Jan 3 at 4:04
Yes, we can use the same helper class to update a particular chart or all, very handy.
– Kunal Khivensara
Jan 3 at 4:05
Also, this method avoids the overhead of storing a chart in a variable on creation.
– Kunal Khivensara
Jan 3 at 4:03
Also, this method avoids the overhead of storing a chart in a variable on creation.
– Kunal Khivensara
Jan 3 at 4:03
Not a bad solution but there are other ways to update the chart
– Aditya Gupta
Jan 3 at 4:04
Not a bad solution but there are other ways to update the chart
– Aditya Gupta
Jan 3 at 4:04
Yes, we can use the same helper class to update a particular chart or all, very handy.
– Kunal Khivensara
Jan 3 at 4:05
Yes, we can use the same helper class to update a particular chart or all, very handy.
– Kunal Khivensara
Jan 3 at 4:05
add a comment |
You can modify the data used by the chart directly.
From there documentation
function addData(chart, label, data) {
chart.data.labels.push(label);
chart.data.datasets.forEach((dataset) => {
dataset.data.push(data);
});
chart.update();
}
function removeData(chart) {
chart.data.labels.pop();
chart.data.datasets.forEach((dataset) => {
dataset.data.pop();
});
chart.update();
}
Here you can pass the new data with the reference to the chart object. You can save the reference wwhen you create new Chart(...)
.
This is helpful to render the charts faster and in a smooth manner.
add a comment |
You can modify the data used by the chart directly.
From there documentation
function addData(chart, label, data) {
chart.data.labels.push(label);
chart.data.datasets.forEach((dataset) => {
dataset.data.push(data);
});
chart.update();
}
function removeData(chart) {
chart.data.labels.pop();
chart.data.datasets.forEach((dataset) => {
dataset.data.pop();
});
chart.update();
}
Here you can pass the new data with the reference to the chart object. You can save the reference wwhen you create new Chart(...)
.
This is helpful to render the charts faster and in a smooth manner.
add a comment |
You can modify the data used by the chart directly.
From there documentation
function addData(chart, label, data) {
chart.data.labels.push(label);
chart.data.datasets.forEach((dataset) => {
dataset.data.push(data);
});
chart.update();
}
function removeData(chart) {
chart.data.labels.pop();
chart.data.datasets.forEach((dataset) => {
dataset.data.pop();
});
chart.update();
}
Here you can pass the new data with the reference to the chart object. You can save the reference wwhen you create new Chart(...)
.
This is helpful to render the charts faster and in a smooth manner.
You can modify the data used by the chart directly.
From there documentation
function addData(chart, label, data) {
chart.data.labels.push(label);
chart.data.datasets.forEach((dataset) => {
dataset.data.push(data);
});
chart.update();
}
function removeData(chart) {
chart.data.labels.pop();
chart.data.datasets.forEach((dataset) => {
dataset.data.pop();
});
chart.update();
}
Here you can pass the new data with the reference to the chart object. You can save the reference wwhen you create new Chart(...)
.
This is helpful to render the charts faster and in a smooth manner.
answered Jan 3 at 4:10
Aditya GuptaAditya Gupta
8811922
8811922
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012560%2fhow-do-i-destroy-update-chart-data-in-this-chart-js-code-example%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You need to be saving your chart to a variable. So where
new Chart
is being called, set it to something likevar myChart = new Chart
. You can read about class constructors (Chart is the class, constructor is what happens when you callnew
) here. After that you'll be able to perform operations on your variable (likemyChart.destroy
).– sheng
Jan 2 at 20:43
Thanks. Do I set the variable ahead of the existing showChart line or does it need to be set within the showChart function?
– ReindeerHorns
Jan 2 at 20:56
It needs to bet set wherever you initialize your chart, so in this case it would be inside the
showChart
function --showChart = function() { var myChart = new Chart... }
. Hope that helps.– sheng
Jan 2 at 21:01
Thanks. I've set the var myChart. Where would it be best to implement the myChart.destroy(); ?
– ReindeerHorns
Jan 2 at 21:41
I can't exactly determine that without seeing more of your code (CodePen or JSFiddle would be nice) but you'll probably want to set your variable outside of the function and then clear as needed:
var myChart = null; showChart = function(...) { if (myChart !== null) { myChart.destroy(); } myChart = ... }
– sheng
Jan 2 at 22:17