Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / src / chart / axis / Radial.js
1 /**
2  * @class Ext.chart.axis.Radial
3  * @extends Ext.chart.axis.Abstract
4  * @ignore
5  */
6 Ext.define('Ext.chart.axis.Radial', {
7
8     /* Begin Definitions */
9
10     extend: 'Ext.chart.axis.Abstract',
11
12     /* End Definitions */
13
14     position: 'radial',
15
16     alias: 'axis.radial',
17
18     drawAxis: function(init) {
19         var chart = this.chart,
20             surface = chart.surface,
21             bbox = chart.chartBBox,
22             store = chart.store,
23             l = store.getCount(),
24             centerX = bbox.x + (bbox.width / 2),
25             centerY = bbox.y + (bbox.height / 2),
26             rho = Math.min(bbox.width, bbox.height) /2,
27             sprites = [], sprite,
28             steps = this.steps,
29             i, j, pi2 = Math.PI * 2,
30             cos = Math.cos, sin = Math.sin;
31
32         if (this.sprites && !chart.resizing) {
33             this.drawLabel();
34             return;
35         }
36
37         if (!this.sprites) {
38             //draw circles
39             for (i = 1; i <= steps; i++) {
40                 sprite = surface.add({
41                     type: 'circle',
42                     x: centerX,
43                     y: centerY,
44                     radius: Math.max(rho * i / steps, 0),
45                     stroke: '#ccc'
46                 });
47                 sprite.setAttributes({
48                     hidden: false
49                 }, true);
50                 sprites.push(sprite);
51             }
52             //draw lines
53             store.each(function(rec, i) {
54                 sprite = surface.add({
55                     type: 'path',
56                     path: ['M', centerX, centerY, 'L', centerX + rho * cos(i / l * pi2), centerY + rho * sin(i / l * pi2), 'Z'],
57                     stroke: '#ccc'
58                 });
59                 sprite.setAttributes({
60                     hidden: false
61                 }, true);
62                 sprites.push(sprite);
63             });
64         } else {
65             sprites = this.sprites;
66             //draw circles
67             for (i = 0; i < steps; i++) {
68                 sprites[i].setAttributes({
69                     x: centerX,
70                     y: centerY,
71                     radius: Math.max(rho * (i + 1) / steps, 0),
72                     stroke: '#ccc'
73                 }, true);
74             }
75             //draw lines
76             store.each(function(rec, j) {
77                 sprites[i + j].setAttributes({
78                     path: ['M', centerX, centerY, 'L', centerX + rho * cos(j / l * pi2), centerY + rho * sin(j / l * pi2), 'Z'],
79                     stroke: '#ccc'
80                 }, true);
81             });
82         }
83         this.sprites = sprites;
84
85         this.drawLabel();
86     },
87
88     drawLabel: function() {
89         var chart = this.chart,
90             surface = chart.surface,
91             bbox = chart.chartBBox,
92             store = chart.store,
93             centerX = bbox.x + (bbox.width / 2),
94             centerY = bbox.y + (bbox.height / 2),
95             rho = Math.min(bbox.width, bbox.height) /2,
96             max = Math.max, round = Math.round,
97             labelArray = [], label,
98             fields = [], nfields,
99             categories = [], xField,
100             aggregate = !this.maximum,
101             maxValue = this.maximum || 0,
102             steps = this.steps, i = 0, j, dx, dy,
103             pi2 = Math.PI * 2,
104             cos = Math.cos, sin = Math.sin,
105             display = this.label.display,
106             draw = display !== 'none',
107             margin = 10;
108
109         if (!draw) {
110             return;
111         }
112
113         //get all rendered fields
114         chart.series.each(function(series) {
115             fields.push(series.yField);
116             xField = series.xField;
117         });
118         
119         //get maxValue to interpolate
120         store.each(function(record, i) {
121             if (aggregate) {
122                 for (i = 0, nfields = fields.length; i < nfields; i++) {
123                     maxValue = max(+record.get(fields[i]), maxValue);
124                 }
125             }
126             categories.push(record.get(xField));
127         });
128         if (!this.labelArray) {
129             if (display != 'categories') {
130                 //draw scale
131                 for (i = 1; i <= steps; i++) {
132                     label = surface.add({
133                         type: 'text',
134                         text: round(i / steps * maxValue),
135                         x: centerX,
136                         y: centerY - rho * i / steps,
137                         'text-anchor': 'middle',
138                         'stroke-width': 0.1,
139                         stroke: '#333'
140                     });
141                     label.setAttributes({
142                         hidden: false
143                     }, true);
144                     labelArray.push(label);
145                 }
146             }
147             if (display != 'scale') {
148                 //draw text
149                 for (j = 0, steps = categories.length; j < steps; j++) {
150                     dx = cos(j / steps * pi2) * (rho + margin);
151                     dy = sin(j / steps * pi2) * (rho + margin);
152                     label = surface.add({
153                         type: 'text',
154                         text: categories[j],
155                         x: centerX + dx,
156                         y: centerY + dy,
157                         'text-anchor': dx * dx <= 0.001? 'middle' : (dx < 0? 'end' : 'start')
158                     });
159                     label.setAttributes({
160                         hidden: false
161                     }, true);
162                     labelArray.push(label);
163                 }
164             }
165         }
166         else {
167             labelArray = this.labelArray;
168             if (display != 'categories') {
169                 //draw values
170                 for (i = 0; i < steps; i++) {
171                     labelArray[i].setAttributes({
172                         text: round((i + 1) / steps * maxValue),
173                         x: centerX,
174                         y: centerY - rho * (i + 1) / steps,
175                         'text-anchor': 'middle',
176                         'stroke-width': 0.1,
177                         stroke: '#333'
178                     }, true);
179                 }
180             }
181             if (display != 'scale') {
182                 //draw text
183                 for (j = 0, steps = categories.length; j < steps; j++) {
184                     dx = cos(j / steps * pi2) * (rho + margin);
185                     dy = sin(j / steps * pi2) * (rho + margin);
186                     if (labelArray[i + j]) {
187                         labelArray[i + j].setAttributes({
188                             type: 'text',
189                             text: categories[j],
190                             x: centerX + dx,
191                             y: centerY + dy,
192                             'text-anchor': dx * dx <= 0.001? 'middle' : (dx < 0? 'end' : 'start')
193                         }, true);
194                     }
195                 }
196             }
197         }
198         this.labelArray = labelArray;
199     }
200 });