-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcommon.js
More file actions
78 lines (63 loc) · 2.05 KB
/
common.js
File metadata and controls
78 lines (63 loc) · 2.05 KB
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
// implements deferred element constructors that let us define elements
// and later draw them in the context of a specific selection and parent gauge
import * as d3 from 'd3';
import {stylable, interactable, transformable, appendable} from "./mixin.js";
export function d3sel(sel) {
return (typeof sel === 'string') ? d3.select(sel) : sel;
}
export function element(elt, attrs_) {
var attrs = attrs_ || {};
function element(sel, g) {
var _ = d3sel(sel).append(elt);
Object.entries(attrs).forEach(([k, v]) => _.attr(k, v));
element.stylable(_);
element.appendable(_, g);
element.interactable(_, g);
}
element.attr = function(k, _) {
return (typeof _ !== 'undefined') ? (attrs[k] = _, element): attrs[k];
}
return interactable(stylable(appendable(element)));
}
export function put() {
function put(sel, g) {
var _ = d3sel(sel).append('g');
put.transformable(_);
put.stylable(_);
put.appendable(_, g);
put.interactable(_, g);
}
interactable(stylable(transformable(appendable(put))));
return put;
}
export function snapScale() {
var step = 1,
start = 0,
strength = 5;
function snapScale(v) {
let v0 = Math.round((v - start)/step)*step + start,
w = step/2,
dv = d3.scalePow().domain([-w,w]).range([-w,w]).exponent(strength)(v - v0);
return v0 + dv;
}
snapScale.start = function(_) {
return arguments.length ? (start = _, snapScale): start;
}
snapScale.step = function(_) {
return arguments.length ? (step = _, snapScale): step;
}
snapScale.strength = function(_) {
return arguments.length ? (strength = _, snapScale): strength;
}
return snapScale;
}
export function flatten(o, ks) {
ks = ks || [];
return [].concat(
...Object.entries(o).map(([k, v]) => {
const kks = ks.concat([k]);
return (v !== null && typeof(v) === 'object')
? flatten(v, kks) : [[kks, v]];
})
);
}