Sulman Azhar
Sulman Azhar

Reputation: 1079

Chart js 2 bars with one customize label on top

I have few values like A and B now I have pair of them I am showing them on chart-js like this

enter image description here

Now for my use case, I am calculating the third value using A and B. E.g a=100 and b=50, and dividing them will give me c=2.0. Now I want to show this c value on top of A and B bar as a common label like this

enter image description here

"chart.js": "^3.3.0", react-js

const newChartInstance = new Chart(chartContainer.current, {
  

  type: "bar",
    options: {
      scales: {
        yAxes: [
          {
            ticks: {
              beginAtZero: true,
            },
          },
        ],
      },
      ...config.options,

      onClick: (e) => {
        const points = newChartInstance.getElementsAtEventForMode(
          e,
          "nearest",
          { intersect: true },
          true
        );

        if (points.length) {
          const firstPoint = points[0];

          var type =
            newChartInstance.data.datasets[firstPoint.datasetIndex].label;
          var label = newChartInstance.data.labels[firstPoint.index];
          var value =
            newChartInstance.data.datasets[firstPoint.datasetIndex].data[
              firstPoint.index
            ];
          // This is the result that you will use to breakdown the chart
          //console.log(label, value, type);
          dispatch(setClickedBar({ label, value, type, tile: props.tile }));
        }
      },
    },
    data: data,
  });

Upvotes: 0

Views: 1352

Answers (1)

LeeLenalee
LeeLenalee

Reputation: 31431

You can use a custom plugin for that:

var options = {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        backgroundColor: 'red'
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        backgroundColor: 'blue'
      }
    ]
  },
  options: {
    plugins: {
      customValue: {
        name: 'ROI',
      }
    }
  },
  plugins: [{
    id: 'customValue',
    afterDraw: (chart, args, opts) => {
      const {
        ctx,
        data: {
          datasets
        },
        _metasets
      } = chart;

      datasets[0].data.forEach((dp, i) => {
        let barValue = `${(datasets[1].data[i] + dp) / 2}%`;
        const lineHeight = ctx.measureText('M').width;
        const textVal = opts.name || 'fill'

        ctx.textAlign = 'center';

        ctx.fillText(barValue, _metasets[0].data[i].x, (_metasets[0].data[i].y - lineHeight * 1.5), _metasets[0].data[i].width);
        ctx.fillText(textVal, _metasets[0].data[i].x, (_metasets[0].data[i].y - lineHeight * 3), _metasets[0].data[i].width);
      });
    }
  }]
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.4.1/chart.js"></script>
</body>

Upvotes: 1

Related Questions