a3ac8d3d430637de9608945c2c47f52c3f2d4fed
[g.raphael] / g.pie.circular.js
1 /*
2  * g.Raphael 0.5 - Charting library, based on RaphaĆ«l
3  *
4  * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com)
5  * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
6  */
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 : {});
10  
11
12
13 (function () {
14
15     /**
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?
23      *   
24      *   
25      * 
26      */
27
28     function Piecircularchart(paper, width, height, cx, cy, r, values, opts) {
29         
30         opts = opts || {};
31
32         var chartinst = this,
33             chart = paper.set(),
34             len = values.length;
35         
36         opts.linewidth = opts.linewidth || 200;
37         
38         paper.rect(0, 0, width, height).attr({ stroke: "none", fill: (opts.background || "#F0F4F7") });
39         
40         for (var i = 0; i < len; i++) {
41             values[i] = {
42                 value: values[i],
43                 origin: i,
44                 valueOf: function () { return this.value; }
45             };
46         }
47         
48         values.sort(function (a, b) {
49             return b.value - a.value;
50         });
51         
52         var dx = cx,
53             dy = cy,
54             dr = r;
55     
56         for (i = 0; i < len; i++) {
57             
58             paper.circle(dx, dy, dr).attr({ fill: opts.colors && opts.colors[i] || chartinst.colors[i] || "#3E66BC", stroke: "#fff", "stroke-width": 2 });
59             
60             var nx = dx,
61                 ny = dy - dr + 20;
62         
63             if(i != 0) {
64                 ny = dy - dr - 20;
65             }
66             
67             paper.text(nx, ny, Roo.util.Format.number(values[i], 0)).attr({ 
68                 "font-size": "18",
69                 "font-family": "'Fontin Sans', Fontin-Sans, sans-serif",
70                 "font-weight": "bold",
71                 fill : "#fff"
72             });
73             
74             paper.path(["M", dx, dy - dr, "l", opts.linewidth, 0]).attr({ stroke: "#fff", "stroke-width": 2 });
75             paper.text(dx + opts.linewidth + 10, dy - dr, opts.legend[values[i].origin]).attr({ 
76                 "font-size": "18",
77                 "font-family": "'Fontin Sans', Fontin-Sans, sans-serif",
78                 "text-anchor": "start",
79                 fill : "#0C024B"
80             });
81             
82             dy = dy + dr / 4 *3;
83             dr = Math.round(dr / 4);
84
85         }
86         
87         chart.cx = cx;
88         chart.cy = cy;
89         chart.r = r;
90         return chart;
91     };
92     
93     //inheritance
94     var F = function() {};
95     F.prototype = Raphael.g;
96     Piecircularchart.prototype = new F;
97     
98     //public
99     Raphael.fn.piecircularchart = function(width, height, cx, cy, r, values, opts) {
100         return new Piecircularchart(this, width, height, cx, cy, r, values, opts);
101     }
102     
103 })();