Chart.js horizontal bar width

12,888

Solution 1

Simply increase the canvas height. For Example;

<canvas id="horizontalbar" height="1000px"></canvas>

Solution 2

You have the barThickness option as mentionned in the docs here -> http://www.chartjs.org/docs/#bar-chart-chart-options

Solution 3

While this is not an official fix, it worked for me. I took advice from this link, https://github.com/chartjs/Chart.js/issues/2787

I store all my values within multiple arrays, so count number of objects and multiply by a specific number and set height of your chart wrapper ( div around the chart ).

<div class="chart-wrapper horizontalBar" style="position: relative; height: 50vh;">
    <canvas id="chart-location"></canvas>
</div>

Then after the chart is drawn, set the height (location_labels is an array housing all my labels)

$( ".horizontalBar" ).height(location_labels.length * 30);
Share:
12,888
Marc Rasmussen
Author by

Marc Rasmussen

SOreadytohelp Website

Updated on June 21, 2022

Comments

  • Marc Rasmussen
    Marc Rasmussen almost 2 years

    I have the following bar chart:

    bar chart

    The above chart is generated with the following code:

        var myChart = new Chart(ctx, {
        type: 'horizontalBar',
        data: {
            labels: labels,
            datasets: [{
                label: '# of Votes',
                data: data,
                backgroundColor: [
                    'rgba(255, 99, 132, 0.2)',
                    'rgba(54, 162, 235, 0.2)',
                    'rgba(255, 206, 86, 0.2)',
                    'rgba(75, 192, 192, 0.2)',
                    'rgba(153, 102, 255, 0.2)',
                    'rgba(255, 159, 64, 0.2)'
                ],
                borderColor: [
                    'rgba(255,99,132,1)',
                    'rgba(54, 162, 235, 1)',
                    'rgba(255, 206, 86, 1)',
                    'rgba(75, 192, 192, 1)',
                    'rgba(153, 102, 255, 1)',
                    'rgba(255, 159, 64, 1)'
                ]
            }]
        },
        options: {
            responsive: true,
            scales: {
                yAxes: [{
                    borderWidth: 40,
                    ticks: {
                        beginAtZero:true
                    }
                }]
            }
        }
    });
    

    As you can see, the bar width is fairly small.

    My question is, how can I increase the width of each bar?

    • Sven Koluem
      Sven Koluem over 7 years
      The width of the bar is defined by data?
    • Jeff Beagley
      Jeff Beagley almost 7 years
      Not sure why this is downvoted, I'm running into the same issue but only on a Horizontal Bar Chart. @SvenKoluem the OP is referring to the width of the X axis, which in the case of ChartJS is relative to whether its a Horizontal Bar chart or regular. For the sake of conversation, we're talking about the vertical height.
  • Jeff Beagley
    Jeff Beagley almost 7 years
    This causes categories to overlap for me.
  • Shinox
    Shinox almost 3 years
    Thx that helped me