[英]How to set minimal step for y-axis in d3.js?
I have a bar chart where values can range from 0 to 5. The values can only be integers (0, 1, 2, 3, 4, 5). 我有一个条形图,其中值的范围可以从0到5。这些值只能是整数(0、1、2、3、4、5)。
However, the y-axis renders with smaller steps, for example 0, 0.5, 1, 1.5, 2 etc. I want to set the axis values to integers only, but playing with domain and range hasn't helped me at all. 但是,y轴的绘制步长较小,例如0、0.5、1、1.5、2等。我想将轴值仅设置为整数,但是使用domain和range根本没有帮助。
I don't see an option to set something like minimalInterval = 1
. 我看不到用于设置诸如
minimalInterval = 1
类的选项的选项。 How do I do this? 我该怎么做呢? I'm sure there's an option somewhere.
我确定某个地方有一个选项。 Current code for the axes:
轴的当前代码:
var x = d3.scaleBand().rangeRound([0, width]).padding(0.1),
y = d3.scaleLinear().rangeRound([height, 0]);
x.domain(data.map(function(d) { return d.day; }));
y.domain([0, d3.max(data, function(d) { return d.value; })]);
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.selectAll("text")
.attr("y", 0)
.attr("x", 9)
.attr("dy", ".35em")
.attr("transform", "rotate(90)")
.style("text-anchor", "start");
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end");
There is nothing like steps
for a D3 generated axis. D3生成的轴没有类似的
steps
。
However, in your case, the solution is simple: you can use tickValues
with d3.range(6)
and a formatter for integers or, even simpler, you can use ticks
. 但是,对于您而言,解决方案很简单:您可以将
tickValues
与d3.range(6)
并使用整数的格式化程序,或者甚至更简单地,可以使用ticks
。
According to the API, 根据API,
Sets the arguments that will be passed to scale.ticks and scale.tickFormat when the axis is rendered, and returns the axis generator.
设置在渲染轴时将传递给scale.ticks和scale.tickFormat的参数,并返回轴生成器。 The meaning of the arguments depends on the axis' scale type: most commonly, the arguments are a suggested count for the number of ticks (or a time interval for time scales), and an optional format specifier to customize how the tick values are formatted.
参数的含义取决于轴的刻度类型:最常见的是,参数是对刻度数(或时间刻度的时间间隔)的建议计数,以及一个可选的格式说明符,用于自定义刻度值的格式。
So, in your case: 因此,在您的情况下:
axis.ticks(5, "f");
Where 5
is the count and f
is the specifier for fixed point notation. 其中
5
是计数, f
是定点符号的说明符。
Here is a demo (with an horizontal axis): 这是一个演示(带有水平轴):
var svg = d3.select("svg"); var scale = d3.scaleLinear() .domain([0, 5]) .range([20, 280]); var axis = d3.axisBottom(scale) .ticks(5, "f") var gX = svg.append("g") .attr("transform", "translate(0,50)") .call(axis)
<script src="https://d3js.org/d3.v4.min.js"></script> <svg></svg>
Just for completeness, the same code without ticks
: 为了完整起见,相同的代码没有
ticks
:
var svg = d3.select("svg"); var scale = d3.scaleLinear() .domain([0, 5]) .range([20, 280]); var axis = d3.axisBottom(scale); var gX = svg.append("g") .attr("transform", "translate(0,50)") .call(axis)
<script src="https://d3js.org/d3.v4.min.js"></script> <svg></svg>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.