Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / docs / source / Label2.html
index 1d77181..4fd77cd 100644 (file)
@@ -3,8 +3,8 @@
 <head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>The source code</title>
-  <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
-  <script type="text/javascript" src="../prettify/prettify.js"></script>
+  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
+  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
   <style type="text/css">
     .highlight { display: block; background-color: #ddd; }
   </style>
   <pre class="prettyprint lang-js"><span id='Ext-chart-Label'>/**
 </span> * @class Ext.chart.Label
  *
- * Labels is a mixin whose methods are appended onto the Series class. Labels is an interface with methods implemented
- * in each of the Series (Pie, Bar, etc) for label creation and label placement.
+ * Labels is a mixin to the Series class. Labels methods are implemented
+ * in each of the Series (Pie, Bar, etc) for label creation and placement.
  *
  * The methods implemented by the Series are:
- *  
+ *
  * - **`onCreateLabel(storeItem, item, i, display)`** Called each time a new label is created.
  *   The arguments of the method are:
  *   - *`storeItem`* The element of the store that is related to the label sprite.
@@ -46,57 +46,59 @@ Ext.define('Ext.chart.Label', {
     /* Begin Definitions */
 
     requires: ['Ext.draw.Color'],
-    
-    /* End Definitions */
-
-<span id='Ext-chart-Label-cfg-display'>    /**
-</span>     * @cfg {String} display
-     * Specifies the presence and position of labels for each pie slice. Either &quot;rotate&quot;, &quot;middle&quot;, &quot;insideStart&quot;,
-     * &quot;insideEnd&quot;, &quot;outside&quot;, &quot;over&quot;, &quot;under&quot;, or &quot;none&quot; to prevent label rendering.
-     * Default value: 'none'.
-     */
-
-<span id='Ext-chart-Label-cfg-color'>    /**
-</span>     * @cfg {String} color
-     * The color of the label text.
-     * Default value: '#000' (black).
-     */
-
-<span id='Ext-chart-Label-cfg-field'>    /**
-</span>     * @cfg {String} field
-     * The name of the field to be displayed in the label.
-     * Default value: 'name'.
-     */
-
-<span id='Ext-chart-Label-cfg-minMargin'>    /**
-</span>     * @cfg {Number} minMargin
-     * Specifies the minimum distance from a label to the origin of the visualization.
-     * This parameter is useful when using PieSeries width variable pie slice lengths.
-     * Default value: 50.
-     */
-
-<span id='Ext-chart-Label-cfg-font'>    /**
-</span>     * @cfg {String} font
-     * The font used for the labels.
-     * Defautl value: &quot;11px Helvetica, sans-serif&quot;.
-     */
 
-<span id='Ext-chart-Label-cfg-orientation'>    /**
-</span>     * @cfg {String} orientation
-     * Either &quot;horizontal&quot; or &quot;vertical&quot;.
-     * Dafault value: &quot;horizontal&quot;.
-     */
+    /* End Definitions */
 
-<span id='Ext-chart-Label-cfg-renderer'>    /**
-</span>     * @cfg {Function} renderer
-     * Optional function for formatting the label into a displayable value.
-     * Default value: function(v) { return v; }
-     * @param v
+<span id='Ext-chart-Label-cfg-label'>    /**
+</span>     * @cfg {Object} label
+     * Object with the following properties:
+     *
+     * - **display** : String
+     *
+     *   Specifies the presence and position of labels for each pie slice. Either &quot;rotate&quot;, &quot;middle&quot;, &quot;insideStart&quot;,
+     *   &quot;insideEnd&quot;, &quot;outside&quot;, &quot;over&quot;, &quot;under&quot;, or &quot;none&quot; to prevent label rendering.
+     *   Default value: 'none'.
+     *
+     * - **color** : String
+     *
+     *   The color of the label text.
+     *   Default value: '#000' (black).
+     *
+     * - **contrast** : Boolean
+     *
+     *   True to render the label in contrasting color with the backround.
+     *   Default value: false.
+     *
+     * - **field** : String
+     *
+     *   The name of the field to be displayed in the label.
+     *   Default value: 'name'.
+     *
+     * - **minMargin** : Number
+     *
+     *   Specifies the minimum distance from a label to the origin of the visualization.
+     *   This parameter is useful when using PieSeries width variable pie slice lengths.
+     *   Default value: 50.
+     *
+     * - **font** : String
+     *
+     *   The font used for the labels.
+     *   Default value: &quot;11px Helvetica, sans-serif&quot;.
+     *
+     * - **orientation** : String
+     *
+     *   Either &quot;horizontal&quot; or &quot;vertical&quot;.
+     *   Dafault value: &quot;horizontal&quot;.
+     *
+     * - **renderer** : Function
+     *
+     *   Optional function for formatting the label into a displayable value.
+     *   Default value: function(v) { return v; }
      */
 
     //@private a regex to parse url type colors.
     colorStringRe: /url\s*\(\s*#([^\/)]+)\s*\)/,
-    
+
     //@private the mixin constructor. Used internally by Series.
     constructor: function(config) {
         var me = this;
@@ -130,103 +132,111 @@ Ext.define('Ext.chart.Label', {
             color = config.color,
             field = [].concat(config.field),
             group = me.labelsGroup,
+            groupLength = (group || 0) &amp;&amp; group.length,
             store = me.chart.store,
             len = store.getCount(),
             itemLength = (items || 0) &amp;&amp; items.length,
             ratio = itemLength / len,
             gradientsCount = (gradients || 0) &amp;&amp; gradients.length,
             Color = Ext.draw.Color,
-            gradient, i, count, index, j, k, colorStopTotal, colorStopIndex, colorStop, item, label,
+            hides = [],
+            gradient, i, count, groupIndex, index, j, k, colorStopTotal, colorStopIndex, colorStop, item, label,
             storeItem, sprite, spriteColor, spriteBrightness, labelColor, colorString;
 
         if (display == 'none') {
             return;
         }
+        // no items displayed, hide all labels
+        if(itemLength == 0){
+            while(groupLength--)
+                hides.push(groupLength);
+        }else{
+            for (i = 0, count = 0, groupIndex = 0; i &lt; len; i++) {
+                index = 0;
+                for (j = 0; j &lt; ratio; j++) {
+                    item = items[count];
+                    label = group.getAt(groupIndex);
+                    storeItem = store.getAt(i);
+                    //check the excludes
+                    while(this.__excludes &amp;&amp; this.__excludes[index] &amp;&amp; ratio &gt; 1) {
+                        if(field[j]){
+                            hides.push(groupIndex);
+                        }
+                        index++;
 
-        for (i = 0, count = 0; i &lt; len; i++) {
-            index = 0;
-            for (j = 0; j &lt; ratio; j++) {
-                item = items[count];
-                label = group.getAt(count);
-                storeItem = store.getAt(i);
-                
-                //check the excludes
-                while(this.__excludes &amp;&amp; this.__excludes[index]) {
-                    index++;
-                }
-
-                if (!item &amp;&amp; label) {
-                    label.hide(true);
-                }
+                    }
 
-                if (item &amp;&amp; field[j]) {
-                    if (!label) {
-                        label = me.onCreateLabel(storeItem, item, i, display, j, index);
+                    if (!item &amp;&amp; label) {
+                        label.hide(true);
+                        groupIndex++;
                     }
-                    me.onPlaceLabel(label, storeItem, item, i, display, animate, j, index);
 
-                    //set contrast
-                    if (config.contrast &amp;&amp; item.sprite) {
-                        sprite = item.sprite;
-                        //set the color string to the color to be set.
-                        if (sprite._endStyle) {
-                            colorString = sprite._endStyle.fill;
-                        }
-                        else if (sprite._to) {
-                            colorString = sprite._to.fill;
-                        }
-                        else {
-                            colorString = sprite.attr.fill;
+                    if (item &amp;&amp; field[j]) {
+                        if (!label) {
+                            label = me.onCreateLabel(storeItem, item, i, display, j, index);
                         }
-                        colorString = colorString || sprite.attr.fill;
-                        
-                        spriteColor = Color.fromString(colorString);
-                        //color wasn't parsed property maybe because it's a gradient id
-                        if (colorString &amp;&amp; !spriteColor) {
-                            colorString = colorString.match(me.colorStringRe)[1];
-                            for (k = 0; k &lt; gradientsCount; k++) {
-                                gradient = gradients[k];
-                                if (gradient.id == colorString) {
-                                    //avg color stops
-                                    colorStop = 0; colorStopTotal = 0;
-                                    for (colorStopIndex in gradient.stops) {
-                                        colorStop++;
-                                        colorStopTotal += Color.fromString(gradient.stops[colorStopIndex].color).getGrayscale();
+                        me.onPlaceLabel(label, storeItem, item, i, display, animate, j, index);
+                        groupIndex++;
+
+                        //set contrast
+                        if (config.contrast &amp;&amp; item.sprite) {
+                            sprite = item.sprite;
+                            //set the color string to the color to be set.
+                            if (sprite._endStyle) {
+                                colorString = sprite._endStyle.fill;
+                            }
+                            else if (sprite._to) {
+                                colorString = sprite._to.fill;
+                            }
+                            else {
+                                colorString = sprite.attr.fill;
+                            }
+                            colorString = colorString || sprite.attr.fill;
+
+                            spriteColor = Color.fromString(colorString);
+                            //color wasn't parsed property maybe because it's a gradient id
+                            if (colorString &amp;&amp; !spriteColor) {
+                                colorString = colorString.match(me.colorStringRe)[1];
+                                for (k = 0; k &lt; gradientsCount; k++) {
+                                    gradient = gradients[k];
+                                    if (gradient.id == colorString) {
+                                        //avg color stops
+                                        colorStop = 0; colorStopTotal = 0;
+                                        for (colorStopIndex in gradient.stops) {
+                                            colorStop++;
+                                            colorStopTotal += Color.fromString(gradient.stops[colorStopIndex].color).getGrayscale();
+                                        }
+                                        spriteBrightness = (colorStopTotal / colorStop) / 255;
+                                        break;
                                     }
-                                    spriteBrightness = (colorStopTotal / colorStop) / 255;
-                                    break;
                                 }
                             }
+                            else {
+                                spriteBrightness = spriteColor.getGrayscale() / 255;
+                            }
+                            if (label.isOutside) {
+                                spriteBrightness = 1;
+                            }
+                            labelColor = Color.fromString(label.attr.color || label.attr.fill).getHSL();
+                            labelColor[2] = spriteBrightness &gt; 0.5 ? 0.2 : 0.8;
+                            label.setAttributes({
+                                fill: String(Color.fromHSL.apply({}, labelColor))
+                            }, true);
                         }
-                        else {
-                            spriteBrightness = spriteColor.getGrayscale() / 255;
-                        }
-                        if (label.isOutside) {
-                            spriteBrightness = 1;
-                        }
-                        labelColor = Color.fromString(label.attr.color || label.attr.fill).getHSL();
-                        labelColor[2] = spriteBrightness &gt; 0.5 ? 0.2 : 0.8;
-                        label.setAttributes({
-                            fill: String(Color.fromHSL.apply({}, labelColor))
-                        }, true);
+
                     }
+                    count++;
+                    index++;
                 }
-                count++;
-                index++;
             }
         }
-        me.hideLabels(count);
+        me.hideLabels(hides);
     },
-
-    //@private a method to hide labels.
-    hideLabels: function(index) {
-        var labelsGroup = this.labelsGroup, len;
-        if (labelsGroup) {
-            len = labelsGroup.getCount();
-            while (len--&gt;index) {
-                labelsGroup.getAt(len).hide(true);
-            }
-        }
+    hideLabels: function(hides){
+        var labelsGroup = this.labelsGroup,
+            hlen = hides.length;
+        while(hlen--)
+            labelsGroup.getAt(hides[hlen]).hide(true);
     }
 });</pre>
 </body>