Chartjs-Re-compile JSON data to make less rows

3👍

You can modify your data just before creating the chart.

var top6 = datachart.slice(0,5)
top6[5] = {
    label: 'other',
    value: datachart.slice(5).reduce(function(sum, data) {
      return sum + data.value
    }, 0)
}
var ctx2 = document.getElementById("chart-area2").getContext("2d");
var myChart = new Chart(ctx2).Pie(top6);

This can be made more general to get the top N entries

var getTopN = function(dataArray, n)
  var tops = dataArray.slice(0, n)
  tops[n] = {
    label: 'other',
    value: dataArray.slice(n).reduce(function(sum, data) {
      return sum + data.value
    }, 0)
  }
  return tops
}  

var top5 = getTopN(datachart, 5) 
var top10 = getTopN(datachart, 10)  

Leave a comment