Chartjs-I want to multiply chart data according as array length

0👍

You are creating your array as a String.

You need to create an array of objects [{},{},{}].

The method used to INSERT a new object in an array is PUSH().

Check the fiddle. Now it´s working OK (move the mouse on the screen to see the chart is there (you will only see it when mouse is over it):

window.onload = function() {

  var arr = ['HTML', 'CSS', 'JS'],
    i, data;
  data = [];
  for (i = 0; i < arr.length; i++) {
    if (i == arr.length - 1) {
      data.push({
        value: "300",
        color: "#fff",
        highlight: "#aaa",
        label: arr[i]
      });

    } else {
      data.push({
        value: "300",
        color: "#fff",
        highlight: "#aaa",
        label: arr[i]
      });

    }
  }

  var dat = data;

  var ctx = document.getElementById("chart-area").getContext("2d");
  window.myBar = new Chart(ctx).Doughnut(dat, {
    responsive: true,
  });
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/nnnick/Chart.js/master/Chart.js"></script>

<canvas id="chart-area"></canvas>

Leave a comment