简体   繁体   中英

How to remove paddings in Bar chart? (Chart.JS)

Is it possible to remove paddings inside bar chart?

<canvas id="weeksChartFallout" width="660" height="200"></canvas>

var falloutArray = [12, 24, 20, 15, 18, 20, 22, 10, 10, 12, 14, 10, 16, 16];

var dataWeeksFallouts = {
    labels: ["16.02", "17.02", "18.02", "19.02", "20.02", "21.02", "22.02", "23.02", "24.02", "25.02", "26.02", "27.02", "28.02", "01.03"],
    datasets: [
        {
            label: "Fallouts",
            fillColor: "rgba(63,107,245,0.67)",
            data: falloutArray
        }
    ]
};

var fc = document.getElementById('weeksChartFallout').getContext('2d');

window.weeksChartFallout = new Chart(fc).Bar(dataWeeksFallouts,{
    barShowStroke : false,
    barValueSpacing : 4, //distance between bars
    barValueWidth: 20,
    scaleShowLabels: false,
    scaleFontColor: "transparent",
    tooltipEvents: []
});

I mean space between first bar and left line and, especially space between last Bar and end of the chart ( screenshot ).

Here is my Fiddle

The x scale left and right paddings are calculated in the calculateXLabelRotation. If you have only these kind of charts you could simply replace this function to return no padding, like below

var originalCalculateXLabelRotation = Chart.Scale.prototype.calculateXLabelRotation
Chart.Scale.prototype.calculateXLabelRotation = function () {
    originalCalculateXLabelRotation.apply(this, arguments);
    this.xScalePaddingRight = 0;
    this.xScalePaddingLeft = 0;
}

Fiddle - http://jsfiddle.net/ov9p5qhz/

Note that there is still some spacing on the left and right - that comes from your barValueSpacing: 4 option.

If you have other charts on the page that you don't want to keep separate, use Chart.noConflict()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM