javascript – Chart.js设置条形图的最大条形大小

我想限制条形图的最大宽度

我的代码

<script>
        // bar chart data
        var a=[];
            a.push('kalai 2015-04-11');
        var b=[];
            b.push('300');
        var barData = {
            labels : a,datasets : [
                {
                    fillColor : "#48A497",strokeColor : "#48A4D1",data : b

                }
            ]
        }
        // get bar chart canvas
        var income = document.getElementById("income").getContext("2d");
        // draw bar chart
        new Chart(income).Bar(barData,{scaleGridlinewidth : 1});
        <!--new Chart(income).Bar(barData);-->
    </script>

这样做的方法是什么

单个值看起来像这样

条形图的大小随着条形数量增加而减小如何设置最大条形尺寸以使其更易于查看

解决方法

您可以添加认情况下不可用的新选项.但是您需要编辑chart.js

在Chart.js中添加这些代码
步骤1:

//Boolean - Whether barwidth should be fixed
        isFixedWidth:false,//Number - Pixel width of the bar
        barWidth:20,

在条形图的defaultConfig添加这两行

第2步:

calculateBarWidth : function(datasetCount){

                    **if(options.isFixedWidth){
                        return options.barWidth;
                    }else{**
                        //The padding between datasets is to the right of each bar,providing that there are more than 1 dataset
                        var baseWidth = this.calculateBaseWidth() - ((datasetCount - 1) * options.barDatasetSpacing);
                            return (baseWidth / datasetCount);
                    }

在条形图的calculateBarWidth函数添加此条件

现在,您可以通过设置将自定义js文件中的barWidth设置为选项

isFixedWidth:true,barWidth:xx

如果您不想指定固定的barwidth,只需更改isFixedWidth:false

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...