Chartjs-Not able to draw Doughnut Chart from json response

1👍

ChartJS expects an array of objects as parameter to the Doughnut() method. You on the other hand just create the objects and thereby overwrite the last object.

Instead you should declare pieData to be an array and add entries inside the each() callback like this:

$(document).on('click', '#plotGraph', function (event) {
  event.preventDefault();

  $.getJSON("PlotExpenseGraph", function (data1) {

    // prepare parameter to ChartJS
    var pieData = [];
    $.each(data1.expenseList, function (position, expenseList) {

      // add respective entry
      pieData.push({
        value : expenseList.totalValue,
        color : "#F7464A",
        highlight : "#FF5A5E",
        label : expenseList.param
      });

    });

    // paint
    var ctx = $("#myChart").get(0).getContext("2d");
    var myNewChart = new Chart(ctx);
    new Chart(ctx).Doughnut(pieData);
  });
});

Leave a comment