NiceToMytyuk
NiceToMytyuk

Reputation: 4307

How to draw the stroke behind bars in Chart.js?

I've wrote a custom Bar Chart in Chart.JS which on dataset hover highlight the bars by drawing a stroke on it the issue is that stroke is drawn over bars while i would make it something like 'background color' instead.

enter image description here

Like the bars are visible because the stroke color opacity is set to 0.05 while if i set it to 1 obviously those will not be visible anymore.

The code

class CustomBar extends Chart.BarController {
    draw() {
        super.draw(arguments);
        if (this.chart.tooltip._active && this.chart.tooltip._active.length) {
            const points = this.chart.tooltip._active[0];
            const ctx = this.chart.ctx;
            const x = points.element.x;
            const topY = points.element.y + 150;
            const width = points.element.width;
            const bottomY = 0;

            ctx.save();
            ctx.beginPath();
            ctx.moveTo(x, topY * 100);
            ctx.lineTo(x + width * 1.3, bottomY);
            ctx.lineWidth = width * 4.3;
            ctx.strokeStyle = 'rgba(0, 0, 0, 0.05)';
            ctx.stroke();
            ctx.restore();
        }
    }
}

CustomBar.id = 'shadowBar';
CustomBar.defaults = Chart.BarController.defaults;

Chart.register(CustomBar);

Upvotes: 1

Views: 791

Answers (1)

LeeLenalee
LeeLenalee

Reputation: 31421

You will need a custom plugin for this, in there you can specify that you want it to draw before the datasets are being drawn. You can do that like so:

Chart.register({
  id: 'barShadow',
  beforeDatasetsDraw: (chart, args, opts) => {
    const {
      ctx,
      tooltip,
      chartArea: {
        bottom
      }
    } = chart;
    
    if (!tooltip || !tooltip._active[0]) {
      return
    }
    
    const point = tooltip._active[0];
    const element = point.element;
    const x = element.x;
    const topY = -(element.height + 150);
    const width = element.width;
    const bottomY = 0;
    const xOffset = opts.xOffset || 0;
    const shadowColor = opts.color || 'rgba(0, 0, 0, 1)';

    ctx.save();
    ctx.beginPath();
    ctx.fillStyle = shadowColor;
    ctx.fillRect(x - (element.width / 2) + xOffset, bottom, width * 1.3 * 4.3, topY);
    ctx.restore();
  }
});

const options = {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        backgroundColor: 'orange'
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        backgroundColor: 'pink'
      }
    ]
  },
  options: {
    plugins: {
      barShadow: {
        xOffset: -10,
        color: 'red'
      }
    }
  }
}

const 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.7.0/chart.js"></script>
</body>

Upvotes: 2

Related Questions