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) {
46 function sector(cx, cy, r, startAngle, endAngle, fill) {
47 var rad = Math.PI / 180,
48 x1 = cx + r * Math.cos(-startAngle * rad),
49 x2 = cx + r * Math.cos(-endAngle * rad),
50 xm = cx + r / 2 * Math.cos(-(startAngle + (endAngle - startAngle) / 2) * rad),
51 y1 = cy + r * Math.sin(-startAngle * rad),
52 y2 = cy + r * Math.sin(-endAngle * rad),
53 ym = cy + r / 2 * Math.sin(-(startAngle + (endAngle - startAngle) / 2) * rad),
57 "A", r, r, 0, +(Math.abs(endAngle - startAngle) > 180), 1, x2, y2,
61 res.middle = { x: xm, y: ym };
65 chart.covers = covers;
66 Roo.log('show colors');
67 Roo.log(chartinst.colors);
69 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 }));
70 covers.push(paper.circle(cx, cy, r).attr(chartinst.shim));
71 total = values[0] * 1;
72 values[0] = { value: values[0], order: 0, valueOf: function () { return this.value; } };
73 series[0].middle = {x: cx, y: cy};
74 series[0].mangle = 180;
76 for (var i = 0; i < len; i++) {
77 total += values[i] * 1;
81 valueOf: function () { return this.value; }
86 values.sort(function (a, b) {
87 return b.value - a.value;
90 for (i = 0; i < len; i++) {
91 if (defcut && values[i] * 360 / total <= 1.5) {
98 values[cut].value += values[i];
99 values[cut].others = true;
100 others = values[cut].value;
104 len = Math.min(cut + 1, values.length);
105 others && values.splice(len) && (values[cut].others = true);
107 for (i = 0; i < len; i++) {
108 var mangle = angle - 360 * values[i] / total / 2;
112 if (typeof(opts.start_angle) == 'undefined') {
115 angle = opts.start_angle;
118 //Roo.log('start angle: ' + angle);
119 mangle = angle - 360 * values[i] / total / 2;
123 var ipath = sector(cx, cy, 1, angle, angle - 360 * values[i] / total).join(",");
126 var path = sector(cx, cy, r, angle, angle -= 360 * values[i] / total);
128 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" });
131 p.middle = path.middle;
135 opts.init && p.animate({ path: path.join(",") }, (+opts.init - 1) || 1000, ">");
138 for (i = 0; i < len; i++) {
139 p = paper.path(sectors[i].attr("path")).attr(chartinst.shim);
140 opts.href && opts.href[i] && p.attr({ href: opts.href[i] });
141 p.attr = function () {};
147 chart.hover = function (fin, fout) {
148 fout = fout || function () {};
152 for (var i = 0; i < len; i++) {
153 (function (sector, cover, j) {
161 mangle: sector.mangle,
166 label: that.labels && that.labels[j]
168 cover.mouseover(function () {
170 }).mouseout(function () {
173 })(series[i], covers[i], i);
178 // x: where label could be put
179 // y: where label could be put
180 // value: value to show
181 // total: total number to count %
182 chart.each = function (f) {
185 for (var i = 0; i < len; i++) {
186 (function (sector, cover, j) {
194 mangle: sector.mangle,
199 label: that.labels && that.labels[j]
202 })(series[i], covers[i], i);
207 chart.click = function (f) {
210 for (var i = 0; i < len; i++) {
211 (function (sector, cover, j) {
219 mangle: sector.mangle,
224 label: that.labels && that.labels[j]
226 cover.click(function () { f.call(o); });
227 })(series[i], covers[i], i);
232 chart.inject = function (element) {
233 element.insertBefore(covers[0]);
236 var legend = function (labels, otherslabel, mark, dir) {
237 var x = cx + r + r / 5,
241 labels = labels || [];
242 dir = (dir && dir.toLowerCase && dir.toLowerCase()) || "east";
243 mark = paper[mark && mark.toLowerCase()] || "circle";
244 chart.labels = paper.set();
246 for (var i = 0; i < len; i++) {
247 var clr = series[i].attr("fill"),
251 values[i].others && (labels[j] = otherslabel || "Others");
252 labels[j] = chartinst.labelise(labels[j], values[i], total);
253 chart.labels.push(paper.set());
254 chart.labels[i].push(paper[mark](x + 5, h, 5).attr({ fill: clr, stroke: "none" }));
255 chart.labels[i].push(
256 txt = paper.text(x + 20, h, labels[j] || values[j]).attr(chartinst.txtattr).attr({ fill: opts.legendcolor || "#000", "text-anchor": "start"}));
257 covers[i].label = chart.labels[i];
258 // Roo.log(JSON.stringify(txt.getBBox()));
259 h += txt.getBBox().height * 1.2;
262 var bb = chart.labels.getBBox(),
264 east: [0, -bb.height / 2],
265 west: [-bb.width - 2 * r - 20, -bb.height / 2],
266 north: [-r - bb.width / 2, -r - bb.height - 10],
267 south: [-r - bb.width / 2, r + 10]
270 chart.labels.translate.apply(chart.labels, tr);
271 chart.push(chart.labels);
275 legend(opts.legend, opts.legendothers, opts.legendmark, opts.legendpos);
278 chart.push(series, covers);
279 chart.series = series;
280 chart.covers = covers;
282 chart.sector = sector;
290 var F = function() {};
291 F.prototype = Raphael.g;
292 Piechart.prototype = new F;
295 Raphael.fn.piechart = function(cx, cy, r, values, opts) {
296 return new Piechart(this, cx, cy, r, values, opts);