Use .nice()
:
var values = [245, 483, 498, 597, 726, 788, 799, 974];
var scale = d3.scale.linear().domain(d3.extent(values)).nice();
var ticks = scale.ticks(9);
سؤال
Given a list of values
var values = [245, 483, 498, 597, 726, 788, 799, 974];
I want to get tick values that "surround" the extent of the values
domain:
// [200, 300, 400, 500, 600, 700, 800, 900, 1000]
Using the following does not work for this intent:
var scale = d3.scale.linear().domain(d3.extent(values));
var ticks = scale.ticks(9);
// [300, 400, 500, 600, 700, 800, 900] == ticks
That is no surprise since the D3.js reference says:
The returned tick values are […] guaranteed to be within the extent of the input domain.
المحلول
Use .nice()
:
var values = [245, 483, 498, 597, 726, 788, 799, 974];
var scale = d3.scale.linear().domain(d3.extent(values)).nice();
var ticks = scale.ticks(9);