Charts.js to Load new data set to an existing chart - Javascript Chart.js

Javascript examples for Chart.js:Chart Data

Description

Charts.js to Load new data set to an existing chart

Demo Code

ResultView the demo in separate window

<html lang="en">
   <head> 
      <title>  marcelo2605</title> 
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> 
   </head> 
   <body translate="no"> 
      <div class="container"> 
         <canvas id="myChart" width="600" height="400"></canvas> 
         <button id="btn-load">load</button> 
      </div> 
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> 
      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script> 
      <script>
      Chart.defaults.global.legend.display = false;
Chart.defaults.global.tooltips.enabled = true;
var data = {//from  w  ww .j a va 2 s.  co m
   labels: ["Argentina", "Brasil", "Chile", "Mexico", "Peru", "Paraguai", "Bolevia", "Haiti"],
   datasets: [{
       label: 'carga tribut?ria',
       data: [36.57, 31.60, 23.49, 23.52, 20.34, 21.99, 35.31, 19.37],
       backgroundColor: ['rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)', 'rgba(153, 102, 255, 0.2)', 'rgba(255, 159, 64, 0.2)', 'rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)']
   }]
};
var data2 = {
   labels: ["Argentina", "Brasil", "Chile", "M?xico", "Peru", "Paraguai", "Bol?via", "Haiti"],
   datasets: [{
       label: 'carga tribut?ria',
       data: [10, 20, 30, 40, 50, 60, 70, 80],
       backgroundColor: ['rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)', 'rgba(153, 102, 255, 0.2)', 'rgba(255, 159, 64, 0.2)', 'rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)']
   }]
};
var ctx = $("#myChart");
var myChart = new Chart(ctx, {
    type: 'polarArea',
    data: data,
    options: {
       responsive: true,
       title: {
            display: true,
            text: 'Carga tribut?ria'
        },
        scale: {
         ticks: {
           beginAtZero: true
         },
         reverse: false
       },
    }
});
$('#btn-load').on('click', function(){
   myChart.data.datasets[0] = data2['datasets'][0];
   myChart.data.labels = data2['labels'];
   myChart.update();
});
  
      </script>  
   </body>
</html>

Related Tutorials