-
Notifications
You must be signed in to change notification settings - Fork 46
/
trend-line.js
70 lines (60 loc) · 2.04 KB
/
trend-line.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
(function() {
'use strict';
/*
* A trendline allows you to associate a line with a numerical value.
*
* @name trendLine
*/
d4.feature('trendLine', function(name) {
return {
accessors: {
tipSize: 6,
text: function(d) {
return d[this.valueKey];
},
textX: function() {
return this.x(this.width);
},
textY: function() {
return this.x(this.height);
},
x1: function() {
return this.x(this.x.$key);
},
x2: function() {
return this.x(this.width);
},
y1: function() {
return this.y(this.y.$key);
},
y2: function() {
return this.y(this.height);
},
},
render: function(scope, data, selection) {
var defs = this.container.select('defs');
d4.appendOnce(defs, 'marker#' + name + '-start')
.attr('viewBox', '0 0 10 10')
.attr('refX', 10)
.attr('refY', 5)
.attr('markerWidth', d4.functor(scope.accessors.tipSize).bind(this)())
.attr('markerHeight', d4.functor(scope.accessors.tipSize).bind(this))
.attr('orient', 'auto')
.append('path')
.attr('d', 'M 0 0 L 10 5 L 0 10 z');
d4.appendOnce(selection, 'g.' + name);
var trendLine = d4.appendOnce(this.container.select('.' + name), 'line.line')
.attr('x1', d4.functor(scope.accessors.x1).bind(this))
.attr('x2', d4.functor(scope.accessors.x2).bind(this))
.attr('y1', d4.functor(scope.accessors.y1).bind(this))
.attr('y2', d4.functor(scope.accessors.y2).bind(this))
.attr('marker-end', 'url(#' + name + '-start)');
d4.appendOnce(this.container.select('.' + name), 'text.trendLine-label')
.text(d4.functor(scope.accessors.text).bind(this))
.attr('x', d4.functor(scope.accessors.textX).bind(this))
.attr('y', d4.functor(scope.accessors.textY).bind(this));
return trendLine;
}
};
});
}).call(this);