1 | import {partition} from './util/util';
|
2 | import {Transform, stableCompare} from 'vega-dataflow';
|
3 | import {dotbin} from 'vega-statistics';
|
4 | import {extent, identity, inherits, span} from 'vega-util';
|
5 |
|
6 | const Output = 'bin';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | export default function DotBin(params) {
|
22 | Transform.call(this, null, params);
|
23 | }
|
24 |
|
25 | DotBin.Definition = {
|
26 | 'type': 'DotBin',
|
27 | 'metadata': {'modifies': true},
|
28 | 'params': [
|
29 | { 'name': 'field', 'type': 'field', 'required': true },
|
30 | { 'name': 'groupby', 'type': 'field', 'array': true },
|
31 | { 'name': 'step', 'type': 'number' },
|
32 | { 'name': 'smooth', 'type': 'boolean', 'default': false },
|
33 | { 'name': 'as', 'type': 'string', 'default': Output }
|
34 | ]
|
35 | };
|
36 |
|
37 | const autostep = (data, field) => span(extent(data, field)) / 30;
|
38 |
|
39 | inherits(DotBin, Transform, {
|
40 | transform(_, pulse) {
|
41 | if (this.value && !(_.modified() || pulse.changed())) {
|
42 | return pulse;
|
43 | }
|
44 |
|
45 | const source = pulse.materialize(pulse.SOURCE).source,
|
46 | groups = partition(pulse.source, _.groupby, identity),
|
47 | smooth = _.smooth || false,
|
48 | field = _.field,
|
49 | step = _.step || autostep(source, field),
|
50 | sort = stableCompare((a, b) => field(a) - field(b)),
|
51 | as = _.as || Output,
|
52 | n = groups.length;
|
53 |
|
54 |
|
55 | let min = Infinity, max = -Infinity, i = 0, j;
|
56 | for (; i<n; ++i) {
|
57 | const g = groups[i].sort(sort);
|
58 | j = -1;
|
59 | for (const v of dotbin(g, step, smooth, field)) {
|
60 | if (v < min) min = v;
|
61 | if (v > max) max = v;
|
62 | g[++j][as] = v;
|
63 | }
|
64 | }
|
65 |
|
66 | this.value = {
|
67 | start: min,
|
68 | stop: max,
|
69 | step: step
|
70 | };
|
71 | return pulse.reflow(true).modifies(as);
|
72 | }
|
73 | });
|