-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcolumn-labels.js
72 lines (66 loc) · 2.19 KB
/
column-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
(function() {
'use strict';
/*
* The columnLabels feature is used to affix data labels to column series.
*
* @name columnLabels
*/
d4.feature('columnLabels', function(name) {
// FIXME: Remove this hardcoded variable or expose it as a setting.
var padding = 5;
var anchorText = function() {
if (d4.isContinuousScale(this.y)) {
return 'middle';
} else {
return 'start';
}
};
return {
accessors: {
key: d4.functor(d4.defaultKey),
x: function(xScaleId, d) {
var axis = this[xScaleId];
if (d4.isOrdinalScale(axis)) {
return axis(d[axis.$key]) + (axis.rangeBand() / 2);
} else {
var width = Math.abs(axis(d[axis.$key]) - axis(0));
return axis(d[axis.$key]) - width / 2;
}
},
y: function(yScaleId, d) {
var axis = this[yScaleId];
if (d4.isOrdinalScale(axis)) {
return axis(d[axis.$key]) + (axis.rangeBand() / 2) + padding;
} else {
var height = Math.abs(axis(d[axis.$key]) - axis(0));
return (d[axis.$key] < 0 ? axis(d[axis.$key]) - height : axis(d[axis.$key])) - padding;
}
},
text: function(d) {
return d[this.valueKey];
},
xScaleId: function() {
return 'x';
},
yScaleId: function() {
return 'y';
}
},
render: function(scope, data, selection) {
var xScaleId = d4.functor(scope.accessors.xScaleId)();
var yScaleId = d4.functor(scope.accessors.yScaleId)();
var group = d4.appendOnce(selection, 'g.' + name);
var label = group.selectAll('text')
.data(data, d4.functor(scope.accessors.key).bind(this));
label.enter().append('text');
label.exit().remove();
label.attr('class', 'column-label ' + name)
.text(d4.functor(scope.accessors.text).bind(this))
.attr('text-anchor', anchorText.bind(this))
.attr('x', d4.functor(scope.accessors.x).bind(this, xScaleId))
.attr('y', d4.functor(scope.accessors.y).bind(this, yScaleId));
return label;
}
};
});
}).call(this);