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

274
Visualizações
How to use excess vertical space in stacked bar chart?

I am trying to create a stacked bar chart (with chart.js 3.7+) which uses the entire canvas:

new Chart(..., {
    type: 'bar',
    data: {
        labels: ['a','b','c','d','e'],
        datasets: [{
            backgroundColor: '#ff00ff',
            data: [20,40,60,40,20],
        },{
            backgroundColor: '#00ff00',
            data: [100,200,300,200,100],
        }]
    },
    options: {
        maintainAspectRatio: false,
        responsive: true,
        plugins: {
            legend: { display: false },
        },
        scales: {
            x: {
                display: false,
                stacked: true 
            }, 
            y: { 
                display: true,
                stacked: true 
            }
        }
    }
});

When I use a normal bar chart the graph is stretched to use up all the vertical space:

Bar chart

But when I stack the bars the combined longest bar doesn't use up all the vertical space:

Stacked bar chart

How can I get the stacked bar chart to stretch to use all available vertical space?

See example here.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could sum your arrays, find the max value in the resulting array and set the options.scales.y.max property with this value :

let data1 = [20,40,60,40,20];
let data2 = [100,200,300,200,100];

let sum = data1.map(function (num, idx) {
  return num + data2[idx];
});

let max = Math.max(...sum);

var options = 
{
    maintainAspectRatio: false,
    responsive: true,
    plugins: {
        legend: { display: false },
    },
    scales: {
      x: {
        display: false,
        stacked: true 
      }, 
      y: { 
        display: true,
        stacked: true,
        max: max
      }
    }
  };

var mychart = new Chart(document.getElementById('mycanvas2').getContext('2d'), {
  type: 'bar',
  data: {
    labels: ['a','b','c','d','e'],
    datasets: [{
      backgroundColor: '#ff00ff',
      data: data1
    },{
      backgroundColor: '#00ff00',
      data: data2
    }]
  },
  options: options
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.min.js"></script>
<div  style="width:400px; height: 200px">
  <canvas id="mycanvas2"></canvas>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

According to this issue with chart.js you can configure the y-axis bounds to fit the data:

options: {
    scales: {
        x: {
            display: false,
            stacked: true 
        }, 
        y: { 
            display: true,
            stacked: true,
            bounds: 'data' <----------
        }
    }
}
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