2 * g.Raphael 0.5 - Charting library, based on Raphaƫl
4 * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com)
5 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
9 function Piechart(paper, cx, cy, r, values, opts) {
25 function sector(cx, cy, r, startAngle, endAngle, fill) {
26 var rad = Math.PI / 180,
27 x1 = cx + r * Math.cos(-startAngle * rad),
28 x2 = cx + r * Math.cos(-endAngle * rad),
29 xm = cx + r / 2 * Math.cos(-(startAngle + (endAngle - startAngle) / 2) * rad),
30 y1 = cy + r * Math.sin(-startAngle * rad),
31 y2 = cy + r * Math.sin(-endAngle * rad),
32 ym = cy + r / 2 * Math.sin(-(startAngle + (endAngle - startAngle) / 2) * rad),
36 "A", r, r, 0, +(Math.abs(endAngle - startAngle) > 180), 1, x2, y2,
40 res.middle = { x: xm, y: ym };
44 chart.covers = covers;
47 series.push(paper.circle(cx, cy, r).attr({ fill: chartinst.colors[0], stroke: opts.stroke || "#fff", "stroke-width": opts.strokewidth == null ? 1 : opts.strokewidth }));
48 covers.push(paper.circle(cx, cy, r).attr(chartinst.shim));
50 values[0] = { value: values[0], order: 0, valueOf: function () { return this.value; } };
51 series[0].middle = {x: cx, y: cy};
52 series[0].mangle = 180;
54 for (var i = 0; i < len; i++) {
56 values[i] = { value: values[i], order: i, valueOf: function () { return this.value; } };
59 values.sort(function (a, b) {
60 return b.value - a.value;
63 for (i = 0; i < len; i++) {
64 if (defcut && values[i] * 360 / total <= 1.5) {
71 values[cut].value += values[i];
72 values[cut].others = true;
73 others = values[cut].value;
77 len = Math.min(cut + 1, values.length);
78 others && values.splice(len) && (values[cut].others = true);
80 for (i = 0; i < len; i++) {
81 var mangle = angle - 360 * values[i] / total / 2;
85 if (typeof(opts.start_angle) == 'undefined') {
88 angle = opts.start_angle;
91 Roo.log('start angle: ' + angle);
92 mangle = angle - 360 * values[i] / total / 2;
96 var ipath = sector(cx, cy, 1, angle, angle - 360 * values[i] / total).join(",");
99 var path = sector(cx, cy, r, angle, angle -= 360 * values[i] / total);
100 var p = paper.path(opts.init ? ipath : path).attr({ fill: opts.colors && opts.colors[i] || chartinst.colors[i] || "#666", stroke: opts.stroke || "#fff", "stroke-width": (opts.strokewidth == null ? 1 : opts.strokewidth), "stroke-linejoin": "round" });
103 p.middle = path.middle;
107 opts.init && p.animate({ path: path.join(",") }, (+opts.init - 1) || 1000, ">");
110 for (i = 0; i < len; i++) {
111 p = paper.path(sectors[i].attr("path")).attr(chartinst.shim);
112 opts.href && opts.href[i] && p.attr({ href: opts.href[i] });
113 p.attr = function () {};
119 chart.hover = function (fin, fout) {
120 fout = fout || function () {};
124 for (var i = 0; i < len; i++) {
125 (function (sector, cover, j) {
133 mangle: sector.mangle,
138 label: that.labels && that.labels[j]
140 cover.mouseover(function () {
142 }).mouseout(function () {
145 })(series[i], covers[i], i);
150 // x: where label could be put
151 // y: where label could be put
152 // value: value to show
153 // total: total number to count %
154 chart.each = function (f) {
157 for (var i = 0; i < len; i++) {
158 (function (sector, cover, j) {
166 mangle: sector.mangle,
171 label: that.labels && that.labels[j]
174 })(series[i], covers[i], i);
179 chart.click = function (f) {
182 for (var i = 0; i < len; i++) {
183 (function (sector, cover, j) {
191 mangle: sector.mangle,
196 label: that.labels && that.labels[j]
198 cover.click(function () { f.call(o); });
199 })(series[i], covers[i], i);
204 chart.inject = function (element) {
205 element.insertBefore(covers[0]);
208 var legend = function (labels, otherslabel, mark, dir) {
209 var x = cx + r + r / 5,
213 labels = labels || [];
214 dir = (dir && dir.toLowerCase && dir.toLowerCase()) || "east";
215 mark = paper[mark && mark.toLowerCase()] || "circle";
216 chart.labels = paper.set();
218 for (var i = 0; i < len; i++) {
219 var clr = series[i].attr("fill"),
223 values[i].others && (labels[j] = otherslabel || "Others");
224 labels[j] = chartinst.labelise(labels[j], values[i], total);
226 chart.labels.push(paper.set());
227 chart.labels[i].push(paper[mark](x + 5, h, 5).attr({ fill: clr, stroke: "none" }));
228 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"}));
229 covers[i].label = chart.labels[i];
230 h += txt.getBBox().height * 1.2;
233 var bb = chart.labels.getBBox(),
235 east: [0, -bb.height / 2],
236 west: [-bb.width - 2 * r - 20, -bb.height / 2],
237 north: [-r - bb.width / 2, -r - bb.height - 10],
238 south: [-r - bb.width / 2, r + 10]
241 chart.labels.translate.apply(chart.labels, tr);
242 chart.push(chart.labels);
246 legend(opts.legend, opts.legendothers, opts.legendmark, opts.legendpos);
249 chart.push(series, covers);
250 chart.series = series;
251 chart.covers = covers;
253 chart.sector = sector;
261 var F = function() {};
262 F.prototype = Raphael.g;
263 Piechart.prototype = new F;
266 Raphael.fn.piechart = function(cx, cy, r, values, opts) {
267 return new Piechart(this, cx, cy, r, values, opts);