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 prototype = inherits(DotBin, Transform);
|
38 |
|
39 | prototype.transform = function(_, pulse) {
|
40 | if (this.value && !(_.modified() || pulse.changed())) {
|
41 | return pulse;
|
42 | }
|
43 |
|
44 | const source = pulse.materialize(pulse.SOURCE).source,
|
45 | groups = partition(pulse.source, _.groupby, identity),
|
46 | smooth = _.smooth || false,
|
47 | field = _.field,
|
48 | step = _.step || autostep(source, field),
|
49 | sort = stableCompare((a, b) => field(a) - field(b)),
|
50 | as = _.as || Output,
|
51 | n = groups.length;
|
52 |
|
53 |
|
54 | let min = Infinity, max = -Infinity, i = 0, j;
|
55 | for (; i<n; ++i) {
|
56 | const g = groups[i].sort(sort);
|
57 | j = -1;
|
58 | for (const v of dotbin(g, step, smooth, field)) {
|
59 | if (v < min) min = v;
|
60 | if (v > max) max = v;
|
61 | g[++j][as] = v;
|
62 | }
|
63 | }
|
64 |
|
65 | this.value = {
|
66 | start: min,
|
67 | stop: max,
|
68 | step: step
|
69 | };
|
70 | return pulse.reflow(true).modifies(as);
|
71 | };
|
72 |
|
73 | function autostep(data, field) {
|
74 | return span(extent(data, field)) / 30;
|
75 | }
|