-
Notifications
You must be signed in to change notification settings - Fork 46
/
grouped-column-series.js
149 lines (125 loc) · 4.3 KB
/
grouped-column-series.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
146
147
148
149
(function() {
'use strict';
/*
* This feature is specifically designed to use with the groupedColumn and groupedRow charts.
*
* @name groupedColumnSeries
*/
d4.feature('groupedColumnSeries', function(name) {
var sign = function(val) {
return (val > 0) ? 'positive' : 'negative';
};
var useDiscretePosition = function(d) {
return this.groups(d[this.groups.$key]);
};
var useDiscreteGroupPosition = function(d) {
var dimension = this.groups.$dimension;
var axis = this[dimension];
var pos = axis(d.values[0][axis.$key]);
var translate;
if (dimension === 'x') {
translate = [pos, 0];
} else if (dimension === 'y') {
translate = [0, pos];
}
return 'translate(' + translate + ')';
};
var useDiscreteSize = function() {
return this.groups.rangeBand();
};
var useContinuousSize = function(dimension, d) {
var axis = this[dimension];
var domainMin = axis.domain()[0];
var axisMin = (domainMin < 0) ? 0 : domainMin;
return Math.abs(axis(d[axis.$key]) - axis(axisMin));
};
var useContinuousPosition = function(dimension, d) {
var axis = this[dimension],
val;
if (dimension === 'y') {
return d[axis.$key] < 0 ? axis(0) : axis(d[axis.$key]);
} else {
val = d[axis.$key] - Math.max(0, d[axis.$key]);
return axis(val);
}
};
return {
accessors: {
classes: function(d, i) {
return 'bar fill item' + i + ' ' + sign(d[this.valueKey]) + ' ' + d[this.valueKey];
},
height: function(yScaleId, d) {
if (d4.isOrdinalScale(this.y)) {
return useDiscreteSize.bind(this)(yScaleId);
} else {
return useContinuousSize.bind(this)(yScaleId, d);
}
},
key: d4.functor(d4.defaultKey),
rx: 0,
ry: 0,
width: function(xScaleId, d) {
if (d4.isOrdinalScale(this.x)) {
return useDiscreteSize.bind(this)();
} else {
return useContinuousSize.bind(this)(xScaleId, d);
}
},
groupPositions: function(d, i) {
return useDiscreteGroupPosition.bind(this)(d, i);
},
x: function(d, i) {
if (d4.isOrdinalScale(this.x)) {
return useDiscretePosition.bind(this)(d);
} else {
return useContinuousPosition.bind(this)('x', d, i);
}
},
y: function(d, i) {
if (d4.isOrdinalScale(this.y)) {
return useDiscretePosition.bind(this)(d);
} else {
return useContinuousPosition.bind(this)('y', d, i);
}
},
xScaleId: function() {
return 'x';
},
yScaleId: function() {
return 'y';
}
},
render: function(scope, data, selection) {
if (data.length > 0) {
this.groupsOf = this.groupsOf || data[0].values.length;
}
var xScaleId = d4.functor(scope.accessors.xScaleId)();
var yScaleId = d4.functor(scope.accessors.yScaleId)();
var group = d4.appendOnce(selection, 'g.' + name);
var columnGroups = group.selectAll('g')
.data(data, d4.functor(scope.accessors.key).bind(this));
columnGroups.enter().append('g');
columnGroups.attr('class', function(d, i) {
return 'series' + i + ' ' + this.x.$key;
}.bind(this))
.attr('transform', d4.functor(scope.accessors.groupPositions).bind(this));
var rect = columnGroups.selectAll('rect')
.data(function(d) {
return d.values;
}.bind(this));
rect.enter().append('rect');
rect
.attr('class', d4.functor(scope.accessors.classes).bind(this))
.attr('x', d4.functor(scope.accessors.x).bind(this))
.attr('y', d4.functor(scope.accessors.y).bind(this))
.attr('ry', d4.functor(scope.accessors.ry).bind(this))
.attr('rx', d4.functor(scope.accessors.rx).bind(this))
.attr('width', d4.functor(scope.accessors.width).bind(this, xScaleId))
.attr('height', d4.functor(scope.accessors.height).bind(this, yScaleId));
rect.exit().remove();
columnGroups.exit().remove();
return rect;
}
};
});
}).call(this);