Support for "nice" polylinear scales.

This commit is contained in:
Jason Davies 2011-06-07 08:44:02 +01:00
Родитель 8421fca43d
Коммит ea8e8785a7
5 изменённых файлов: 25 добавлений и 10 удалений

9
d3.js поставляемый
Просмотреть файл

@ -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();
};

4
d3.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -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 ]