score:2

Accepted answer

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>


Related Query

More Query from same tag