D3 比例尺如何只在比例尺上放置最小和最大数字

D3 scale how to put only the minimum and the max number on the scale

创建比例:

headers.forEach(function(d) {
            // Coerce values to numbers.
            jsonObj.forEach(function(p) {
                y[d] = d3.scale.linear().domain(
                        d3.extent(jsonObj, function(p) {
                            return +p[d] || 0;
                        })).range([ h, 0 ]);
                y[d].brush = d3.svg.brush().y(y[d]).on("brush", brush);
            });
        }); 



g.append("svg:g").attr("class", "axis").each(function(d) {
            d3.select(this).call(axis.scale(y[d]));

比例即将到来 [0.0,0.1,0.2,...,1.0]

我想要这样的东西 [0,1]。

可能吗?我该怎么做?

听起来你要找的是轴的.ticks()方法,它指定轴上的刻度数。

尝试:d3.select(this).call(axis.scale(y[d]).ticks(2));

你应该调查一下 axis.ticks()。我假设 axis.ticks(2) 只会给你最小和最大轴值。

如果您事先知道 min/max 值或想要计算它们,另一个可能有用的选项是 axis.ticksValues()

有关这两个方面的更多信息,以及有关轴的许多其他详细信息,请访问 https://github.com/mbostock/d3/wiki/SVG-Axes#ticks