-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathstacked-labels.js
145 lines (124 loc) · 4.19 KB
/
stacked-labels.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
(function() {
'use strict';
/*
* The stackedLabels are appropriate for use with the stacked shape series.
*
* @name stackedLabels
*/
d4.feature('stackedLabels', function(name) {
// FIXME: We should not need to sniff this out.
var dataInColumns = function(d) {
if (d4.isDefined(d.y0)) {
return true;
}
return d4.isContinuousScale(this.y);
};
var anchorText = function(d) {
return dataInColumns.bind(this)(d) ? 'middle' : 'start';
};
var useDiscretePosition = function(dimension, d) {
var axis = this[dimension];
return axis(d[axis.$key]) + (axis.rangeBand() / 2);
};
var useContinuousPosition = function(dimension, d) {
var axis = this[dimension];
var offset = Math.abs(axis(d.y0) - axis(d.y0 + d.y)) / 2;
// FIXME: Remove this hardcoding.
var padding = 10;
var val;
if (dimension === 'x') {
offset *= -1;
padding *= -1;
}
if (d4.isDefined(d.y0)) {
val = d.y0 + d.y;
return (val <= 0 ? axis(d.y0) : axis(val)) + offset;
} else {
return (d[axis.$key] <= 0 ? axis(0) : axis(d[axis.$key])) - padding;
}
};
return {
accessors: {
classes: 'column-label',
key: d4.functor(d4.defaultKey),
stagger: true,
text: function(d) {
if (d4.isDefined(d.y0)) {
if (d4.isOrdinalScale(this.x)) {
if (Math.abs(this.y(d.y0) - this.y(d.y0 + d.y)) > 20) {
return d[this.valueKey];
}
} else {
if (Math.abs(this.x(d.y0) - this.x(d.y0 + d.y)) > 20) {
return d[this.valueKey];
}
}
} else {
return d[this.valueKey];
}
},
textAnchor: function(d) {
return anchorText.bind(this)(d);
},
x: function(d) {
if (d4.isOrdinalScale(this.x)) {
return useDiscretePosition.bind(this)('x', d);
} else {
return useContinuousPosition.bind(this)('x', d);
}
},
y: function(d) {
if (d4.isOrdinalScale(this.y)) {
return useDiscretePosition.bind(this)('y', d);
} else {
return useContinuousPosition.bind(this)('y', d);
}
}
},
render: function(scope, data, selection) {
var group = d4.appendOnce(selection, 'g.' + name);
var labelGroups = group.selectAll('g')
.data(data, d4.functor(scope.accessors.key).bind(this));
labelGroups.enter().append('g')
.attr('class', function(d, i) {
return 'series' + i + ' ' + this.x.$key;
}.bind(this));
labelGroups.exit().remove();
var text = labelGroups.selectAll('text')
.data(function(d) {
return d.values;
}.bind(this));
text.enter().append('text');
text
.text(d4.functor(scope.accessors.text).bind(this))
.attr('text-anchor', d4.functor(scope.accessors.textAnchor).bind(this))
.attr('class', d4.functor(scope.accessors.classes).bind(this))
.attr('y', d4.functor(scope.accessors.y).bind(this))
.attr('x', d4.functor(scope.accessors.x).bind(this));
if (d4.functor(scope.accessors.stagger).bind(this)()) {
// FIXME: This should be moved into a helper injected using DI.
if (d4.isContinuousScale(this.y)) {
group.selectAll('text').call(d4.helpers.staggerTextVertically, -1);
} else {
group.selectAll('text').call(d4.helpers.staggerTextHorizontally, 1);
}
}
group.selectAll('text').call(function(rows) {
var rect;
d4.each(rows, function(cols) {
d4.each(cols, function(text) {
var txt = d3.select(text);
rect = text.getBoundingClientRect();
if (txt.attr('transform') === null) {
txt.attr('transform', 'translate(0,' + Math.floor(rect.height / 2) + ')');
}
});
});
});
labelGroups.exit().remove();
text.exit().remove();
return text;
}
};
});
}).call(this);