Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

309
Vistas
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);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda