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