Skip to content Skip to sidebar Skip to footer

Display Values In Pareto Chart Using Chart.js 2.0.2

I have a Pareto chart (using Chart.js Version: 2.0.2) and i need to display values inside of the bars and under the line. Any help will be very appreciated: This is the code (hope

Solution 1:

This is the answer i developed (only in the options object):

var options = {
    animation: {
        onComplete: function() {
            var ctx = this.chart.ctx;
            ctx.textAlign = "center";
            ctx.textBaseline = "middle";

            this.chart.config.data.datasets.forEach(function(dataset) {
                ctx.font = "20px Arial";
                switch (dataset.type) {
                    case"line":
                        ctx.fillStyle = "Black";
                        dataset.metaData.forEach(function(p) {
                            ctx.fillText(p._chart.config.data.datasets[p._datasetIndex].data[p._index], p._model.x, p._model.y - 20);
                        });
                        break;
                    case"bar":
                        ctx.fillStyle = "White";
                        dataset.metaData.forEach(function(p) {
                            ctx.fillText(p._chart.config.data.datasets[p._datasetIndex].data[p._index], p._model.x, p._model.y  + 20);
                        });
                        break;
                }
            });
        }
    }
...
};

And this is the result:

enter image description here

Solution 2:

I think since version 2.6.0 it's possible to Mix ChartTypes, like Bars and Lines

follow the instructions

A short example is 2 DataSet, one is bar type and other line type:

constdata = {
  labels: [
    'January',
    'February',
    'March',
    'April'
  ],
  datasets: [{
    type: 'bar',
    label: 'Bar Dataset',
    data: [10, 20, 30, 40],
    borderColor: 'rgb(255, 99, 132)',
    backgroundColor: 'rgba(255, 99, 132, 0.2)'
  }, {
    type: 'line',
    label: 'Line Dataset',
    data: [50, 50, 50, 50],
    fill: false,
    borderColor: 'rgb(54, 162, 235)'
  }]
};

You just have to calculate the Pareto values and put on the Dataset Line Type

Post a Comment for "Display Values In Pareto Chart Using Chart.js 2.0.2"