jquery Chart.js条形图厚度

tct7dpnv  于 2023-06-22  发布在  jQuery
关注(0)|答案(2)|浏览(160)

I need help on bar thickness using chart.js v3.5问题:我设置了条厚度**“barThickness:55,"**and after in responsive我不知道在responsive中管理它这里是我使用的代码,请让我知道我应该做什么的解决方案。谢谢
在这里我有这是在响应创建的问题,看看图像的URL:image link

var myNewChartB = new Chart(ctx, {
            type: "bar",
            data: barData,
            options: {
                borderSkipped: false,
                borderRadius: 3,
                plugins: {
                    barRoundness: 1,
                    legend: {
                        display: false,
                    },
                    title: {
                        font: {
                            size: 16
                        },
                        display: true,
                        text: 'Researchers (n=3)',
                        padding: {
                            left: 0,
                            right: 0,
                            top: 0,
                            bottom: 0
                        }
                    },
                    datalabels: {
                        display: true,
                        clamp: true,
                        formatter: (val, context) => (`${val}%`),
                        anchor: 'start',
                        align: 'end',
                    }
                },
    
                responsive: true,
                tooltips: {
                    enabled: false,
                },
                barThickness: 55,
                maintainAspectRatio: false,
    
                scales: {
                    x: {
                        display: true,
                        title: {
                            font: {
                                size: 16,
                            },
                            display: true,
                            text: "Scroes (1-9)",
                        },
                        grid: {
                            display: false,
                            drawBorder: false, //<- set this
                        },
                    },
                    y: {
                        display: true,
    
                        ticks: {
                            display: false
                        },
                        title: {
                            font: {
                                size: 16
                            },
                            display: true,
                            text: "% of Respondants",
                        },
                        grid: {
                            color: '#9B9898',
                            drawBorder: false, //<- set this
                        },
                    },
                },
            },
        });
oug3syen

oug3syen1#

我创造了一个小故事。看一下图表会自动响应。
我不知道这个data: barData,从哪里来。所以我创建了自己的数据集。

const ctx = document.getElementById('myNewChartB').getContext('2d');
const myNewChartB = new Chart(ctx, {
            type: "bar",
            data: {
        labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3],
            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)'
            ],
            borderWidth: 55
        }]
    },
            options: {
                borderSkipped: false,
                borderRadius: 3,
                plugins: {
                    barRoundness: 1,
                    legend: {
                        display: false,
                    },
                    title: {
                        font: {
                            size: 16
                        },
                        display: true,
                        text: 'Researchers (n=3)',
                        padding: {
                            left: 0,
                            right: 0,
                            top: 0,
                            bottom: 0
                        }
                    },
                    datalabels: {
                        display: true,
                        clamp: true,
                        formatter: (val, context) => (`${val}%`),
                        anchor: 'start',
                        align: 'end',
                    }
                },
    
                responsive: true,
                tooltips: {
                    enabled: false,
                },
                barThickness: 55,
                maintainAspectRatio: false,
    
                scales: {
                    x: {
                        display: true,
                        title: {
                            font: {
                                size: 16,
                            },
                            display: true,
                            text: "Scroes (1-9)",
                        },
                        grid: {
                            display: false,
                            drawBorder: false, //<- set this
                        },
                    },
                    y: {
                        display: true,
    
                        ticks: {
                            display: false
                        },
                        title: {
                            font: {
                                size: 16
                            },
                            display: true,
                            text: "% of Respondants",
                        },
                        grid: {
                            color: '#9B9898',
                            drawBorder: false, //<- set this
                        },
                    },
                },
            },
        });
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<canvas id="myNewChartB" width="400" height="400"></canvas>
7hiiyaii

7hiiyaii2#

在Chart.js 4+
我们可以使用barPercentage来控制条的厚度
就像这样:
const options = { barPercentage:0.3,};

相关问题