防止 NVD3 图表轴中的负值

Prevent negative values in NVD3 chart axis

本文关键字:NVD3 防止      更新时间:2023-09-26

嗨,我需要将带有 NVD3/D3 的折线图限制为没有负数的 Y 轴。 我正在尝试根据以下答案来做到这一点:

chart.y1Axis.scale().domain([0, maxValue])
            .tickFormat(d3.format(',f'));

但是在链中添加".scale().domain([0, maxValue])"会杀死整个图表。 我不知道如何做这个基本的事情,也找不到一个有效的例子。 有什么想法吗? 谢谢!

y1Axis.scale()返回一个 scale 对象,.domain([0, maxValue])修改该对象。

.tickFormat(d3.format(',f'))需要一个轴对象,而不是缩放对象。要解决此问题,请在修改完轴后放置.scale()

chart.y1Axis
    .tickFormat(d3.format(',f'))
    .tickValues([1, 2, 3, 5, 8, 13, 21])
    .etc(...)
  .scale()
    .domain([0, maxValue])