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.Legend-method-constructor'><span id='Ext-chart.Legend'>/**
2 </span></span> * @class Ext.chart.Legend
4 * Defines a legend for a chart's series.
5 * The 'chart' member must be set prior to rendering.
6 * The legend class displays a list of legend items each of them related with a
7 * series being rendered. In order to render the legend item of the proper series
8 * the series configuration object must have `showInSeries` set to true.
10 * The legend configuration object accepts a `position` as parameter.
11 * The `position` parameter can be `left`, `right`
12 * `top` or `bottom`. For example:
19 <pre><code>
20 var store = Ext.create('Ext.data.JsonStore', {
21 fields: ['name', 'data1', 'data2', 'data3', 'data4', 'data5'],
23 {'name':'metric one', 'data1':10, 'data2':12, 'data3':14, 'data4':8, 'data5':13},
24 {'name':'metric two', 'data1':7, 'data2':8, 'data3':16, 'data4':10, 'data5':3},
25 {'name':'metric three', 'data1':5, 'data2':2, 'data3':14, 'data4':12, 'data5':7},
26 {'name':'metric four', 'data1':2, 'data2':14, 'data3':6, 'data4':1, 'data5':23},
27 {'name':'metric five', 'data1':27, 'data2':38, 'data3':36, 'data4':13, 'data5':33}
31 Ext.create('Ext.chart.Chart', {
32 renderTo: Ext.getBody(),
46 fields: ['data1', 'data2', 'data3', 'data4', 'data5'],
47 title: 'Sample Values',
57 adjustMinimumByMajorUnit: 0
62 title: 'Sample Metrics',
75 yField: ['data1', 'data2', 'data3', 'data4', 'data5'],
81 </code></pre>
85 Ext.define('Ext.chart.Legend', {
87 /* Begin Definitions */
89 requires: ['Ext.chart.LegendItem'],
93 <span id='Ext-chart.Legend-cfg-visible'> /**
94 </span> * @cfg {Boolean} visible
95 * Whether or not the legend should be displayed.
99 <span id='Ext-chart.Legend-cfg-position'> /**
100 </span> * @cfg {String} position
101 * The position of the legend in relation to the chart. One of: "top",
102 * "bottom", "left", "right", or "float". If set to "float", then the legend
103 * box will be positioned at the point denoted by the x and y parameters.
107 <span id='Ext-chart.Legend-cfg-x'> /**
108 </span> * @cfg {Number} x
109 * X-position of the legend box. Used directly if position is set to "float", otherwise
110 * it will be calculated dynamically.
114 <span id='Ext-chart.Legend-cfg-y'> /**
115 </span> * @cfg {Number} y
116 * Y-position of the legend box. Used directly if position is set to "float", otherwise
117 * it will be calculated dynamically.
121 <span id='Ext-chart.Legend-cfg-labelFont'> /**
122 </span> * @cfg {String} labelFont
123 * Font to be used for the legend labels, eg '12px Helvetica'
125 labelFont: '12px Helvetica, sans-serif',
127 <span id='Ext-chart.Legend-cfg-boxStroke'> /**
128 </span> * @cfg {String} boxStroke
129 * Style of the stroke for the legend box
133 <span id='Ext-chart.Legend-cfg-boxStrokeWidth'> /**
134 </span> * @cfg {String} boxStrokeWidth
135 * Width of the stroke for the legend box
139 <span id='Ext-chart.Legend-cfg-boxFill'> /**
140 </span> * @cfg {String} boxFill
141 * Fill style for the legend box
145 <span id='Ext-chart.Legend-cfg-itemSpacing'> /**
146 </span> * @cfg {Number} itemSpacing
147 * Amount of space between legend items
151 <span id='Ext-chart.Legend-cfg-padding'> /**
152 </span> * @cfg {Number} padding
153 * Amount of padding between the legend box's border and its items
162 <span id='Ext-chart.Legend-cfg-boxZIndex'> /**
163 </span> * @cfg {Number} boxZIndex
164 * Sets the z-index for the legend. Defaults to 100.
168 constructor: function(config) {
171 Ext.apply(me, config);
174 <span id='Ext-chart.Legend-property-isVertical'> /**
175 </span> * Whether the legend box is oriented vertically, i.e. if it is on the left or right side or floating.
178 me.isVertical = ("left|right|float".indexOf(me.position) !== -1);
180 // cache these here since they may get modified later on
185 <span id='Ext-chart.Legend-method-create'> /**
186 </span> * @private Create all the sprites for the legend
191 if (!me.created && me.isDisplayed()) {
195 // Listen for changes to series titles to trigger regeneration of the legend
196 me.chart.series.each(function(series) {
197 series.on('titlechange', function() {
205 <span id='Ext-chart.Legend-method-isDisplayed'> /**
206 </span> * @private Determine whether the legend should be displayed. Looks at the legend's 'visible' config,
207 * and also the 'showInLegend' config for each of the series.
209 isDisplayed: function() {
210 return this.visible && this.chart.series.findIndex('showInLegend', true) !== -1;
213 <span id='Ext-chart.Legend-method-createItems'> /**
214 </span> * @private Create the series markers and labels
216 createItems: function() {
219 surface = chart.surface,
221 padding = me.padding,
222 itemSpacing = me.itemSpacing,
228 vertical = me.isVertical,
234 len = items ? items.length : 0,
235 x, y, spacing, item, bbox, height, width;
237 //remove all legend items
239 for (; i < len; i++) {
245 // Create all the item labels, collecting their dimensions and positioning each one
246 // properly in relation to the previous item
247 chart.series.each(function(series, i) {
248 if (series.showInLegend) {
249 Ext.each([].concat(series.yField), function(field, j) {
250 item = Ext.create('Ext.chart.LegendItem', {
253 surface: chart.surface,
256 bbox = item.getBBox();
258 //always measure from x=0, since not all markers go all the way to the left
260 height = bbox.height;
263 spacing = vertical ? padding + height / 2 : padding;
266 spacing = itemSpacing / (vertical ? 2 : 1);
268 // Set the item's position relative to the legend box
269 item.x = mfloor(vertical ? padding : totalWidth + spacing);
270 item.y = mfloor(vertical ? totalHeight + spacing : padding + height / 2);
272 // Collect cumulative dimensions
273 totalWidth += width + spacing;
274 totalHeight += height + spacing;
275 maxWidth = mmax(maxWidth, width);
276 maxHeight = mmax(maxHeight, height);
283 // Store the collected dimensions for later
284 me.width = mfloor((vertical ? maxWidth : totalWidth) + padding * 2);
285 if (vertical && items.length === 1) {
288 me.height = mfloor((vertical ? totalHeight - spacingOffset * spacing : maxHeight) + (padding * 2));
289 me.itemHeight = maxHeight;
292 <span id='Ext-chart.Legend-method-getBBox'> /**
293 </span> * @private Get the bounds for the legend's outer box
295 getBBox: function() {
298 x: Math.round(me.x) - me.boxStrokeWidth / 2,
299 y: Math.round(me.y) - me.boxStrokeWidth / 2,
305 <span id='Ext-chart.Legend-method-createBox'> /**
306 </span> * @private Create the box around the legend items
308 createBox: function() {
310 box = me.boxSprite = me.chart.surface.add(Ext.apply({
312 stroke: me.boxStroke,
313 "stroke-width": me.boxStrokeWidth,
320 <span id='Ext-chart.Legend-method-updatePosition'> /**
321 </span> * @private Update the position of all the legend's sprites to match its current x/y values
323 updatePosition: function() {
326 legendWidth = me.width,
327 legendHeight = me.height,
328 padding = me.padding,
330 chartBBox = chart.chartBBox,
331 insets = chart.insetPadding,
332 chartWidth = chartBBox.width - (insets * 2),
333 chartHeight = chartBBox.height - (insets * 2),
334 chartX = chartBBox.x + insets,
335 chartY = chartBBox.y + insets,
336 surface = chart.surface,
339 if (me.isDisplayed()) {
340 // Find the position based on the dimensions
341 switch(me.position) {
342 case "left":
344 y = mfloor(chartY + chartHeight / 2 - legendHeight / 2);
346 case "right":
347 x = mfloor(surface.width - legendWidth) - insets;
348 y = mfloor(chartY + chartHeight / 2 - legendHeight / 2);
350 case "top":
351 x = mfloor(chartX + chartWidth / 2 - legendWidth / 2);
354 case "bottom":
355 x = mfloor(chartX + chartWidth / 2 - legendWidth / 2);
356 y = mfloor(surface.height - legendHeight) - insets;
359 x = mfloor(me.origX) + insets;
360 y = mfloor(me.origY) + insets;
365 // Update the position of each item
366 Ext.each(me.items, function(item) {
367 item.updatePosition();
369 // Update the position of the outer box
370 me.boxSprite.setAttributes(me.getBBox(), true);
373 });</pre></pre></body></html>