-
Notifications
You must be signed in to change notification settings - Fork 0
/
g.multistack.bar.js
191 lines (181 loc) · 7.06 KB
/
g.multistack.bar.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*!
* g.multistack.bar - Multi StackBar Chart, based on Raphaël
*
* Copyright (c) 2010 Mirek Mencel (http://mirumee.com)
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
*/
Raphael.fn.g.multistackbar = function (x, y, width, height, values, opts) {
opts = opts || {};
var type = {round: "round", sharp: "sharp", soft: "soft"}[opts.type] || "square",
gutter = parseFloat(opts.gutter || "20%"),
chart = this.set(),
bars = this.set(),
covers = this.set(),
covers2 = this.set(),
covers3 = this.set(),
total = 0,
paper = this,
multi = values[0].length, // Total number of stacks in one group
stacklen = values[0][0].length, // Total number of stack's components
len = values.length,
colors = opts.colors || this.g.colors,
grouplabels = opts.grouplabels,
itemlabels = opts.itemlabels || [],
stackitemlabels = opts.stackitemlabels || [],
stacklabelcolors = opts.stacklabelcolors,
len = values.length;
// Who's the most
var sum = 0;
for (var i = values.length; i--;) {
var group = values[i];
for (var j = group.length; j--;) {
var stack = group[j];
for (var k = stack.length; k--; ) {
sum += stack[k];
}
total = Math.max(total, sum);
sum = 0;
stacklen = Math.max(stacklen, stack.length);
}
multi = Math.max(multi, group.length);
}
if(opts.total>0) total = opts.total;
// Fill the gaps
for (var i = values.length; i--;) {
var group = values[i];
if (group.length < multi) {
for (var ff0 = multi - group.length; ff0--;) { // Fill up missing elements
group.push([]);
}
}
for (var j = group.length; j--;) {
var stack = group[j];
bars.push(this.set());
if (stack.length < stacklen) {
for (var ff1 = stacklen - stack.length; ff1 > 0; ff1--) {
stack.push(0);
}
}
}
}
// Calculate the gutters (percentage of the bar and group width accordingly)
var axiswidth = 30, // TODO: fixed y-axis width
innerwidth = width - axiswidth,
innerheight = height - 50,
groupwidth = Math.round(innerwidth / (len + (len-1)*(gutter/100))),
grouphgutter = Math.round(groupwidth * gutter / 100),
barwidth = Math.round(groupwidth / (multi + (multi-1)*(gutter/100))),
barhgutter = Math.round(barwidth * gutter / 100),
barvgutter = opts.vgutter == null ? 25 : opts.vgutter,
stack = [],
X = x + axiswidth,
Y = (total != 0 ? (innerheight - 2 * barvgutter) / total: 0),
bottomy = y + innerheight - barvgutter + barhgutter;
var sum = 0,
axis = this.set(),
miny = 0,
maxy = total;
// y-axis
+opts.axis && axis.push(this.g.axis(x + 15, y + innerheight - barvgutter, innerheight - 2 * barvgutter, miny, maxy, opts.axisystep || Math.floor((innerheight) / 30), 1));
for (var i = 0; i < values.length; i++) {
var group = values[i];
var startX = X;
for (var j = 0; j < group.length; j++, sum = 0) {
var stackd = group[j],
stackSum = 0;
for (var k = 0; k < stackd.length; k++) {
var h = Math.round(stackd[k] * Y),
top = y + innerheight - barvgutter - h,
barx = Math.round(X + barwidth / 2),
bary = sum + top + h,
bar = this.g.finger(barx, bary,
barwidth, h,
true, type).attr({stroke: "none", fill: colors[k]});
bars[j].push(bar);
bar.y = bary;
bar.x = barx;
bar.w = barwidth;
bar.h = h;
bar.value = stackd[k];
bar.label = itemlabels[i] && itemlabels[i][j] && itemlabels[i][j][k] || stackitemlabels[k] || "";
stack.push(bar);
// Rollover
var cover;
covers.push(cover = this.rect(bar.x - bar.w / 2, sum + top, barwidth, bar.h).attr(this.g.shim));
cover.bar = bar;
cover.value = bar.value;
stackSum += stackd[k];
sum -= bar.h;
}
// Sort it
for (var s = stack.length; s--;) {
stack[s].toFront();
}
// 'Total' rollover
var cvr;
covers2.push(cvr = this.rect(bar.x - bar.w / 2, y, barwidth, innerheight).attr(this.g.shim));
cvr.bar = {x: bar.x, y: bar.y - bar.h, w: barwidth, h: innerheight, value: stackSum};
// Optional stack color label
var boxheight = barwidth - barwidth*0.5,
boxwidth = Math.min(barwidth * 3/4, 8),
boxmargin = (barwidth - boxwidth)/2,
box, boxx, boxy;
if (stacklabelcolors) {
//axis.push(box = this.rect(boxx = bar.x - bar.w / 2 + boxmargin, boxy = y + height - barwidth, boxwidth, boxheight).attr({stroke: "none", fill: stacklabelcolors[j] || '#BBB'}));
axis.push(box = this.g.drop(boxx = bar.x, boxy = bottomy, "", boxwidth, -90).attr({stroke: "none", fill: stacklabelcolors[j] || '#BBB'}));
box.tip = {x: boxx, y: boxy};
covers3.push(box);
}
if (j < (group.length - 1)) {
X += barhgutter;
}
X += barwidth;
}
if (grouplabels) {
var endX;
axis.push(this.g.finger(startX, bottomy + boxwidth*2 , endX = X-startX, 1, false, type).attr({stroke: "none", fill: '#BBB'}));
axis.push(this.g.flag(startX, bottomy + boxwidth*2, grouplabels[i] || "", -60));
}
X += grouphgutter;
}
covers3.toFront();
axis.toFront();
covers2.toFront();
covers.toFront();
covers3.mouseover(function() {
this.flag = paper.g.popup(this.tip.x, this.tip.y, "Test").insertBefore(this);
//this.animate({scale: 2}, 300);
}).mouseout(function() {
//this.flag.animate({opacity: 0}, 300, function () {this.remove();});
});
chart.hover = function (fin, fout) {
//covers2.hide();
covers.show();
covers.mouseover(fin).mouseout(fout);
return this;
};
chart.hoverColumn = function (fin, fout) {
//covers.hide();
//covers2.show();
fout = fout || function () {};
covers2.mouseover(fin).mouseout(fout);
return this;
};
chart.click = function (f) {
//covers2.hide();
covers.show();
covers.click(f);
return this;
};
chart.clickColumn = function (f) {
covers.hide();
covers2.show();
covers2.click(f);
return this;
};
chart.push(bars, covers, covers2, axis);
chart.bars = bars;
chart.covers = covers;
chart.axis = axis;
return chart;
};