1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-chart.Label'>/**
2 </span> * @class Ext.chart.Label
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.
7 * The methods implemented by the Series are:
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
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.
27 Ext.define('Ext.chart.Label', {
29 /* Begin Definitions */
31 requires: ['Ext.draw.Color'],
35 <span id='Ext-chart.Label-cfg-display'> /**
36 </span> * @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'.
42 <span id='Ext-chart.Label-cfg-color'> /**
43 </span> * @cfg {String} color
44 * The color of the label text.
45 * Default value: '#000' (black).
48 <span id='Ext-chart.Label-cfg-field'> /**
49 </span> * @cfg {String} field
50 * The name of the field to be displayed in the label.
51 * Default value: 'name'.
54 <span id='Ext-chart.Label-cfg-minMargin'> /**
55 </span> * @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.
61 <span id='Ext-chart.Label-cfg-font'> /**
62 </span> * @cfg {String} font
63 * The font used for the labels.
64 * Defautl value: "11px Helvetica, sans-serif".
67 <span id='Ext-chart.Label-cfg-orientation'> /**
68 </span> * @cfg {String} orientation
69 * Either "horizontal" or "vertical".
70 * Dafault value: "horizontal".
73 <span id='Ext-chart.Label-cfg-renderer'> /**
74 </span> * @cfg {Function} renderer
75 * Optional function for formatting the label into a displayable value.
76 * Default value: function(v) { return v; }
80 //@private a regex to parse url type colors.
81 colorStringRe: /url\s*\(\s*#([^\/)]+)\s*\)/,
83 //@private the mixin constructor. Used internally by Series.
84 constructor: function(config) {
86 me.label = Ext.applyIf(me.label || {},
88 display: "none",
89 color: "#000",
90 field: "name",
92 font: "11px Helvetica, sans-serif",
93 orientation: "horizontal",
94 renderer: function(v) {
99 if (me.label.display !== 'none') {
100 me.labelsGroup = me.chart.surface.getGroup(me.seriesId + '-labels');
104 //@private a method to render all labels in the labelGroup
105 renderLabels: function() {
108 gradients = chart.gradients,
111 animate = chart.animate,
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,
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,
128 if (display == 'none') {
132 for (i = 0, count = 0; i < len; i++) {
133 for (j = 0; j < ratio; j++) {
135 label = group.getAt(count);
136 storeItem = store.getAt(i);
138 if (!item && label) {
142 if (item && field[j]) {
144 label = me.onCreateLabel(storeItem, item, i, display, j, count);
146 me.onPlaceLabel(label, storeItem, item, i, display, animate, j, count);
149 if (config.contrast && item.sprite) {
150 sprite = item.sprite;
151 colorString = sprite._to && sprite._to.fill || sprite.attr.fill;
152 spriteColor = Color.fromString(colorString);
153 //color wasn't parsed property maybe because it's a gradient id
154 if (colorString && !spriteColor) {
155 colorString = colorString.match(me.colorStringRe)[1];
156 for (k = 0; k < gradientsCount; k++) {
157 gradient = gradients[k];
158 if (gradient.id == colorString) {
160 colorStop = 0; colorStopTotal = 0;
161 for (colorStopIndex in gradient.stops) {
163 colorStopTotal += Color.fromString(gradient.stops[colorStopIndex].color).getGrayscale();
165 spriteBrightness = (colorStopTotal / colorStop) / 255;
171 spriteBrightness = spriteColor.getGrayscale() / 255;
173 labelColor = Color.fromString(label.attr.color || label.attr.fill).getHSL();
175 labelColor[2] = spriteBrightness > 0.5? 0.2 : 0.8;
176 label.setAttributes({
177 fill: String(Color.fromHSL.apply({}, labelColor))
184 me.hideLabels(count);
187 //@private a method to hide labels.
188 hideLabels: function(index) {
189 var labelsGroup = this.labelsGroup, len;
191 len = labelsGroup.getCount();
192 while (len-->index) {
193 labelsGroup.getAt(len).hide(true);
197 });</pre></pre></body></html>