1 | import {Transform, ingest} from 'vega-dataflow';
|
2 | import {
|
3 | GradientLegend, SymbolLegend,
|
4 | labelFormat, labelFraction, labelValues,
|
5 | scaleFraction, tickCount
|
6 | } from 'vega-scale';
|
7 | import {constant, inherits, isFunction, peek} from 'vega-util';
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | export default function LegendEntries(params) {
|
28 | Transform.call(this, [], params);
|
29 | }
|
30 |
|
31 | var prototype = inherits(LegendEntries, Transform);
|
32 |
|
33 | prototype.transform = function(_, pulse) {
|
34 | if (this.value != null && !_.modified()) {
|
35 | return pulse.StopPropagation;
|
36 | }
|
37 |
|
38 | var locale = pulse.dataflow.locale(),
|
39 | out = pulse.fork(pulse.NO_SOURCE | pulse.NO_FIELDS),
|
40 | items = this.value,
|
41 | type = _.type || SymbolLegend,
|
42 | scale = _.scale,
|
43 | limit = +_.limit,
|
44 | count = tickCount(scale, _.count == null ? 5 : _.count, _.minstep),
|
45 | lskip = !!_.values || type === SymbolLegend,
|
46 | format = _.format || labelFormat(locale, scale, count, type, _.formatSpecifier, _.formatType, lskip),
|
47 | values = _.values || labelValues(scale, count),
|
48 | domain, fraction, size, offset, ellipsis;
|
49 |
|
50 | if (items) out.rem = items;
|
51 |
|
52 | if (type === SymbolLegend) {
|
53 | if (limit && values.length > limit) {
|
54 | pulse.dataflow.warn('Symbol legend count exceeds limit, filtering items.');
|
55 | items = values.slice(0, limit - 1);
|
56 | ellipsis = true;
|
57 | } else {
|
58 | items = values;
|
59 | }
|
60 |
|
61 | if (isFunction(size = _.size)) {
|
62 |
|
63 | if (!_.values && scale(items[0]) === 0) {
|
64 | items = items.slice(1);
|
65 | }
|
66 |
|
67 | offset = items.reduce(function(max, value) {
|
68 | return Math.max(max, size(value, _));
|
69 | }, 0);
|
70 | } else {
|
71 | size = constant(offset = size || 8);
|
72 | }
|
73 |
|
74 | items = items.map(function(value, index) {
|
75 | return ingest({
|
76 | index: index,
|
77 | label: format(value, index, items),
|
78 | value: value,
|
79 | offset: offset,
|
80 | size: size(value, _)
|
81 | });
|
82 | });
|
83 |
|
84 | if (ellipsis) {
|
85 | ellipsis = values[items.length];
|
86 | items.push(ingest({
|
87 | index: items.length,
|
88 | label: `\u2026${values.length-items.length} entries`,
|
89 | value: ellipsis,
|
90 | offset: offset,
|
91 | size: size(ellipsis, _)
|
92 | }));
|
93 | }
|
94 | }
|
95 |
|
96 | else if (type === GradientLegend) {
|
97 | domain = scale.domain(),
|
98 | fraction = scaleFraction(scale, domain[0], peek(domain));
|
99 |
|
100 |
|
101 |
|
102 | if (values.length < 3 && !_.values && domain[0] !== peek(domain)) {
|
103 | values = [domain[0], peek(domain)];
|
104 | }
|
105 |
|
106 | items = values.map(function(value, index) {
|
107 | return ingest({
|
108 | index: index,
|
109 | label: format(value, index, values),
|
110 | value: value,
|
111 | perc: fraction(value)
|
112 | });
|
113 | });
|
114 | }
|
115 |
|
116 | else {
|
117 | size = values.length - 1;
|
118 | fraction = labelFraction(scale);
|
119 |
|
120 | items = values.map(function(value, index) {
|
121 | return ingest({
|
122 | index: index,
|
123 | label: format(value, index, values),
|
124 | value: value,
|
125 | perc: index ? fraction(value) : 0,
|
126 | perc2: index === size ? 1 : fraction(values[index+1])
|
127 | });
|
128 | });
|
129 | }
|
130 |
|
131 | out.source = items;
|
132 | out.add = items;
|
133 | this.value = items;
|
134 |
|
135 | return out;
|
136 | };
|