-
Notifications
You must be signed in to change notification settings - Fork 534
/
macd.js
56 lines (44 loc) · 1.73 KB
/
macd.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
'use strict';
module.exports = function(accessor_macd, plot, plotMixin) { // Injected dependencies
return function() { // Closure function
var p = {}; // Container for private, direct access mixed in variables
function macd(g) {
var group = plot.groupSelect(g, plot.dataMapper.array, p.accessor.d);
var histogramSelection = group.selection
.append('g').attr('class', 'difference')
.selectAll('g.difference').data(function(data) { return data; });
histogramSelection.exit().remove();
histogramSelection.enter().append('path').attr('class', 'difference');
group.selection.append('path').attr('class', 'zero');
group.selection.append('path').attr('class', 'macd');
group.selection.append('path').attr('class', 'signal');
macd.refresh(g);
}
macd.refresh = function(g) {
refresh(g, p.accessor, p.xScale, p.yScale, plot);
};
// Mixin 'superclass' methods and variables
plotMixin(macd, p, accessor_macd());
return macd;
};
};
function refresh(g, accessor, x, y, plot) {
g.selectAll('path.difference').attr('d', differencePath(accessor, x, y));
g.selectAll('path.zero').attr('d', plot.horizontalPathLine(accessor.d, x, accessor.z, y));
g.selectAll('path.macd').attr('d', plot.pathLine(accessor.d, x, accessor.m, y));
g.selectAll('path.signal').attr('d', plot.pathLine(accessor.d, x, accessor.s, y));
}
function differencePath(accessor, x, y) {
return function(d) {
var zero = y(0),
height = y(accessor.dif(d)) - zero,
rangeBand = x.band(),
xValue = x(accessor.d(d)) - rangeBand/2;
return [
'M', xValue, zero,
'l', 0, height,
'l', rangeBand, 0,
'l', 0, -height
].join(' ');
};
}