Javascript Chart.js 条形图:如何在 v2.3 中删除条形之间的空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39879445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:00:42  来源:igfitidea点击:

Chart.js Bar Chart: How to remove space between the bars in v2.3?

javascriptchart.js

提问by DeannaD

I'm trying to remove the space between my bar chart bars, but even though I see this solution many places it doesn't work for me. It's also not mentioned in the Chart.js docs so that is odd. Can someone tell me how to specify it?

我正在尝试删除条形图栏之间的空间,但即使我在很多地方看到此解决方案,它也对我不起作用。Chart.js 文档中也没有提到它,所以这很奇怪。有人可以告诉我如何指定它吗?

var options = {
    barValueSpacing : 1,        // doesn't work; find another way
    barDatasetSpacing : 1,      // doesn't work; find another way

    legend: {
        display: false          // Hides annoying dataset label
    },
    tooltips: {
        callbacks: {
            label: function(tooltipItem) {
                return tooltipItem.yLabel;
            }
        }
    }
};

var ctx = document.getElementById("canvasX").getContext("2d");          
var myBarChart = new Chart(ctx, {
    type: 'bar',
    data: data,
    options: options
});

回答by Jonathon Hill

You need to set barPercentageand categoryPercentageto 1.0on the x-axis scale. Add this to your optionsobject:

您需要设置barPercentage,并categoryPercentage1.0在x轴刻度。将此添加到您的options对象:

var options = {
    ...
    scales: {
        xAxes: [{
            categoryPercentage: 1.0,
            barPercentage: 1.0
        }]
    }
};

See http://www.chartjs.org/docs/#bar-chart-chart-options

请参阅http://www.chartjs.org/docs/#bar-chart-chart-options