Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / src / chart / Label.js
1 /**
2  * @class Ext.chart.Label
3  *
4  * Labels is a mixin whose methods are appended onto the Series class. Labels is an interface with methods implemented
5  * in each of the Series (Pie, Bar, etc) for label creation and label placement.
6  *
7  * The methods implemented by the Series are:
8  *  
9  * - **`onCreateLabel(storeItem, item, i, display)`** Called each time a new label is created.
10  *   The arguments of the method are:
11  *   - *`storeItem`* The element of the store that is related to the label sprite.
12  *   - *`item`* The item related to the label sprite. An item is an object containing the position of the shape
13  *     used to describe the visualization and also pointing to the actual shape (circle, rectangle, path, etc).
14  *   - *`i`* The index of the element created (i.e the first created label, second created label, etc)
15  *   - *`display`* The display type. May be <b>false</b> if the label is hidden
16  *
17  *  - **`onPlaceLabel(label, storeItem, item, i, display, animate)`** Called for updating the position of the label.
18  *    The arguments of the method are:
19  *    - *`label`* The sprite label.</li>
20  *    - *`storeItem`* The element of the store that is related to the label sprite</li>
21  *    - *`item`* The item related to the label sprite. An item is an object containing the position of the shape
22  *      used to describe the visualization and also pointing to the actual shape (circle, rectangle, path, etc).
23  *    - *`i`* The index of the element to be updated (i.e. whether it is the first, second, third from the labelGroup)
24  *    - *`display`* The display type. May be <b>false</b> if the label is hidden.
25  *    - *`animate`* A boolean value to set or unset animations for the labels.
26  */
27 Ext.define('Ext.chart.Label', {
28
29     /* Begin Definitions */
30
31     requires: ['Ext.draw.Color'],
32     
33     /* End Definitions */
34
35     /**
36      * @cfg {String} display
37      * Specifies the presence and position of labels for each pie slice. Either "rotate", "middle", "insideStart",
38      * "insideEnd", "outside", "over", "under", or "none" to prevent label rendering.
39      * Default value: 'none'.
40      */
41
42     /**
43      * @cfg {String} color
44      * The color of the label text.
45      * Default value: '#000' (black).
46      */
47
48     /**
49      * @cfg {String} field
50      * The name of the field to be displayed in the label.
51      * Default value: 'name'.
52      */
53
54     /**
55      * @cfg {Number} minMargin
56      * Specifies the minimum distance from a label to the origin of the visualization.
57      * This parameter is useful when using PieSeries width variable pie slice lengths.
58      * Default value: 50.
59      */
60
61     /**
62      * @cfg {String} font
63      * The font used for the labels.
64      * Defautl value: "11px Helvetica, sans-serif".
65      */
66
67     /**
68      * @cfg {String} orientation
69      * Either "horizontal" or "vertical".
70      * Dafault value: "horizontal".
71      */
72
73     /**
74      * @cfg {Function} renderer
75      * Optional function for formatting the label into a displayable value.
76      * Default value: function(v) { return v; }
77      * @param v
78      */
79
80     //@private a regex to parse url type colors.
81     colorStringRe: /url\s*\(\s*#([^\/)]+)\s*\)/,
82     
83     //@private the mixin constructor. Used internally by Series.
84     constructor: function(config) {
85         var me = this;
86         me.label = Ext.applyIf(me.label || {},
87         {
88             display: "none",
89             color: "#000",
90             field: "name",
91             minMargin: 50,
92             font: "11px Helvetica, sans-serif",
93             orientation: "horizontal",
94             renderer: function(v) {
95                 return v;
96             }
97         });
98
99         if (me.label.display !== 'none') {
100             me.labelsGroup = me.chart.surface.getGroup(me.seriesId + '-labels');
101         }
102     },
103
104     //@private a method to render all labels in the labelGroup
105     renderLabels: function() {
106         var me = this,
107             chart = me.chart,
108             gradients = chart.gradients,
109             gradient,
110             items = me.items,
111             animate = chart.animate,
112             config = me.label,
113             display = config.display,
114             color = config.color,
115             field = [].concat(config.field),
116             group = me.labelsGroup,
117             store = me.chart.store,
118             len = store.getCount(),
119             ratio = items.length / len,
120             i, count, index, j, 
121             k, gradientsCount = (gradients || 0) && gradients.length,
122             colorStopTotal, colorStopIndex, colorStop,
123             item, label, storeItem,
124             sprite, spriteColor, spriteBrightness, labelColor,
125             Color = Ext.draw.Color,
126             colorString;
127
128         if (display == 'none') {
129             return;
130         }
131
132         for (i = 0, count = 0; i < len; i++) {
133             index = 0;
134             for (j = 0; j < ratio; j++) {
135                 item = items[count];
136                 label = group.getAt(count);
137                 storeItem = store.getAt(i);
138                 
139                 //check the excludes
140                 while(this.__excludes && this.__excludes[index]) {
141                     index++;
142                 }
143
144                 if (!item && label) {
145                     label.hide(true);
146                 }
147
148                 if (item && field[j]) {
149                     if (!label) {
150                         label = me.onCreateLabel(storeItem, item, i, display, j, index);
151                     }
152                     me.onPlaceLabel(label, storeItem, item, i, display, animate, j, index);
153
154                     //set contrast
155                     if (config.contrast && item.sprite) {
156                         sprite = item.sprite;
157                         colorString = sprite._to && sprite._to.fill || sprite.attr.fill;
158                         spriteColor = Color.fromString(colorString);
159                         //color wasn't parsed property maybe because it's a gradient id
160                         if (colorString && !spriteColor) {
161                             colorString = colorString.match(me.colorStringRe)[1];
162                             for (k = 0; k < gradientsCount; k++) {
163                                 gradient = gradients[k];
164                                 if (gradient.id == colorString) {
165                                     //avg color stops
166                                     colorStop = 0; colorStopTotal = 0;
167                                     for (colorStopIndex in gradient.stops) {
168                                         colorStop++;
169                                         colorStopTotal += Color.fromString(gradient.stops[colorStopIndex].color).getGrayscale();
170                                     }
171                                     spriteBrightness = (colorStopTotal / colorStop) / 255;
172                                     break;
173                                 }
174                             }
175                         }
176                         else {
177                             spriteBrightness = spriteColor.getGrayscale() / 255;
178                         }
179                         labelColor = Color.fromString(label.attr.color || label.attr.fill).getHSL();
180                         
181                         labelColor[2] = spriteBrightness > 0.5? 0.2 : 0.8;
182                         label.setAttributes({
183                             fill: String(Color.fromHSL.apply({}, labelColor))
184                         }, true);
185                     }
186                 }
187                 count++;
188                 index++;
189             }
190         }
191         me.hideLabels(count);
192     },
193
194     //@private a method to hide labels.
195     hideLabels: function(index) {
196         var labelsGroup = this.labelsGroup, len;
197         if (labelsGroup) {
198             len = labelsGroup.getCount();
199             while (len-->index) {
200                 labelsGroup.getAt(len).hide(true);
201             }
202         }
203     }
204 });