Support for "nice" polylinear scales.
This commit is contained in:
Родитель
8421fca43d
Коммит
ea8e8785a7
|
@ -2213,16 +2213,17 @@ d3.scale.linear = function() {
|
|||
};
|
||||
|
||||
scale.nice = function() {
|
||||
var start = domain[0],
|
||||
end = domain[domain.length - 1],
|
||||
var last = domain.length - 1,
|
||||
start = domain[0],
|
||||
end = domain[last],
|
||||
reverse = end < start,
|
||||
min = reverse ? end : start,
|
||||
max = reverse ? start : end,
|
||||
span = max - min;
|
||||
|
||||
var step = Math.pow(10, Math.round(Math.log(span) / Math.log(10)) - 1);
|
||||
domain = [Math.floor(min / step) * step, Math.ceil(max / step) * step];
|
||||
if (reverse) domain.reverse();
|
||||
domain[reverse ? last : 0] = Math.floor(min / step) * step;
|
||||
domain[reverse ? 0 : last] = Math.ceil(max / step) * step;
|
||||
|
||||
return rescale();
|
||||
};
|
||||
|
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -83,16 +83,17 @@ d3.scale.linear = function() {
|
|||
};
|
||||
|
||||
scale.nice = function() {
|
||||
var start = domain[0],
|
||||
end = domain[domain.length - 1],
|
||||
var last = domain.length - 1,
|
||||
start = domain[0],
|
||||
end = domain[last],
|
||||
reverse = end < start,
|
||||
min = reverse ? end : start,
|
||||
max = reverse ? start : end,
|
||||
span = max - min;
|
||||
|
||||
var step = Math.pow(10, Math.round(Math.log(span) / Math.log(10)) - 1);
|
||||
domain = [Math.floor(min / step) * step, Math.ceil(max / step) * step];
|
||||
if (reverse) domain.reverse();
|
||||
domain[reverse ? last : 0] = Math.floor(min / step) * step;
|
||||
domain[reverse ? 0 : last] = Math.ceil(max / step) * step;
|
||||
|
||||
return rescale();
|
||||
};
|
||||
|
|
|
@ -28,3 +28,10 @@ console.log(" 0.5 -> " + f(x(.5)));
|
|||
console.log(" 1.0 -> " + f(x(1)));
|
||||
console.log(" 1.5 -> " + f(x(1.5)));
|
||||
console.log("");
|
||||
|
||||
var x = d3.scale.linear();
|
||||
[[1.1, 1, 2, 3, 10.9], [123.1, 1, 2, 3, -.9]].forEach(function(d) {
|
||||
console.log("domain([" + d.map(f) + " ]).nice():");
|
||||
console.log(" ", x.domain(d).nice().domain());
|
||||
console.log("");
|
||||
});
|
||||
|
|
|
@ -20,3 +20,9 @@ domain(-1, 0, 1).range(-100, 0, 10):
|
|||
1.0 -> 10.000
|
||||
1.5 -> 15.000
|
||||
|
||||
domain([ 1.100, 1.000, 2.000, 3.000, 10.900 ]).nice():
|
||||
[ 1, 1, 2, 3, 11 ]
|
||||
|
||||
domain([ 123.100, 1.000, 2.000, 3.000,−0.900 ]).nice():
|
||||
[ 130, 1, 2, 3, -10 ]
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче