Changing data when click button in chart.js

Jwon

I'm working on some website project, in I have problem with charts.js, I want to change the data inside of the chart, So when i click button the data value is changing, but i'm stuck on it, i don't know how to do it. Here's the example of the code that similar with my project Chart.js.

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 context = document.querySelector('#graph').getContext('2d');
    new Chart(context).Line(data);
<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>
Option 1
</button>
<button>
Option 2
</button>

Shree

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Switch chart.js data with button click

data in charts of charts.js is changing when I click in the line chart

Vue Chart.js - Chart is not updating when data is changing

Chart.js - Show new data on button click

setText not changing or updating when button click

height and width is not changing when click on button

changing culture(language) when button click

Chart not rendering w/Chart.js on button click

Changing style of a button on click

Changing image on button click

changing the text on a button on click

dc.js access data points in multiple charts when click datapoint in first chart

jQuery - product quantity not changing when I click plus button

Angular 5: Data of *ngFor changing after a click on a radio button

Changing url parameter with js without reloading page on button click

Chart js options not changing chart

Unable to supdate chart when switching dataset on button click

passing data when click dropdown button

When you click on the button, the data is not added

Passing data when you click a button?

Flutter http post data when click the button

When i click button the value is not sending in js?

how to transform changing data to be visualized in a chart.js

Laravel + Vue.js. Load more data when i click on the button

How to show a loading screen while data is processing when I click on a button - React js

Changing button text of different button on button click

Changing button width on click but not container

Changing button color on click JavaScript

Why image is not changing on button click?