score:7
Accepted answer
I figured it out. This works:
function addData(chart, label, data) {
chart.data.labels = label
chart.data.datasets.forEach((dataset) => {
dataset.data = data;
});
chart.update();
}
$("#btn").click(function() {
addData (myChart, labelsNew, dataNew);
});
instead of pushing the data (which adds on), data needs to be allocated by " = ".
score:1
I see 2 problems:
- in function
updateData()
missing chart argument toremoveData(chart);
- click handler for button, use simply:
$("#btn").click(function() { updateData(myChart, labelsNew, dataNew) });
score:1
var config = {
type: 'line',
data: {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My First dataset",
data: [65, 0, 80, 81, 56, 85, 40],
fill: false
}]
}
};
var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx, config);
labelsNew = ["Why", "u", "no", "work", "???"];
dataNew = [2, 4, 5, 6, 10];
function addData(chart, label, data) {
chart.data.labels = label
chart.data.datasets.forEach((dataset) => {
dataset.data = data;
});
chart.update();
}
function clickupdate(){
addData(myChart, labelsNew, dataNew);
}
.chart-container {
height: 300px;
width: 500px;
position: relative;
}
canvas {
position: absolute;
}
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<div class="button-container">
<button onclick="clickupdate()">Change Data</button>
</div>
<div class="chart-container">
<canvas id="myChart"></canvas>
</div>
Source: stackoverflow.com
Related Query
- Chart.js update function (chart,labels,data) will not update the chart
- Annotation events not update the chart
- Angular-Charts bar chart does not update when I change the data, series, labels
- $scope variable do not respond to the onClick function of angular chart
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- chartjs line graph destroy function is not clearing the old chart instances
- Dynamically update the options of a chart in chartjs using Javascript
- Chart looks only grey, does not show the color - Chartjs,discordjs
- angular-chartjs line chart TypeError: t.merge is not a function
- Modify the labels in chart.js when I update the chart
- chart is not getting updated from the values it received from Jquery
- Chartjs destroy method not affecting the chart
- code works fine on jsfiddle but one function is not working on website
- React JS Chart JS 2 is not hiding the grid lines in the background
- Background color of the chart area in chartjs not working
- Update the chart data from an array stored in a variable on button click
- The chart list is not showing
- Issue with chartjs linear gradient for the mixed bar chart in ReactJS is not calculated for each individual Bars
- Chart.js not resorting rows based on plugin after using update function
- chart.js one of the chart from mixed chart is not plotting
- vertical grid line not taking full hight of the canvas in chart js 3.3.2
- Problem with script src for Chart.js. The CDN for Chart.js funtions fine, so my code is ok. Somehow I'm not linking the file correctly
- Charts.js does not render chart until I open the console
- How to update the chart dynamically with vue-chartjs?
- Chart.js chart not displaying until I open the browser's console
- Destroying chart.js is not working when chart created inside function - chart.destroy() is not a function
- Bar chart from Chart.js not showing the second dataset
- Chart.js: chart does not update
- The colors of the bar chart in chart.js is not showing. I tried background color and fill color but none of it worked
- Bar chart (chart.js) with only 2 points does not show one of the bars
More Query from same tag
- X and Y axis labels not displaying on line chart (Chart.js)
- chartjs tooltip html: Use data to populate tooltip
- Chart.JS removing data stuck in loop
- Different color for line segments in ChartJS
- Chart.js - if there is not value show 0
- Chartjs line chart gets all scrambled up when an x value coincides with a label
- passing each element of array in the labels of char.js
- Chart js loading screen
- Chart.js only appears randomly, and disappears on page refresh
- Multiple Charts of the Same Type in ChartJS2 ReactJS - Buggy Tooltips
- how can i use chartjs-plugin-annotation?
- Set Chart Background Image - Chart.js
- how to show only each 15th x-labels in line chart chart.js in angular 6?
- Cursor selects multiple points instead of just one - chart.js
- Chart js Datalabels styling
- Dynamically created Chart.js chart overpopulating time based x-axis?
- Using Radar Chart inside react component
- How to set the xAxes min and max values of time cartesian chart in Chart.js
- Angular 2 ng2-charts doughnut text in the middle disappears on mouse hover
- Chartjs Timestamp to Datetime labels
- How to enable timescale in chart.js line chart?
- TypeError: Cannot read property 'defaults' of undefined when using the react wrapper of chartjs
- How to align legend in angular-chart.js
- Generate Chart With 2 Datasets Charts.js
- ChartJs functional component does not refresh even when forceUpdate
- Who knows why I can't plot my chart properly?
- Updating Chartjs dataset in Ionic
- Chart.js returns a console error and does not display the chart when using variables as data input
- Is there any way to get y of specific x which not belongs to the dataset in Chart.js graph?
- How to visualize data with chart.js properly?