This repository is currently being migrated. It's locked while the migration is in progress.
forked from DmitryBaranovskiy/g.raphael
-
Notifications
You must be signed in to change notification settings - Fork 1
/
g.pie.js
300 lines (261 loc) · 11.4 KB
/
g.pie.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*!
* g.Raphael 0.51 - Charting library, based on Raphaël
*
* Copyright (c) 2009-2012 Dmitry Baranovskiy (http://g.raphaeljs.com)
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
*/
/*
* piechart method on paper
*/
/*\
* Paper.piechart
[ method ]
**
* Creates a pie chart
**
> Parameters
**
- cx (number) x coordinate of the chart
- cy (number) y coordinate of the chart
- r (integer) radius of the chart
- values (array) values used to plot
- opts (object) options for the chart
o {
o minPercent (number) minimal percent threshold which will have a slice rendered. Sliced corresponding to data points below this threshold will be collapsed into 1 additional slice. [default `1`]
o maxSlices (number) a threshold for how many slices should be rendered before collapsing all remaining slices into 1 additional slice (to focus on most important data points). [default `100`]
o stroke (string) color of the circle stroke in HTML color format [default `"#FFF"`]
o strokewidth (integer) width of the chart stroke [default `1`]
o init (boolean) whether or not to show animation when the chart is ready [default `false`]
o colors (array) colors be used to plot the chart
o href (array) urls to to set up clicks on chart slices
o legend (array) array containing strings that will be used in a legend. Other label options work if legend is defined.
o legendcolor (string) color of text in legend [default `"#000"`]
o legendothers (string) text that will be used in legend to describe options that are collapsed into 1 slice, because they are too small to render [default `"Others"`]
o legendmark (string) symbol used as a bullet point in legend that has the same colour as the chart slice [default `"circle"`]
o legendpos (string) position of the legend on the chart [default `"east"`]. Other options are `"north"`, `"south"`, `"west"`
o preserveValues (boolean) whether the values should be left as they are provided, rather than the default behaviour of being sorted and small values collapsed [default `false`]
o }
**
= (object) path element of the popup
> Usage
| r.piechart(cx, cy, r, values, opts)
\*/
(function () {
function Piechart(paper, cx, cy, r, values, opts) {
opts = opts || {};
var chartinst = this,
sectors = [],
covers = paper.set(),
chart = paper.set(),
series = paper.set(),
order = [],
len = values.length,
angle = 0,
total = 0,
others = 0,
cut = opts.maxSlices || 100,
minPercent = parseFloat(opts.minPercent) || 1,
defcut = Boolean( minPercent );
function sector(cx, cy, r, startAngle, endAngle, fill) {
var rad = Math.PI / 180,
x1 = cx + r * Math.cos(-startAngle * rad),
x2 = cx + r * Math.cos(-endAngle * rad),
xm = cx + r / 2 * Math.cos(-(startAngle + (endAngle - startAngle) / 2) * rad),
y1 = cy + r * Math.sin(-startAngle * rad),
y2 = cy + r * Math.sin(-endAngle * rad),
ym = cy + r / 2 * Math.sin(-(startAngle + (endAngle - startAngle) / 2) * rad),
res = [
"M", cx, cy,
"L", x1, y1,
"A", r, r, 0, +(Math.abs(endAngle - startAngle) > 180), 1, x2, y2,
"z"
];
res.middle = { x: xm, y: ym };
return res;
}
chart.covers = covers;
if (len == 1) {
series.push(paper.circle(cx, cy, r).attr({ fill: opts.colors && opts.colors[0] || chartinst.colors[0], stroke: opts.stroke || "#fff", "stroke-width": opts.strokewidth == null ? 1 : opts.strokewidth }));
covers.push(paper.circle(cx, cy, r).attr(chartinst.shim));
total = values[0];
values[0] = { value: values[0], order: 0, valueOf: function () { return this.value; } };
opts.href && opts.href[0] && covers[0].attr({ href: opts.href[0] });
series[0].middle = {x: cx, y: cy};
series[0].mangle = 180;
} else {
for (var i = 0; i < len; i++) {
total += values[i];
values[i] = { value: values[i], order: i, valueOf: function () { return this.value; } };
}
if (!opts.preserveValues) {
//values are sorted numerically
values.sort(function (a, b) {
return b.value - a.value;
});
for (i = 0; i < len; i++) {
if (defcut && values[i] * 100 / total < minPercent) {
cut = i;
defcut = false;
}
if (i > cut) {
defcut = false;
values[cut].value += values[i];
values[cut].others = true;
others = values[cut].value;
}
}
}
len = Math.min(cut + 1, values.length);
others && values.splice(len) && (values[cut].others = true);
for (i = 0; i < len; i++) {
var mangle = angle - 360 * values[i] / total / 2;
if (!i) {
angle = 90 - mangle;
mangle = angle - 360 * values[i] / total / 2;
}
if (opts.init) {
var ipath = sector(cx, cy, 1, angle, angle - 360 * values[i] / total).join(",");
}
var path = sector(cx, cy, r, angle, angle -= 360 * values[i] / total);
var j = (opts.matchColors && opts.matchColors == true) ? values[i].order : i;
var p = paper.path(opts.init ? ipath : path).attr({ fill: opts.colors && opts.colors[j] || chartinst.colors[j] || "#666", stroke: opts.stroke || "#fff", "stroke-width": (opts.strokewidth == null ? 1 : opts.strokewidth), "stroke-linejoin": "round" });
p.value = values[i];
p.middle = path.middle;
p.mangle = mangle;
sectors.push(p);
series.push(p);
opts.init && p.animate({ path: path.join(",") }, (+opts.init - 1) || 1000, ">");
}
for (i = 0; i < len; i++) {
p = paper.path(sectors[i].attr("path")).attr(chartinst.shim);
opts.href && opts.href[i] && p.attr({ href: opts.href[i] });
p.attr = function () {};
covers.push(p);
series.push(p);
}
}
chart.hover = function (fin, fout) {
fout = fout || function () {};
var that = this;
for (var i = 0; i < len; i++) {
(function (sector, cover, j) {
var o = {
sector: sector,
cover: cover,
cx: cx,
cy: cy,
mx: sector.middle.x,
my: sector.middle.y,
mangle: sector.mangle,
r: r,
value: values[j],
total: total,
label: that.labels && that.labels[j]
};
cover.mouseover(function () {
fin.call(o);
}).mouseout(function () {
fout.call(o);
});
})(series[i], covers[i], i);
}
return this;
};
// x: where label could be put
// y: where label could be put
// value: value to show
// total: total number to count %
chart.each = function (f) {
var that = this;
for (var i = 0; i < len; i++) {
(function (sector, cover, j) {
var o = {
sector: sector,
cover: cover,
cx: cx,
cy: cy,
x: sector.middle.x,
y: sector.middle.y,
mangle: sector.mangle,
r: r,
value: values[j],
total: total,
label: that.labels && that.labels[j]
};
f.call(o);
})(series[i], covers[i], i);
}
return this;
};
chart.click = function (f) {
var that = this;
for (var i = 0; i < len; i++) {
(function (sector, cover, j) {
var o = {
sector: sector,
cover: cover,
cx: cx,
cy: cy,
mx: sector.middle.x,
my: sector.middle.y,
mangle: sector.mangle,
r: r,
value: values[j],
total: total,
label: that.labels && that.labels[j]
};
cover.click(function () { f.call(o); });
})(series[i], covers[i], i);
}
return this;
};
chart.inject = function (element) {
element.insertBefore(covers[0]);
};
var legend = function (labels, otherslabel, mark, dir) {
var x = cx + r + r / 5,
y = cy,
h = y + 10;
labels = labels || [];
dir = (dir && dir.toLowerCase && dir.toLowerCase()) || "east";
mark = paper[mark && mark.toLowerCase()] || "circle";
chart.labels = paper.set();
for (var i = 0; i < len; i++) {
var clr = series[i].attr("fill"),
j = values[i].order,
txt;
values[i].others && (labels[j] = otherslabel || "Others");
labels[j] = chartinst.labelise(labels[j], values[i], total);
chart.labels.push(paper.set());
chart.labels[i].push(paper[mark](x + 5, h, 5).attr({ fill: clr, stroke: "none" }));
chart.labels[i].push(txt = paper.text(x + 20, h, labels[j] || values[j]).attr(chartinst.txtattr).attr({ fill: opts.legendcolor || "#000", "text-anchor": "start"}));
covers[i].label = chart.labels[i];
h += txt.getBBox().height * 1.2;
}
var bb = chart.labels.getBBox(),
tr = {
east: [0, -bb.height / 2],
west: [-bb.width - 2 * r - 20, -bb.height / 2],
north: [-r - bb.width / 2, -r - bb.height - 10],
south: [-r - bb.width / 2, r + 10]
}[dir];
chart.labels.translate.apply(chart.labels, tr);
chart.push(chart.labels);
};
if (opts.legend) {
legend(opts.legend, opts.legendothers, opts.legendmark, opts.legendpos);
}
chart.push(series, covers);
chart.series = series;
chart.covers = covers;
return chart;
};
//inheritance
var F = function() {};
F.prototype = Raphael.g;
Piechart.prototype = new F;
//public
Raphael.fn.piechart = function(cx, cy, r, values, opts) {
return new Piechart(this, cx, cy, r, values, opts);
}
})();