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.
7 Raphael = typeof(Raphael) != 'undefined' ? Raphael : (imports ? imports.seed.Raphael.Raphael : {});
8 Roo = typeof(Roo) != 'undefined' ? Roo: (imports ? imports.seed.Roo.Roo: {});
9 //chartinst = typeof(chartinst) != 'undefined' ? chartinst: (imports ? imports.chartinst.chartinst : {});
16 * @param {Raphael} paper to draw on
17 * @param {int} cx - centre X
18 * @param {int} cy - centre Y
19 * @param {int} r - radius
20 * @param {Array} values
21 * @param {Object} opts options
22 * cut : after this meany items - do not show a pie element?
28 function Piechart(paper, cx, cy, r, values, opts) {
45 function sector(cx, cy, r, startAngle, endAngle, fill) {
46 var rad = Math.PI / 180,
47 x1 = cx + r * Math.cos(-startAngle * rad),
48 x2 = cx + r * Math.cos(-endAngle * rad),
49 xm = cx + r / 2 * Math.cos(-(startAngle + (endAngle - startAngle) / 2) * rad),
50 y1 = cy + r * Math.sin(-startAngle * rad),
51 y2 = cy + r * Math.sin(-endAngle * rad),
52 ym = cy + r / 2 * Math.sin(-(startAngle + (endAngle - startAngle) / 2) * rad),
56 "A", r, r, 0, +(Math.abs(endAngle - startAngle) > 180), 1, x2, y2,
60 res.middle = { x: xm, y: ym };
64 chart.covers = covers;
67 series.push(paper.circle(cx, cy, r).attr({ fill: opts.colors && opts.colors[0] || chartinst.colors[0] || "#666", stroke: opts.stroke || "#fff", "stroke-width": opts.strokewidth == null ? 1 : opts.strokewidth }));
68 covers.push(paper.circle(cx, cy, r).attr(chartinst.shim));
69 total = values[0] * 1;
70 values[0] = { value: values[0], order: 0, valueOf: function () { return this.value; } };
71 series[0].middle = {x: cx, y: cy};
72 series[0].mangle = 180;
74 for (var i = 0; i < len; i++) {
75 total += values[i] * 1;
79 valueOf: function () { return this.value; }
84 values.sort(function (a, b) {
85 return b.value - a.value;
88 for (i = 0; i < len; i++) {
89 if (defcut && values[i] * 360 / total <= 1.5) {
96 values[cut].value += values[i];
97 values[cut].others = true;
98 others = values[cut].value;
102 len = Math.min(cut + 1, values.length);
103 others && values.splice(len) && (values[cut].others = true);
105 for (i = 0; i < len; i++) {
106 var mangle = angle - 360 * values[i] / total / 2;
110 if (typeof(opts.start_angle) == 'undefined') {
113 angle = opts.start_angle;
116 //Roo.log('start angle: ' + angle);
117 mangle = angle - 360 * values[i] / total / 2;
121 var ipath = sector(cx, cy, 1, angle, angle - 360 * values[i] / total).join(",");
124 var path = sector(cx, cy, r, angle, angle -= 360 * values[i] / total);
125 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" });
128 p.middle = path.middle;
132 opts.init && p.animate({ path: path.join(",") }, (+opts.init - 1) || 1000, ">");
135 for (i = 0; i < len; i++) {
136 p = paper.path(sectors[i].attr("path")).attr(chartinst.shim);
137 opts.href && opts.href[i] && p.attr({ href: opts.href[i] });
138 p.attr = function () {};
144 chart.hover = function (fin, fout) {
145 fout = fout || function () {};
149 for (var i = 0; i < len; i++) {
150 (function (sector, cover, j) {
158 mangle: sector.mangle,
163 label: that.labels && that.labels[j]
165 cover.mouseover(function () {
167 }).mouseout(function () {
170 })(series[i], covers[i], i);
175 // x: where label could be put
176 // y: where label could be put
177 // value: value to show
178 // total: total number to count %
179 chart.each = 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]
199 })(series[i], covers[i], i);
204 chart.click = function (f) {
207 for (var i = 0; i < len; i++) {
208 (function (sector, cover, j) {
216 mangle: sector.mangle,
221 label: that.labels && that.labels[j]
223 cover.click(function () { f.call(o); });
224 })(series[i], covers[i], i);
229 chart.inject = function (element) {
230 element.insertBefore(covers[0]);
233 var legend = function (labels, otherslabel, mark, dir) {
234 var x = cx + r + r / 5,
238 labels = labels || [];
239 dir = (dir && dir.toLowerCase && dir.toLowerCase()) || "east";
240 mark = paper[mark && mark.toLowerCase()] || "circle";
241 chart.labels = paper.set();
243 for (var i = 0; i < len; i++) {
244 var clr = series[i].attr("fill"),
248 values[i].others && (labels[j] = otherslabel || "Others");
249 labels[j] = chartinst.labelise(labels[j], values[i], total);
250 chart.labels.push(paper.set());
251 chart.labels[i].push(paper[mark](x + 5, h, 5).attr({ fill: clr, stroke: "none" }));
252 chart.labels[i].push(
253 txt = paper.text(x + 20, h, labels[j] || values[j]).attr(chartinst.txtattr).attr({ fill: opts.legendcolor || "#000", "text-anchor": "start"}));
254 covers[i].label = chart.labels[i];
255 // Roo.log(JSON.stringify(txt.getBBox()));
256 h += txt.getBBox().height * 1.2;
259 var bb = chart.labels.getBBox(),
261 east: [0, -bb.height / 2],
262 west: [-bb.width - 2 * r - 20, -bb.height / 2],
263 north: [-r - bb.width / 2, -r - bb.height - 10],
264 south: [-r - bb.width / 2, r + 10]
267 chart.labels.translate.apply(chart.labels, tr);
268 chart.push(chart.labels);
272 legend(opts.legend, opts.legendothers, opts.legendmark, opts.legendpos);
275 chart.push(series, covers);
276 chart.series = series;
277 chart.covers = covers;
279 chart.sector = sector;
287 var F = function() {};
288 F.prototype = Raphael.g;
289 Piechart.prototype = new F;
292 Raphael.fn.piechart = function(cx, cy, r, values, opts) {
293 return new Piechart(this, cx, cy, r, values, opts);