nice method
Extends the domain so that it starts and ends on nice round values.
final x = ScaleLinear(
domain: [0.241079, 0.969679],
range: [0, 960],
interpolate: interpolateNumber,
)..nice();
x.domain; // [0.2, 1]
This method typically modifies the scale’s domain, and may only extend the
bounds to the nearest round value. Nicing is useful if the domain is
computed from data, say using d4_array.extent
, and may be irregular. If
the domain has more than two values, nicing the domain only affects the
first and last value. See also d4_array.tickStep
.
An optional tick count
argument allows greater control over the step
size used to extend the bounds, guaranteeing that the returned ticks
will exactly cover the domain.
final x = ScaleLinear(
domain: [0.241079, 0.969679],
range: [0, 960],
interpolate: interpolateNumber,
)..nice(40);
x.domain; // [0.24, 0.98]
Nicing a scale only modifies the current domain; it does not automatically nice domains that are subsequently set. You must re-nice the scale after setting the new domain, if desired.
Implementation
void nice([num count = 10]) {
var d = domain;
var i0 = 0;
var i1 = d.length - 1;
var start = d[i0];
var stop = d[i1];
num? prestep;
num step;
var maxIter = 10;
if (stop < start) {
step = start;
start = stop;
stop = step;
step = i0;
i0 = i1;
i1 = step as int;
}
while (maxIter-- > 0) {
step = d4_array.tickIncrement(start, stop, count);
//if (!step.isFinite) return;
if (step == prestep) {
d[i0] = start;
d[i1] = stop;
domain = d;
break;
} else if (step > 0) {
start = (start / step).floorToDouble() * step;
stop = (stop / step).ceilToDouble() * step;
} else if (step < 0) {
start = (start * step).ceilToDouble() / step;
stop = (stop * step).floorToDouble() / step;
} else {
break;
}
prestep = step;
}
}