score:7
Accepted answer
Call Chart(context).Line(New data);
on button click to reload your chart.
var data = {
labels: ['January', 'February', 'March'],
datasets: [
{
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: [30,120,90]
},
{
fillColor: "rgba(100,220,220,0.7)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [10,70,110]
}
]
};
var data1 = {
labels: ['March', 'Apr', 'May'],
datasets: [
{
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: [50,100,140]
},
{
fillColor: "rgba(100,220,220,0.7)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [40,70,200]
}
]
};
var context = document.querySelector('#graph').getContext('2d');
new Chart(context).Line(data);
$("#btn1").on("click", function() {
var context1 = document.querySelector('#graph').getContext('2d');
new Chart(context1).Line(data);
});
$("#btn2").on("click", function() {
var context2 = document.querySelector('#graph').getContext('2d');
new Chart(context2).Line(data1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
<canvas id="graph" width="800px" height="400px"></canvas>
<button id="btn1">
Option 1
</button>
<button id="btn2">
Option 2
</button>
score:9
You should update the data that you want and call the update
method.
https://www.chartjs.org/docs/latest/developers/api.html#updateconfig
const context = document.querySelector('#graph').getContext('2d'),
chart = new Chart(context).Line(data);
$("#btn1").on("click", function() {
chart.data.datasets[0].data = [140,100,50];
chart.update();
});
Source: stackoverflow.com
Related Query
- Changing data when click button in chart.js
- data in charts of charts.js is changing when I click in the line chart
- Changing data when click button in chart.js/vue.js
- Update the chart data from an array stored in a variable on button click
- I need to increase data in a bar chart by one with a button click
- How to change chart js data on button click
- Chartjs Bar Chart showing old data when hovering
- Changing cursor to pointer on Chart.js bar chart when hover (mousemove) event is disabled?
- ng2-charts customize data and whole html content of tooltip displayed when hovering on bar chart
- chart.js - link to other page when click on specific section in chart
- Switch chart.js data with button click
- Chart not rendering w/Chart.js on button click
- Chart js shows old chart data when hover
- ChartJS v2 - Keep tooltip open when user click on a point of a multiple lines chart
- Chart JS flicker when new data is added to chart (Vue)
- Chart.js Bar Chart with a switch/radio button to group data / Troubleshooting bar color
- ChartJS doughnut data toggle on custom button click
- Unexpected error when attempting to update chart data in Chart.js, in a Vue app
- React - Display Chart when button is clicked
- Chart disappears, when pressing the submit button
- Chart.js animations when updating chart data
- Chartjs Bar Chart showing old data when hovering when use of ajax
- How to show tooltip only when data available in chart js?
- ChartJS dynamic line chart ghosting back to old data when hovered on
- chart js: when all the values passed to data are zeros, nothing is showing
- Show/hide All nested data in ChartJS Pie chart when outer is shown/hidden
- Chartjs to show more set of data of click of a button
- How can I show "No Data" message in Pie Chart when there is no data using VueJS?
- How do I destroy/update Chart Data in this chart.js code example?
- Changing chart data dynamically with C# from SQL database
More Query from same tag
- chartJSRadar downloadhandler creating empty png
- How to draw a needle on a custom donut chart and add datapoints to the needle?
- Implementing a choropleth chart using chartjs-chart-geo plugin with react-chartjs-2
- Charts.js : How to remove the Vertical Black lines in Bar Chart
- The engine “node” is incompatible with this module when installing React-chartjs-2 Chart.js,
- How to properly unpack data from PHP array encoded as JSON from a JavaScript file
- Chart JS Line Graph multitooltipkey background color issue
- Can't generate multi-axis combo chart (bar/line) using chart.js 2.7
- Does the min value of the xAxes have to be within the dataset
- Chart.js in Vue defining multiple datasets
- Chart js pie or doughnut charts 3 inside instead of 1
- Chart.js time scale graph - xAxis labelling
- How to keep side bars in Chartjs bar graph from clipping?
- How can I create custom tooltips for each data point in a graph?
- Why is my line chart using multiple lines to connect random points? (Chart.js)
- How to put 2 labels and distinct tooltips from two bars
- Can I arrange labels of legend in Doughnut chart one by one?
- How do I selecting a date range (like onClick but drag/select)
- Chart.JS plugin, draw after animation
- Can I add a callback function to the legend while keeping the original functionality?
- Error: "barWithErrorBars" is not a chart type. Vue - chartjs-chart-error-bars
- How to map multiple charts with chart.js in react
- Iterating through the same dataset in Chart.js
- Changing data when click button in chart.js
- Chart with tool tip values
- chart.js: hide specific ticks in y-axis and hide all x-axis grid line
- How do I make my Type:Time to work with my Line Chart?
- How do I change the colour of a Chart.js line when drawn above/below an arbitary value?
- Display Chart(.js) when mouse hover over: TypeError: t is null
- In ChartJS how do I change the color of a label in the legend?