Chart.js 3.7.1 - 如何控制折线图 y 轴上的刻度数?

发布于 2025-01-15 07:02:56 字数 541 浏览 3 评论 0原文

数据始终是 -10 到 10 之间的整数,包括 0,因此 y 轴上最多可以有 21 个“间隔”。
无论数据如何,我都需要 y 轴以 1 为增量显示从 -10 到 10(包括 0)。因此,无论数据如何,我都需要 y 轴上总共 21 个(刻度?)。

我根据文档尝试了这个: 我也尝试过没有步骤参数:

options: {
    scales: {
        y: {
            max: -10,
            min: 10,                    
            ticks: {
                steps: 21,
                stepSize: 1
            }
        }
    }
}

这就是我得到的: 折线图

The data will always be integers between -10 and 10, including 0 so there could be a maximum of 21 'intervals' on the y axis.
Regardless of the data, I need the y axis to display from -10 to 10 (including 0) in increments of 1. So I need a total of 21 (ticks?) on the y axis regardless of the data.

I tried this, per the docs:
I also tried it without the steps param:

options: {
    scales: {
        y: {
            max: -10,
            min: 10,                    
            ticks: {
                steps: 21,
                stepSize: 1
            }
        }
    }
}

This is what I got:
Line Chart

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人生戏 2025-01-22 07:02:56

代码中缺少的部分是 autoSkip: false

y: {
  max: 10,
  min: -10,
  ticks: {
    stepSize: 1,
    autoSkip: false
  }
}

根据Chart.js 文档autoskip 默认为 true

autoskip:如果true,则自动计算可以显示的标签数量并相应地隐藏标签。 (...) 关闭 autoSkip 无论如何都会显示所有标签。

请看一下下面的可运行代码并了解它是如何工作的。

new Chart('canvas', {
  type: 'line',
  data: {
    labels: ['A', 'B', 'C', 'D', 'E', 'F'],
    datasets: [{
        label: 'Dataset 1',
        data: [3, 9, 7, 5, 9, 2],
        backgroundColor: 'rgba(255, 99, 132, 0.2)',
        borderColor: 'rgb(255, 99, 132)',
        fill: false
      },
      {
        label: 'Dataset 2',
        data: [1, 2, -3, -5, -2, 1],
        backgroundColor: 'rgba(255, 159, 64, 0.2)',
        borderColor: 'rgb(255, 159, 64)'
      }
    ]
  },
  options: {
    scales: {
      y: {
        max: 10,
        min: -10,
        ticks: {
          stepSize: 1,
          autoSkip: false
        }
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js"></script>
<canvas id="canvas"></canvas>

The missing part in your code is autoSkip: false.

y: {
  max: 10,
  min: -10,
  ticks: {
    stepSize: 1,
    autoSkip: false
  }
}

According to the Chart.js documentation, autoskip is true by default.

autoskip: If true, automatically calculates how many labels can be shown and hides labels accordingly. (...) Turn autoSkip off to show all labels no matter what.

Please take a look at below runnable code and see how it works.

new Chart('canvas', {
  type: 'line',
  data: {
    labels: ['A', 'B', 'C', 'D', 'E', 'F'],
    datasets: [{
        label: 'Dataset 1',
        data: [3, 9, 7, 5, 9, 2],
        backgroundColor: 'rgba(255, 99, 132, 0.2)',
        borderColor: 'rgb(255, 99, 132)',
        fill: false
      },
      {
        label: 'Dataset 2',
        data: [1, 2, -3, -5, -2, 1],
        backgroundColor: 'rgba(255, 159, 64, 0.2)',
        borderColor: 'rgb(255, 159, 64)'
      }
    ]
  },
  options: {
    scales: {
      y: {
        max: 10,
        min: -10,
        ticks: {
          stepSize: 1,
          autoSkip: false
        }
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js"></script>
<canvas id="canvas"></canvas>

春花秋月 2025-01-22 07:02:56

您可以使用以下代码管理刻度计数:

options: {
    scales: {
        x: {
            ticks: {
                maxTicksLimit: value
            }
        },
        y: {
            ticks: {
                maxTicksLimit: value 
            }
        }
    }
}

you can manage ticks count with this code:

options: {
    scales: {
        x: {
            ticks: {
                maxTicksLimit: value
            }
        },
        y: {
            ticks: {
                maxTicksLimit: value 
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文