4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-chart-Label'>/**
19 </span> * @class Ext.chart.Label
21 * Labels is a mixin to the Series class. Labels methods are implemented
22 * in each of the Series (Pie, Bar, etc) for label creation and placement.
24 * The methods implemented by the Series are:
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 <b>false</b> if the label is hidden
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.</li>
37 * - *`storeItem`* The element of the store that is related to the label sprite</li>
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 <b>false</b> if the label is hidden.
42 * - *`animate`* A boolean value to set or unset animations for the labels.
44 Ext.define('Ext.chart.Label', {
46 /* Begin Definitions */
48 requires: ['Ext.draw.Color'],
52 <span id='Ext-chart-Label-cfg-label'> /**
53 </span> * @cfg {Object} label
54 * Object with the following properties:
56 * - **display** : String
58 * Specifies the presence and position of labels for each pie slice. Either "rotate", "middle", "insideStart",
59 * "insideEnd", "outside", "over", "under", or "none" to prevent label rendering.
60 * Default value: 'none'.
62 * - **color** : String
64 * The color of the label text.
65 * Default value: '#000' (black).
67 * - **contrast** : Boolean
69 * True to render the label in contrasting color with the backround.
70 * Default value: false.
72 * - **field** : String
74 * The name of the field to be displayed in the label.
75 * Default value: 'name'.
77 * - **minMargin** : Number
79 * Specifies the minimum distance from a label to the origin of the visualization.
80 * This parameter is useful when using PieSeries width variable pie slice lengths.
85 * The font used for the labels.
86 * Default value: "11px Helvetica, sans-serif".
88 * - **orientation** : String
90 * Either "horizontal" or "vertical".
91 * Dafault value: "horizontal".
93 * - **renderer** : Function
95 * Optional function for formatting the label into a displayable value.
96 * Default value: function(v) { return v; }
99 //@private a regex to parse url type colors.
100 colorStringRe: /url\s*\(\s*#([^\/)]+)\s*\)/,
102 //@private the mixin constructor. Used internally by Series.
103 constructor: function(config) {
105 me.label = Ext.applyIf(me.label || {},
107 display: "none",
108 color: "#000",
109 field: "name",
111 font: "11px Helvetica, sans-serif",
112 orientation: "horizontal",
113 renderer: function(v) {
118 if (me.label.display !== 'none') {
119 me.labelsGroup = me.chart.surface.getGroup(me.seriesId + '-labels');
123 //@private a method to render all labels in the labelGroup
124 renderLabels: function() {
127 gradients = chart.gradients,
129 animate = chart.animate,
131 display = config.display,
132 color = config.color,
133 field = [].concat(config.field),
134 group = me.labelsGroup,
135 groupLength = (group || 0) && group.length,
136 store = me.chart.store,
137 len = store.getCount(),
138 itemLength = (items || 0) && items.length,
139 ratio = itemLength / len,
140 gradientsCount = (gradients || 0) && gradients.length,
141 Color = Ext.draw.Color,
143 gradient, i, count, groupIndex, index, j, k, colorStopTotal, colorStopIndex, colorStop, item, label,
144 storeItem, sprite, spriteColor, spriteBrightness, labelColor, colorString;
146 if (display == 'none') {
149 // no items displayed, hide all labels
152 hides.push(groupLength);
154 for (i = 0, count = 0, groupIndex = 0; i < len; i++) {
156 for (j = 0; j < ratio; j++) {
158 label = group.getAt(groupIndex);
159 storeItem = store.getAt(i);
161 while(this.__excludes && this.__excludes[index] && ratio > 1) {
163 hides.push(groupIndex);
169 if (!item && label) {
174 if (item && field[j]) {
176 label = me.onCreateLabel(storeItem, item, i, display, j, index);
178 me.onPlaceLabel(label, storeItem, item, i, display, animate, j, index);
182 if (config.contrast && item.sprite) {
183 sprite = item.sprite;
184 //set the color string to the color to be set.
185 if (sprite._endStyle) {
186 colorString = sprite._endStyle.fill;
188 else if (sprite._to) {
189 colorString = sprite._to.fill;
192 colorString = sprite.attr.fill;
194 colorString = colorString || sprite.attr.fill;
196 spriteColor = Color.fromString(colorString);
197 //color wasn't parsed property maybe because it's a gradient id
198 if (colorString && !spriteColor) {
199 colorString = colorString.match(me.colorStringRe)[1];
200 for (k = 0; k < gradientsCount; k++) {
201 gradient = gradients[k];
202 if (gradient.id == colorString) {
204 colorStop = 0; colorStopTotal = 0;
205 for (colorStopIndex in gradient.stops) {
207 colorStopTotal += Color.fromString(gradient.stops[colorStopIndex].color).getGrayscale();
209 spriteBrightness = (colorStopTotal / colorStop) / 255;
215 spriteBrightness = spriteColor.getGrayscale() / 255;
217 if (label.isOutside) {
218 spriteBrightness = 1;
220 labelColor = Color.fromString(label.attr.color || label.attr.fill).getHSL();
221 labelColor[2] = spriteBrightness > 0.5 ? 0.2 : 0.8;
222 label.setAttributes({
223 fill: String(Color.fromHSL.apply({}, labelColor))
233 me.hideLabels(hides);
235 hideLabels: function(hides){
236 var labelsGroup = this.labelsGroup,
239 labelsGroup.getAt(hides[hlen]).hide(true);