tickFormat method
Like ScaleLinear.tickFormat
, but customized for a log scale.
The specified count
typically has the same value as the count that is
used to generate the tick values.
final x = ScaleLog(
domain: [1, 100],
range: [0, 960],
interpolate: interpolateNumber
);
final T = x.ticks(); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, …]
final f = x.tickFormat();
T.map(f); // ["1", "2", "3", "4", "5", "", "", "", "", "10", …]
If there are too many ticks, the formatter may return the empty string for some of the tick labels; however, note that the ticks are still shown to convey the logarithmic transform accurately. To disable filtering, specify a count of Infinity.
When specifying a count
, you may also provide a format specifier
or
format function. For example, to get a tick formatter that will display 20
ticks of a currency, say log.tickFormat(20, "$,f")
. If the specifier
does not have a defined precision, the precision will be set automatically
by the scale, returning the appropriate format. This provides a convenient
way of specifying a format whose precision will be automatically set by
the scale.
Implementation
String Function(num) tickFormat([num count = 10, Object? specifier]) {
specifier ??= base == 10 ? "s" : ",";
if (specifier is! String Function(dynamic)) {
if (base.remainder(1) == 0 &&
(specifier = FormatSpecifier.parse(specifier as String)).precision ==
null) (specifier as FormatSpecifier).trim = true;
specifier = format(specifier.toString());
}
if (count == double.infinity) return specifier;
final k = max(1, base * count / ticks().length); // TODO fast estimate?
return (d) {
var i = d / _pows((_logs(d)).round());
if (i * base < base - 0.5) i *= base;
return i <= k ? (specifier as String Function(dynamic))(d) : "";
};
}