首页 > 解决方案 > 使用 Chart.js - X 轴标签并未全部显示

问题描述

我将 Chart.js 用于我的饼图/条形图,并且在折线图和条形图中,由于某种原因,所有“x”轴标签并未全部显示,但在饼图中它们是:

**旁边所有 3 个图表的图像**

下面的代码已被使用,并且“类型”刚刚针对我希望使用的图形类型进行了更改:

var ctx = document.getElementById("my3Chart");

var mylineChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["Time Management", "Career Coach", "Stress & Wellbeing", "Note Taking", "Exam Prep", "Presentations"],
        datasets: [{
            label: 'Module Tracker',
            data: [6, 4, 2, 0, 3, 1],
            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)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
});

标签: javascripthtmlchart.js

解决方案


I've tested your code in a fiddle with Chart.js 2.0 for both bar/line and it looks fine. Chart takes in x-axis labels with:

labels: ["Time Management", "Career Coach", "Stress & Wellbeing", "Note Taking", "Exam Prep", "Presentations"]

The reason you can't see all your x-axis labels is that the width's are too thin. You can fix this by either expanding your width, or rotating your labels to be vertical instead.


推荐阅读