javascript - Changing data when click button in chart.js -


i'm working on website project, in have problem charts.js, want change data inside of chart, when click button data value changing, i'm stuck on it, don't know how it. here's example of code similar 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>

call chart(context).line(new data); on button click reload 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>


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -