fix #8056 - more refinements to checking data
[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      *   background (string) : background color
23      *   colors (array) : colors of the pies
24      *   borderwidth (number) : width of circle border
25      *   labelfont (string) : font family of labels
26      *   labelsize (number) : font size of labels
27      *   labelweight (string) (number) : font weight of labels
28      *   labelcolor (string) : font color of the labels
29      *   linewidth (number) : width of the lines connecting the legend and the circles
30      *   lineheight (number) : height of the lines connecting the legend and the circles
31      *   legend (array) : legend
32      *   legendfont (string) : font family of the legend labels
33      *   legendfontsize (number) : font size of the legend labels
34      *   legendfontcolor (string) : font color of the legend colors
35      * 
36      */
37
38     function Piecircularchart(paper, width, height, cx, cy, r, values, opts) {
39         
40         opts = opts || {};
41
42         var chartinst = this,
43             chart = paper.set(),
44             len = values.length;
45             borderWidth = typeof(opts.borderwidth) != 'undefined' ? opts.borderwidth : 2, // width can be 0 (no border)
46             labelFont = opts.labelfont || "'Fontin Sans', Fontin-Sans, sans-serif",
47             labelSize = opts.labelsize || 18,
48             labelWeight = opts.labelweight || 'bold',
49             labelColor = opts.labelcolor || '#FFFFFF',
50             lineWidth = opts.linewidth || 200,
51             lineHeight = opts.lineheight || 2,
52             lineColor = opts.linecolor || '#FFFFFF',
53             legendFont = opts.legendfont || "'Fontin Sans', Fontin-Sans, sans-serif",
54             legendFontSize = opts.legendfontsize || 18,
55             legendFontColor = opts.legendfontcolor || '#0C024B';
56         
57         paper.rect(0, 0, width, height).attr({ stroke: "none", fill: (opts.background || "#F0F4F7") });
58         
59         for (var i = 0; i < len; i++) {
60             values[i] = {
61                 value: values[i],
62                 origin: i,
63                 valueOf: function () { return this.value; }
64             };
65         }
66         
67         values.sort(function (a, b) {
68             return b.value - a.value;
69         });
70         
71         // dx, dy: position of the center of the circle
72         // dr: radius of the circle
73         var dx = cx,
74             dy = cy,
75             dr = r;
76     
77         for (i = 0; i < len; i++) {
78             
79             paper.circle(dx, dy, dr).attr({ fill: opts.colors && opts.colors[i] || chartinst.colors[i] || "#3E66BC", stroke: "#fff", "stroke-width": borderWidth });
80             
81             // nx, ny: position of the label
82             var nx = dx,
83                 ny = dy - dr + 20;
84         
85             if(i != 0) {
86                 ny = dy - dr - 20;
87             }
88             
89             paper.text(nx, ny, Roo.util.Format.number(values[i], 0)).attr({ 
90                 "font-size": labelSize,
91                 "font-family": labelFont,
92                 "font-weight": labelWeight,
93                 "text-anchor": "middle",
94                 fill : labelColor
95             });
96             
97             paper.path(["M", dx, dy - dr, "l", lineWidth, 0]).attr({ stroke: lineColor, "stroke-width": lineHeight });
98
99             paper.text(dx + lineWidth + 10, dy - dr, opts.legend[values[i].origin]).attr({ 
100                 "font-size": legendFontSize,
101                 "font-family": legendFont,
102                 "text-anchor": "start",
103                 fill : legendFontColor
104             });
105             
106             dy = dy + dr / 4 *3;
107             dr = Math.round(dr / 4);
108
109         }
110         
111         chart.cx = cx;
112         chart.cy = cy;
113         chart.r = r;
114         return chart;
115     };
116     
117     //inheritance
118     var F = function() {};
119     F.prototype = Raphael.g;
120     Piecircularchart.prototype = new F;
121     
122     //public
123     Raphael.fn.piecircularchart = function(width, height, cx, cy, r, values, opts) {
124         return new Piecircularchart(this, width, height, cx, cy, r, values, opts);
125     }
126     
127 })();