Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

barPercentage & categoryPercentage do not put the bars right next to each other

Open khalidahmedshalabi opened this issue 10 years ago • 7 comments

In Chart JS documentation, barPercentage is said to put the bars right next to each other if set to 1.0 since it will take the whole category width.

chartissue

However, I get this

chartissue

I want no spacing/padding between the two bars. Note: this occurs only if categoryPercentage is equal to anything other than 1.0. I don't want to set categoryPercentage to 1.0 because I don't want the category to take full width and I want the bars to be thin.

Live demonstration: http://codepen.io/anon/pen/KgoKyg?editors=0010

khalidahmedshalabi avatar Oct 08 '16 14:10 khalidahmedshalabi

@KHKKhalid i tried to look at your codepen but it is infinitely refreshing the page :/

etimberg avatar Oct 08 '16 15:10 etimberg

I have no clue why this would happen. Here's the code anyway

var ctx = document.getElementById("myChart");
var chart = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: ['X', 'Y'],
            datasets: [{
                label: 'Magnitude',
                data: [1, 2],
                backgroundColor: ['yellow', 'orange']
            }]
        },
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero: true,
                        fixedStepSize: 5,
                        max: 15,
                        min: 0
                    },
                    gridLines: {
                        color: 'black',
                        zeroLineColor: 'black',
                        zeroLineWidth: 4
                    }
                }],
                xAxes: [{
                    gridLines: {
                        display:false,
                        zeroLineWidth: 4,
                    },
                  barPercentage: 1.0,
                        categoryPercentage: 0.1
                }]
            }
        }
    });

khalidahmedshalabi avatar Oct 08 '16 15:10 khalidahmedshalabi

Does this work for you http://codepen.io/khalidahmedshalabi/pen/mAxJmp

khalidahmedshalabi avatar Oct 08 '16 17:10 khalidahmedshalabi

For anyone facing this issue, I figured out a workaround, set categoryPercentage and barPercentage to 1.0 (this will put bars next to each other), then keep pushing empty data to your graph (transparent color, 0 values) at the front and back (to make the bars centralized) of your graph until you reach the thickness you want.

Here's a snippet of my project that sorta illustrates how it works

var AddEmptyDataToChart = function ()
    {
        if(bars_shown < 4 * MAX_TABS)
        {
            var i;
            var data_count_on_each = Math.ceil(((4 * MAX_TABS) - bars_shown) / 2);

            // Add at the beginning and at the end
            for(i = 0; i < data_count_on_each; i ++)
            {
                // Beginning
                labels.push(" ");
                values.push(0);
                bgcol.push("rgba(0, 0, 0, 0.0)");
                bordercol.push("rgba(0, 0, 0, 0.0)");

                // End
                labels.unshift(" ");
                values.unshift(0);
                bgcol.unshift("rgba(0, 0, 0, 0.0)");
                bordercol.unshift("rgba(0, 0, 0, 0.0)");
            }
        }
    };

khalidahmedshalabi avatar Oct 12 '16 21:10 khalidahmedshalabi

I am facing the same issue in horizontal bar chart. Is there any fix for it?

ennyta avatar Nov 10 '17 16:11 ennyta

@ennyta check the workaround I posted above.

khalidahmedshalabi avatar Nov 13 '17 13:11 khalidahmedshalabi

It works as documented, you are just misunderstanding what a category is. 1 label = 1 category.

You can also split the data to individual datasets (so the bars can be in same category): https://codepen.io/kurkle/pen/oNgoNjR?editors=0010

kurkle avatar Jan 02 '20 14:01 kurkle