-
Notifications
You must be signed in to change notification settings - Fork 534
/
plot.js
130 lines (112 loc) · 4.3 KB
/
plot.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
module.exports = function(d3_svg_line, d3_select) {
function dataSelection(g, dataMapper, accessor_date) {
var selection = g.selectAll('g.data').data(dataMapper, accessor_date);
selection.exit().remove();
return selection;
}
function dataEntry(dataSelection) {
return dataSelection.enter().append('g').attr('class', 'data');
}
return {
dataMapper: {
unity: function(d) { return d; },
array: function(d) { return [d]; }
},
dataSelection: dataSelection,
dataEntry: dataEntry,
groupSelect: function(g, dataMapper, accessor_date) {
var selection = dataSelection(g, dataMapper, accessor_date),
entry = dataEntry(selection);
return {
selection: selection,
entry: entry
};
},
classedUpDown: function(accessor) {
return {
up: function(d) { return accessor.o(d) < accessor.c(d); },
down: function(d) { return accessor.o(d) > accessor.c(d); }
};
},
horizontalPathLine: function(accessor_date, x, accessor_value, y) {
return function(d) {
var firstDatum = d[0],
lastDatum = d[d.length-1];
return [
'M', x(accessor_date(firstDatum)), y(accessor_value(firstDatum)),
'L', x(accessor_date(lastDatum)), y(accessor_value(lastDatum))
].join(' ');
};
},
pathLine: function(accessor_date, x, accessor_value, y, offset) {
return d3_svg_line().interpolate('monotone')
.defined(function(d) { return accessor_value(d) !== null; })
.x(function(d) { return x(accessor_date(d), offset === undefined ? offset : offset(d)); } )
.y(function(d) { return y(accessor_value(d)); } );
},
interaction: {
mousedispatch: function(dispatch) {
return function(selection) {
return selection.on('mouseenter', function(d) {
d3_select(this.parentNode).classed('mouseover', true);
dispatch.mouseenter(d);
})
.on('mouseleave', function(d) {
var parentElement = d3_select(this.parentNode);
if(!parentElement.classed('dragging')) {
parentElement.classed('mouseover', false);
dispatch.mouseout(d);
}
})
.on('mousemove', function(d) { dispatch.mousemove(d); });
};
},
dragStartEndDispatch: function(drag, dispatch) {
return drag.on('dragstart', function(d) {
d3_select(this.parentNode.parentNode).classed('dragging', true);
dispatch.dragstart(d);
})
.on('dragend', function(d) {
d3_select(this.parentNode.parentNode).classed('dragging', false);
dispatch.dragend(d);
});
}
},
annotation: {
append: function(selection, annotations, clazz, accessor, scale) {
// Use this to either scale the data or initialise to null if accessor and scales are not provided
var argumentLength = arguments.length;
var annotationSelection = selection.append('g').attr('class', 'axisannotation ' + clazz)
.selectAll('g').data(function(d) {
// Transform the data to values for each annotation, only if we have accessor and scale
var y = argumentLength > 3 ? scale(accessor(d)) : null;
return annotations.map(function(annotation) {
var value = argumentLength > 3 ? annotation.axis().scale().invert(y) : null;
// Only ever 1 data point per annotation
return [{ value: value }];
});
}
);
annotationSelection.enter().append('g').attr('class', function(d, i) { return i; })
.each(function(d, i) {
// Store some meta for lookup later, could use class instance, but this 'should' be more reliable
this.__annotation__ = i;
annotations[i](d3_select(this));
});
},
update: function(annotations, value) {
return function(d) {
var annotation = annotations[this.__annotation__];
// As in append, should only ever be 1 in the array
d[0].value = annotation.axis().scale().invert(value);
};
},
refresh: function(annotations) {
return function() {
annotations[this.__annotation__].refresh(d3_select(this));
};
}
}
};
};