-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.js
51 lines (41 loc) · 1.23 KB
/
number.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
import { Reducer } from 'redugator';
import cf from './cf';
import { format as d3_format } from 'd3-format';
import { select } from 'd3-selection';
import { interpolate } from 'd3-interpolate';
const format = d3_format(',.0f');
const universal_dim = cf.dimension(_ => 'all');
export default class NumberContainer {
constructor({anchor, description, reducer, accessor}) {
this.el = select(anchor);
this.group = Reducer.reduceGroup(reducer, universal_dim.group());
this.description = description;
this.accessor = accessor;
this._value = 0;
}
render() {
if (!this.group.all()[0]) return;
let _value = this.accessor(this.group.all()[0]);
let _sel = this.el.selectAll('.number').data([_value]);
_sel.enter()
.append('span')
.attr('class', 'number')
.text(this._value);
_sel.enter()
.append('span')
.attr('class', 'description')
.text(this.description);
_sel.exit().remove();
this.el.selectAll('.number').transition().duration(375)
.tween('text', d => {
const i = interpolate(this._value, d);
this._value = d;
return function(t) {
this.textContent = format(i(t));
};
});
}
redraw() {
this.render();
}
}