3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.chart.LegendItem
17 * @extends Ext.draw.CompositeSprite
18 * A single item of a legend (marker plus label)
20 Ext.define('Ext.chart.LegendItem', {
22 /* Begin Definitions */
24 extend: 'Ext.draw.CompositeSprite',
26 requires: ['Ext.chart.Shape'],
30 // Position of the item, relative to the upper-left corner of the legend box
35 constructor: function(config) {
36 this.callParent(arguments);
37 this.createLegend(config);
41 * Creates all the individual sprites for this legend item
43 createLegend: function(config) {
45 index = config.yFieldIndex,
47 seriesType = series.type,
51 refX = legend.x + me.x,
52 refY = legend.y + me.y,
54 markerConfig, label, mask,
55 radius, toggle = false,
56 seriesStyle = Ext.apply(series.seriesStyle, series.style);
58 function getSeriesProp(name) {
59 var val = series[name];
60 return (Ext.isArray(val) ? val[idx] : val);
63 label = me.add('label', surface.add({
68 font: legend.labelFont,
69 text: getSeriesProp('title') || getSeriesProp('yField')
72 // Line series - display as short line with optional marker in the middle
73 if (seriesType === 'line' || seriesType === 'scatter') {
74 if(seriesType === 'line') {
75 me.add('line', surface.add({
77 path: 'M0.5,0.5L16.5,0.5',
79 "stroke-width": series.lineWidth,
80 "stroke-linejoin": "round",
81 "stroke-dasharray": series.dash,
82 stroke: seriesStyle.stroke || '#000',
88 if (series.showMarkers || seriesType === 'scatter') {
89 markerConfig = Ext.apply(series.markerStyle, series.markerConfig || {});
90 me.add('marker', Ext.chart.Shape[markerConfig.type](surface, {
91 fill: markerConfig.fill,
95 radius: markerConfig.radius || markerConfig.size,
102 // All other series types - display as filled box
104 me.add('box', surface.add({
111 fill: series.getLegendColor(index),
124 mask = me.add('mask', surface.add({
128 width: bbox.width || 20,
129 height: bbox.height || 20,
130 zIndex: (z || 0) + 1000,
138 //add toggle listener
139 me.on('mouseover', function() {
141 'font-weight': 'bold'
146 series._index = index;
147 series.highlightItem();
150 me.on('mouseout', function() {
152 'font-weight': 'normal'
154 series._index = index;
155 series.unHighlightItem();
158 if (!series.visibleInLegend(index)) {
160 label.setAttributes({
165 me.on('mousedown', function() {
168 label.setAttributes({
173 label.setAttributes({
179 me.updatePosition({x:0, y:0}); //Relative to 0,0 at first so that the bbox is calculated correctly
183 * Update the positions of all this item's sprites to match the root position
185 * @param {Object} relativeTo (optional) If specified, this object's 'x' and 'y' values will be used
186 * as the reference point for the relative positioning. Defaults to the Legend.
188 updatePosition: function(relativeTo) {
195 relativeTo = me.legend;
197 for (; i < ln; i++) {
202 x: 20 + relativeTo.x + me.x,
203 y: relativeTo.y + me.y
209 x: relativeTo.x + me.x,
210 y: relativeTo.y + me.y - 6
217 x: relativeTo.x + me.x,
218 y: relativeTo.y + me.y