Chartjs-Get data from sql database in chartjs using codeigniter

0👍

You are looping unnecessary myChart.

Try this.

$(document).ready(function(){

$.ajax({
  url:'<?php echo site_url("user/get_chart_data"); ?>',
  success:function(response)
  {
    var data = [];

    for (var i = 0; i < response.length; i++) {
      data.push(response[i].reply_message);
     }


      var ctx = document.getElementById("myChart").getContext('2d');
      var myChart = new Chart(ctx, {
          type: 'pie',
          data: {
              labels: ["Yes", "No"],
              datasets: [{
                  label: '# of Votes',
                  data: data,
                  backgroundColor: [
                      'rgba(255, 99, 132, 0.2)',
                      'rgba(54, 162, 235, 0.2)',

                  ],
                  borderColor: [
                      'rgba(255,99,132,1)',
                      'rgba(54, 162, 235, 1)',
                  ],
                  borderWidth: 2
              }]
          },

      });


  }
});

});  

Leave a comment