Chartjs-Chart.js dynamic dataset and tooltip on stacked bar

3👍

Please have a look at https://stackoverflow.com/a/59658062/2358409 to understand how it basically works.

Starting from this information, some data processing together with the use of chartjs-plugin-datalabels leads you to the following sample code.

const data = [
   {
      "date":"2020-02-07",
      "range":"1-3",
      "off":15
   },
   {
      "date":"2020-02-07",
      "range":"3-6",
      "off":30
   },
   {
      "date":"2020-02-08",
      "range":"1-4",
      "off":25
   },
   {
      "date":"2020-02-08",
      "range":"4-5",
      "off":15
   },
   {
      "date":"2020-02-08",
      "range":"5-6",
      "off":15
   }
];

const distinctDates = Array.from(new Set(data.map(o => o.date)));
const dataPerDate = distinctDates.map(d => data.filter(o => o.date == d));
const numberOfDatasets = Math.max.apply(null, dataPerDate.map(data => data.length));
const dataSets = [];
for (let i = 0; i < numberOfDatasets; i++) {
  dataSets.push({
    data: dataPerDate.map(data => i < data.length ? data[i].off : 0),
    ranges: dataPerDate.map(data => i < data.length ? data[i].range : ''),
    backgroundColor: distinctDates.map(d => 
      "rgba(" + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + ", 0.5)"),
    categoryPercentage: 1,
    barPercentage: 1
  }); 
}

new Chart(document.getElementById("myChart"), {
  type: "bar",
  data: {
    labels: distinctDates,
    datasets: dataSets
  },
  options: {
    plugins: {
	  datalabels: {
        display: context => context.dataset.data[context.dataIndex] > 0,
        formatter: (value, context) => value + ' (' + context.dataset.ranges[context.dataIndex] + ')'
			}
		},
    legend: {
      display: false
    },
    tooltips: {
      enabled: false
    },
    scales: {
      xAxes: [{
         stacked: true
      }],
      yAxes: [{
        stacked: true
      }]      
    }
  }
});
canvas {
  max-width: 300px
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels"></script>
<canvas id="myChart" width="100" height="100"></canvas>

Leave a comment