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

281
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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