Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda