【问题标题】:chartjs chart spilling out of containerchartjs图表溢出容器
【发布时间】:2020-09-21 11:45:33
【问题描述】:

我有一个 chartjs 图表,在我更改了 x 轴的日期格式后,图表从其容器中溢出。它似乎受此影响,但我需要格式保持 dd-mm-yyyy。谁能想到它为什么这样做?我试过改变保持纵横比/响应布尔值,我试过改变画布的高度和宽度,但也没有用。

我的编码:

   <div>
    <canvas id="Chart" width="400" height="200"></canvas>
</div>


<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script>

<script>

var ctx = document.getElementById("Chart").getContext('2d');
var recentActivityChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: [],
        datasets: [{
            label: 'hours',
            data: [],
            barThickness: 12,
            fill: true,
            backgroundColor: "rgba(54, 162, 235, 1)",
            borderColor: "rgba(54, 162, 235, 1)",
            borderWidth: 1,
        }]
    },
    options: {
        animation: {
            duration: 1000,
            easing: "linear",
        },
        responsive: true,
        maintainAspectRatio: true,
        legend: {
            display: false,
            position: 'bottom',
            usePointStyle: true,
            labels: {
                fontColor: "grey",
                usePointStyle: true,
            },
        },
        scales: {
            yAxes: [{
                gridLines: {
                    display: true,
                    borderDash: [8, 4],
                },
                scaleLabel: {
                    display: true,
                    labelString: 'hours',
                },
                ticks: {
                    beginAtZero: false,
                }
            }],
            xAxes: [{
                type: 'time',
                time: {
                    unit: 'day',
                    displayFormats: {
                        day: 'DD-MM-YYYY'
                    },
                },
                gridLines: {
                    scaleShowVerticalLines: false,
                    display: false,
                },
                ticks: {
                    beginAtZero: false,
                }
            }]
        },
    }
});
</script>

这就是它的样子:

【问题讨论】:

    标签: javascript chart.js bar-chart


    【解决方案1】:

    您应该将选项 offset: true 添加到 x 轴,如下所示:

    xAxes: [{
      type: 'time',
      offset: true,
    

    请查看您修改后的代码,看看它是如何工作的。

    var ctx = document.getElementById("Chart").getContext('2d');
    var recentActivityChart = new Chart(ctx, {
      type: 'bar',
      data: {
        labels: ['2020-01-01', '2020-01-02', '2020-01-03'],
        datasets: [{
          label: 'hours',
          data: [60, 44, 43],
          barThickness: 12,
          backgroundColor: "rgba(54, 162, 235, 1)",
          borderColor: "rgba(54, 162, 235, 1)",
          borderWidth: 1
        }]
      },
      options: {
        animation: {
          duration: 1000,
          easing: "linear",
        },
        responsive: true,
        maintainAspectRatio: true,
        legend: {
          display: false,
          position: 'bottom',
          usePointStyle: true,
          labels: {
            fontColor: "grey",
            usePointStyle: true,
          },
        },
        scales: {
          yAxes: [{
            gridLines: {
              display: true,
              borderDash: [8, 4],
            },
            scaleLabel: {
              display: true,
              labelString: 'hours',
            },
            ticks: {
              beginAtZero: false,
            }
          }],
          xAxes: [{
            type: 'time',
            offset: true,
            time: {
              unit: 'day',
              displayFormats: {
                day: 'DD-MM-YYYY'
              },
            },
            gridLines: {
              scaleShowVerticalLines: false,
              display: false,
            },
            ticks: {
              beginAtZero: false,
            }
          }]
        },
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script>
    <canvas id="Chart" width="400" height="200"></canvas>

    【讨论】:

      猜你喜欢
      • 2019-03-18
      • 2021-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多