-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
labels.js
117 lines (96 loc) · 2.67 KB
/
labels.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {Symbols, Discrete} from './legend-types';
import {tickFormat, tickValues} from './ticks';
import {peek} from 'vega-util';
import {
Quantile,
Quantize,
Threshold,
tickFormat as spanFormat,
Time,
UTC
} from 'vega-scale';
const symbols = {
[Quantile]: 'quantiles',
[Quantize]: 'thresholds',
[Threshold]: 'domain'
};
const formats = {
[Quantile]: 'quantiles',
[Quantize]: 'domain'
};
export function labelValues(scale, count) {
return scale.bins ? binValues(scale.bins)
: symbols[scale.type] ? thresholdValues(scale[symbols[scale.type]]())
: tickValues(scale, count);
}
export function thresholdFormat(scale, specifier) {
var _ = scale[formats[scale.type]](),
n = _.length,
d = n > 1 ? _[1] - _[0] : _[0], i;
for (i=1; i<n; ++i) {
d = Math.min(d, _[i] - _[i-1]);
}
// 3 ticks times 10 for increased resolution
return spanFormat(0, d, 3 * 10, specifier);
}
function thresholdValues(thresholds) {
const values = [-Infinity].concat(thresholds);
values.max = +Infinity;
return values;
}
function binValues(bins) {
const values = bins.slice(0, -1);
values.max = peek(bins);
return values;
}
function isDiscreteRange(scale) {
return symbols[scale.type] || scale.bins;
}
export function labelFormat(scale, count, type, specifier, formatType, noSkip) {
const format = formats[scale.type] && formatType !== Time && formatType !== UTC
? thresholdFormat(scale, specifier)
: tickFormat(scale, count, specifier, formatType, noSkip);
return type === Symbols && isDiscreteRange(scale) ? formatRange(format)
: type === Discrete ? formatDiscrete(format)
: formatPoint(format);
}
function formatRange(format) {
return function(value, index, array) {
var limit = get(array[index + 1], get(array.max, +Infinity)),
lo = formatValue(value, format),
hi = formatValue(limit, format);
return lo && hi ? lo + ' \u2013 ' + hi : hi ? '< ' + hi : '\u2265 ' + lo;
};
}
function get(value, dflt) {
return value != null ? value : dflt;
}
function formatDiscrete(format) {
return function(value, index) {
return index ? format(value) : null;
}
}
function formatPoint(format) {
return function(value) {
return format(value);
};
}
function formatValue(value, format) {
return Number.isFinite(value) ? format(value) : null;
}
export function labelFraction(scale) {
var domain = scale.domain(),
count = domain.length - 1,
lo = +domain[0],
hi = +peek(domain),
span = hi - lo;
if (scale.type === Threshold) {
var adjust = count ? span / count : 0.1;
lo -= adjust;
hi += adjust;
span = hi - lo;
}
return function(value) {
return (value - lo) / span;
};
}