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

254
Vistas
how to highlight stack bar in ChartJS with onclick

Is it possible to select a column in a stack bar chart with a colored border? Like this

enter image description here

I started from here: https://jsfiddle.net/sdfx/hwx9awgn/

// Return with commas in between   
var numberWithCommas = function(x) {
  return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};
var dataPack1 = [40, 47, 44, 38, 27, 31, 25];
var dataPack2 = [10, 12, 7, 5, 4, 6, 8];
var dataPack3 = [17, 11, 22, 18, 12, 7, 5];
var dates = ["SN 1.0.1", "SN 1.0.2", "SN 1.0.3", "SN 1.0.4", "SN 1.0.5", "SN 2.0.0", "SN 2.0.1"];
// Chart.defaults.global.elements.rectangle.backgroundColor = '#FF0000';  
var bar_ctx = document.getElementById('bar-chart');
var bar_chart = new Chart(bar_ctx, {
      type: 'bar',
      data: {
        labels: dates,
        datasets: [{
          label: 'Bad Style',
          data: dataPack1,
          backgroundColor: "#512DA8",
          hoverBackgroundColor: "#7E57C2",
          hoverBorderWidth: 0
        }, {
          label: 'Warning',
          data: dataPack2,
          backgroundColor: "#FFA000",
          hoverBackgroundColor: "#FFCA28",
          hoverBorderWidth: 0
        }, {
          label: 'Error',
          data: dataPack3,
          backgroundColor: "#D32F2F",
          hoverBackgroundColor: "#EF5350",
          hoverBorderWidth: 0
        }, ]
      },
      options: {
        animation: {
          duration: 10,
        },
        tooltips: {
          mode: 'label',
          callbacks: {
            label: function(tooltipItem, data) {
              return data.datasets[tooltipItem.datasetIndex].label + ": " + numberWithCommas(tooltipItem.yLabel);
            }
          }
        },
        scales: {
          xAxes: [{
            stacked: true,
            gridLines: {
              display: false
            },
          }],
          yAxes: [{
            stacked: true,
            ticks: {
              callback: function(value) {
                return numberWithCommas(value);
              },
            },
          }],
        }, // scales         legend: {display: true}     } // options    } );

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Yes what you can do is in the onClick as second argument you get an array with all the active elements. So what you can do is loop over each dataset in your chart and see if the index matches one of the list with active elements.

If this is the case you give it a borderWidth of 1, else you put it to 0. After you did this you call chart variable.update();

To make this work you need to remove the hoverBorderWidth of all datasets and give all datasets a borderColor of yellow

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use attribute borderColor with an array and borderWith with and object.

datasets: [
        {
            label: 'Bad Style',
            data: dataPack1,
            backgroundColor: "#512DA8",
            borderColor: [
              'rgb(243, 255, 51)',
              'rgb(81,45,168)',
              'rgb(243, 255, 51)',
              'rgb(81,45,168)',
              'rgb(81,45,168)',
              'rgb(81,45,168)',
              'rgb(81,45,168)',
            ],
            borderWidth: {
               top: 0,
               right: 4,
               bottom: 0,
               left: 4
            },
        },

rgb(243, 255, 51) is yellow color.

about 4 years ago · Juan Pablo Isaza Denunciar

0

To change the color, try this:

var borderColor =  [
   'rgb(81,45,168)',
   'rgb(81,45,168)',
   'rgb(81,45,168)',
   'rgb(81,45,168)',
   'rgb(81,45,168)',
   'rgb(81,45,168)',
   'rgb(81,45,168)',
];
var bar_ctx = document.getElementById('bar-chart');
var bar_chart = new Chart(bar_ctx, {
    type: 'bar',
    data: {
        labels: dates,
        datasets: [
        {
            label: 'Bad Style',
            data: dataPack1,
            backgroundColor: "#512DA8",
            hoverBackgroundColor: "#7E57C2",
            hoverBorderWidth: 0,
            borderColor: borderColor,
            borderWidth: '4',
        },
        {
            label: 'Warning',
            data: dataPack2,
            backgroundColor: "#FFA000",
            hoverBackgroundColor: "#FFCA28",
            hoverBorderWidth: 0,
            borderColor: borderColor,
            borderWidth: '4',
        },
        {
            label: 'Error',
            data: dataPack3,
            backgroundColor: "#D32F2F",
            hoverBackgroundColor: "#EF5350",
            hoverBorderWidth: 0,
            borderColor: borderColor,
            borderWidth: '4',
        },
        ]
    },
    options: {
            onClick: function(e){
               var element = this.getElementAtEvent(e);
               borderColor[element[0]._index] = 'rgb(244,140,4)';
               this.update();
        },
            animation: {
               duration: 10,
        },
        tooltips: {
          mode: 'label',
          callbacks: {
          label: function(tooltipItem, data) { 
            return data.datasets[tooltipItem.datasetIndex].label + ": " + numberWithCommas(tooltipItem.yLabel);
          }
          }
         },
        scales: {
          xAxes: [{ 
            stacked: true, 
            gridLines: { display: false },
            }],
          yAxes: [{ 
            stacked: true, 
            ticks: {
                    callback: function(value) { return numberWithCommas(value); },
                    }, 
            }],
        }, // scales
        legend: {display: true}
    } // options
   }
);
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