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; }
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-Legend-method-constructor'><span id='Ext-chart-Legend'>/**
19 </span></span> * @class Ext.chart.Legend
21 * Defines a legend for a chart's series.
22 * The 'chart' member must be set prior to rendering.
23 * The legend class displays a list of legend items each of them related with a
24 * series being rendered. In order to render the legend item of the proper series
25 * the series configuration object must have `showInSeries` set to true.
27 * The legend configuration object accepts a `position` as parameter.
28 * The `position` parameter can be `left`, `right`
29 * `top` or `bottom`. For example:
36 <pre><code>
37 var store = Ext.create('Ext.data.JsonStore', {
38 fields: ['name', 'data1', 'data2', 'data3', 'data4', 'data5'],
40 {'name':'metric one', 'data1':10, 'data2':12, 'data3':14, 'data4':8, 'data5':13},
41 {'name':'metric two', 'data1':7, 'data2':8, 'data3':16, 'data4':10, 'data5':3},
42 {'name':'metric three', 'data1':5, 'data2':2, 'data3':14, 'data4':12, 'data5':7},
43 {'name':'metric four', 'data1':2, 'data2':14, 'data3':6, 'data4':1, 'data5':23},
44 {'name':'metric five', 'data1':27, 'data2':38, 'data3':36, 'data4':13, 'data5':33}
48 Ext.create('Ext.chart.Chart', {
49 renderTo: Ext.getBody(),
63 fields: ['data1', 'data2', 'data3', 'data4', 'data5'],
64 title: 'Sample Values',
74 adjustMinimumByMajorUnit: 0
79 title: 'Sample Metrics',
92 yField: ['data1', 'data2', 'data3', 'data4', 'data5'],
98 </code></pre>
102 Ext.define('Ext.chart.Legend', {
104 /* Begin Definitions */
106 requires: ['Ext.chart.LegendItem'],
108 /* End Definitions */
110 <span id='Ext-chart-Legend-cfg-visible'> /**
111 </span> * @cfg {Boolean} visible
112 * Whether or not the legend should be displayed.
116 <span id='Ext-chart-Legend-cfg-position'> /**
117 </span> * @cfg {String} position
118 * The position of the legend in relation to the chart. One of: "top",
119 * "bottom", "left", "right", or "float". If set to "float", then the legend
120 * box will be positioned at the point denoted by the x and y parameters.
124 <span id='Ext-chart-Legend-cfg-x'> /**
125 </span> * @cfg {Number} x
126 * X-position of the legend box. Used directly if position is set to "float", otherwise
127 * it will be calculated dynamically.
131 <span id='Ext-chart-Legend-cfg-y'> /**
132 </span> * @cfg {Number} y
133 * Y-position of the legend box. Used directly if position is set to "float", otherwise
134 * it will be calculated dynamically.
138 <span id='Ext-chart-Legend-cfg-labelFont'> /**
139 </span> * @cfg {String} labelFont
140 * Font to be used for the legend labels, eg '12px Helvetica'
142 labelFont: '12px Helvetica, sans-serif',
144 <span id='Ext-chart-Legend-cfg-boxStroke'> /**
145 </span> * @cfg {String} boxStroke
146 * Style of the stroke for the legend box
150 <span id='Ext-chart-Legend-cfg-boxStrokeWidth'> /**
151 </span> * @cfg {String} boxStrokeWidth
152 * Width of the stroke for the legend box
156 <span id='Ext-chart-Legend-cfg-boxFill'> /**
157 </span> * @cfg {String} boxFill
158 * Fill style for the legend box
162 <span id='Ext-chart-Legend-cfg-itemSpacing'> /**
163 </span> * @cfg {Number} itemSpacing
164 * Amount of space between legend items
168 <span id='Ext-chart-Legend-cfg-padding'> /**
169 </span> * @cfg {Number} padding
170 * Amount of padding between the legend box's border and its items
179 <span id='Ext-chart-Legend-cfg-boxZIndex'> /**
180 </span> * @cfg {Number} boxZIndex
181 * Sets the z-index for the legend. Defaults to 100.
185 constructor: function(config) {
188 Ext.apply(me, config);
191 <span id='Ext-chart-Legend-property-isVertical'> /**
192 </span> * Whether the legend box is oriented vertically, i.e. if it is on the left or right side or floating.
195 me.isVertical = ("left|right|float".indexOf(me.position) !== -1);
197 // cache these here since they may get modified later on
202 <span id='Ext-chart-Legend-method-create'> /**
203 </span> * @private Create all the sprites for the legend
208 if (!me.created && me.isDisplayed()) {
212 // Listen for changes to series titles to trigger regeneration of the legend
213 me.chart.series.each(function(series) {
214 series.on('titlechange', function() {
222 <span id='Ext-chart-Legend-method-isDisplayed'> /**
223 </span> * @private Determine whether the legend should be displayed. Looks at the legend's 'visible' config,
224 * and also the 'showInLegend' config for each of the series.
226 isDisplayed: function() {
227 return this.visible && this.chart.series.findIndex('showInLegend', true) !== -1;
230 <span id='Ext-chart-Legend-method-createItems'> /**
231 </span> * @private Create the series markers and labels
233 createItems: function() {
236 surface = chart.surface,
238 padding = me.padding,
239 itemSpacing = me.itemSpacing,
245 vertical = me.isVertical,
251 len = items ? items.length : 0,
252 x, y, spacing, item, bbox, height, width;
254 //remove all legend items
256 for (; i < len; i++) {
262 // Create all the item labels, collecting their dimensions and positioning each one
263 // properly in relation to the previous item
264 chart.series.each(function(series, i) {
265 if (series.showInLegend) {
266 Ext.each([].concat(series.yField), function(field, j) {
267 item = Ext.create('Ext.chart.LegendItem', {
270 surface: chart.surface,
273 bbox = item.getBBox();
275 //always measure from x=0, since not all markers go all the way to the left
277 height = bbox.height;
280 spacing = vertical ? padding + height / 2 : padding;
283 spacing = itemSpacing / (vertical ? 2 : 1);
285 // Set the item's position relative to the legend box
286 item.x = mfloor(vertical ? padding : totalWidth + spacing);
287 item.y = mfloor(vertical ? totalHeight + spacing : padding + height / 2);
289 // Collect cumulative dimensions
290 totalWidth += width + spacing;
291 totalHeight += height + spacing;
292 maxWidth = mmax(maxWidth, width);
293 maxHeight = mmax(maxHeight, height);
300 // Store the collected dimensions for later
301 me.width = mfloor((vertical ? maxWidth : totalWidth) + padding * 2);
302 if (vertical && items.length === 1) {
305 me.height = mfloor((vertical ? totalHeight - spacingOffset * spacing : maxHeight) + (padding * 2));
306 me.itemHeight = maxHeight;
309 <span id='Ext-chart-Legend-method-getBBox'> /**
310 </span> * @private Get the bounds for the legend's outer box
312 getBBox: function() {
315 x: Math.round(me.x) - me.boxStrokeWidth / 2,
316 y: Math.round(me.y) - me.boxStrokeWidth / 2,
322 <span id='Ext-chart-Legend-method-createBox'> /**
323 </span> * @private Create the box around the legend items
325 createBox: function() {
327 box = me.boxSprite = me.chart.surface.add(Ext.apply({
329 stroke: me.boxStroke,
330 "stroke-width": me.boxStrokeWidth,
337 <span id='Ext-chart-Legend-method-updatePosition'> /**
338 </span> * @private Update the position of all the legend's sprites to match its current x/y values
340 updatePosition: function() {
343 legendWidth = me.width,
344 legendHeight = me.height,
345 padding = me.padding,
347 chartBBox = chart.chartBBox,
348 insets = chart.insetPadding,
349 chartWidth = chartBBox.width - (insets * 2),
350 chartHeight = chartBBox.height - (insets * 2),
351 chartX = chartBBox.x + insets,
352 chartY = chartBBox.y + insets,
353 surface = chart.surface,
356 if (me.isDisplayed()) {
357 // Find the position based on the dimensions
358 switch(me.position) {
359 case "left":
361 y = mfloor(chartY + chartHeight / 2 - legendHeight / 2);
363 case "right":
364 x = mfloor(surface.width - legendWidth) - insets;
365 y = mfloor(chartY + chartHeight / 2 - legendHeight / 2);
367 case "top":
368 x = mfloor(chartX + chartWidth / 2 - legendWidth / 2);
371 case "bottom":
372 x = mfloor(chartX + chartWidth / 2 - legendWidth / 2);
373 y = mfloor(surface.height - legendHeight) - insets;
376 x = mfloor(me.origX) + insets;
377 y = mfloor(me.origY) + insets;
382 // Update the position of each item
383 Ext.each(me.items, function(item) {
384 item.updatePosition();
386 // Update the position of the outer box
387 me.boxSprite.setAttributes(me.getBBox(), true);