【问题标题】:Points cut at half at the edges of top and bottom, at chartjs在 chartjs 的顶部和底部边缘处减半的点
【发布时间】:2021-12-22 13:23:28
【问题描述】:

如图所示,点在到达底部或顶部边缘时被切成两半(在本例中数据为 1 或 5 时)。

我尝试了填充,添加了一些“假”数据来扩展 1 和 5 的限制,并使用刻度上的回调函数将其删除。没有一个按预期工作

这是我对这个图表的配置。

const config = {
    type: 'line' as ChartType,
    data: data,
    options: {
      pointStyle: 'circle',
      //pointBackgroundColor: 'white',
      scales: {
        y: {
          ticks: {
            maxTicksLimit: 5,
            stepSize: 1,
          },
          min: 1,
          max: 5,
          reverse: true,
        },
      },
    },
  };

删除最小值和最大值会产生预期的输出。

但我需要最小值和最大值,因为我想要固定的 Y 轴值

任何线索如何解决这个问题?

【问题讨论】:

    标签: javascript angular typescript configuration chart.js


    【解决方案1】:

    您可以使用afterDataLimits 钩子来设置刻度的最大值和最小值,这样它仍然会溢出图表区域:

    const ctx = document.getElementById('chart').getContext('2d');
    const myChart = new Chart(ctx, {
      type: "line",
      data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
          data: [12, 19, 3, 10, 2, 3],
          borderColor: 'pink',
          backgroundColor: 'pink',
          radius: 10
        }]
      },
      options: {
        scales: {
          y: {
            afterDataLimits: (scale) => {
              scale.max = 10;
              scale.min = 0;
            }
          },
        },
      },
    });
    <canvas id="chart" width="250" height="120" />
    <script src="https://cdn.jsdelivr.net/npm/chart.js@3.6.0/dist/chart.min.js"></script>

    【讨论】:

    • 效果很好!谢谢。你认为这是最小值和最大值的错误吗?
    • 不确定,如果是这样,我认为这更像是我提供的解决方法中的一个错误,因为你说 max 时不要显示高于此点的任何内容,因此溢出会违反这一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    相关资源
    最近更新 更多