-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLegend.js
264 lines (223 loc) · 7.69 KB
/
Legend.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
Chart Legend with no additional features; some methods are refactored
for better flexibility.
This class contains almost 100% Sencha code so I am not releasing it
but providing it as an example under the same terms as Ext JS itself.
Hmm. Not sure I can do this, even.
This class is not intended to be used directly.
*/
Ext.define('Ext.ux.chart.Legend', {
extend: 'Ext.chart.Legend',
requires: [ 'Ext.ux.chart.LegendItem' ],
/**
* @private Create all the sprites for the legend
*/
create: function() {
var me = this,
items = me.chart.series.items;
me.createBox();
if (me.rebuild !== false) {
me.createItems();
};
if (!me.created && me.isDisplayed()) {
me.created = true;
// Listen for changes to series titles to trigger regeneration of the legend
for ( var i = 0, l = items.length; i < l; i++ ) {
var series = items[i];
series.on('titlechange', me.redraw, me);
};
};
},
/**
* @private Redraws the Legend
*/
redraw: function() {
var me = this;
me.create();
me.updatePosition();
},
/**
* @private Create the series markers and labels
*/
createItems: function() {
var me = this,
seriesItems = me.chart.series.items,
items = me.items,
fields;
//remove all legend items
me.removeItems();
// Create all the item labels
for ( var i = 0, li = seriesItems.length; i < li; i++ ) {
var series = seriesItems[i];
if (series.showInLegend) {
fields = [].concat(series.yField);
for ( var j = 0, lj = fields.length; j < lj; j++ ) {
items.push( me.createLegendItem(series, j) );
};
}
};
me.alignItems();
},
/**
* @private Removes all legend items.
*/
removeItems: function() {
var me = this,
items = me.items,
len = items ? items.length : 0;
if (len) {
for (var i = 0; i < len; i++) {
items[i].destroy();
}
};
//empty array
items.length = [];
},
/**
* @private
* Positions all items within Legend box.
*/
alignItems: function() {
var me = this,
items = me.items,
padding = me.padding,
spacingOffset = 2,
vertical = me.isVertical,
math = Math,
mfloor = math.floor,
mmax = math.max,
dim;
dim = me.updateItemDimensions();
var maxWidth = dim.maxWidth,
maxHeight = dim.maxHeight,
totalWidth = dim.totalWidth,
totalHeight = dim.totalHeight,
spacing = dim.spacing;
// Store the collected dimensions for later
me.width = mfloor((vertical ? maxWidth : totalWidth) + padding * 2);
if (vertical && items.length === 1) {
spacingOffset = 1;
}
me.height = mfloor((vertical ? totalHeight - spacingOffset * spacing : maxHeight) + (padding * 2));
me.itemHeight = maxHeight;
},
updateItemDimensions: function() {
var me = this,
items = me.items,
padding = me.padding,
itemSpacing = me.itemSpacing,
maxWidth = 0,
maxHeight = 0,
totalWidth = 0,
totalHeight = 0,
vertical = me.isVertical,
math = Math,
mfloor = math.floor,
mmax = math.max,
spacing = 0;
// Collect item dimensions and position each one
// properly in relation to the previous item
for ( var i = 0, l = items.length; i < l; i++ ) {
var item = items[i],
bbox, width, height;
bbox = item.getBBox();
//always measure from x=0, since not all markers go all the way to the left
width = bbox.width;
height = bbox.height;
if (i === 0) {
spacing = vertical ? padding + height / 2 : padding;
}
else {
spacing = itemSpacing / (vertical ? 2 : 1);
}
// Set the item's position relative to the legend box
item.x = mfloor(vertical ? padding : totalWidth + spacing);
item.y = mfloor(vertical ? totalHeight + spacing : padding + height / 2);
// Collect cumulative dimensions
totalWidth += width + spacing;
totalHeight += height + spacing;
maxWidth = mmax(maxWidth, width);
maxHeight = mmax(maxHeight, height);
};
return {
totalWidth: totalWidth,
totalHeight: totalHeight,
maxWidth: maxWidth,
maxHeight: maxHeight,
spacing: spacing
};
},
/**
* @private Calculates Legend position with respect to other Chart elements.
*/
calcPosition: function() {
var me = this,
x, y,
legendWidth = me.width,
legendHeight = me.height,
padding = me.padding,
chart = me.chart,
chartBBox = chart.chartBBox,
insets = chart.insetPadding,
chartWidth = chartBBox.width - (insets * 2),
chartHeight = chartBBox.height - (insets * 2),
chartX = chartBBox.x + insets,
chartY = chartBBox.y + insets,
surface = chart.surface,
mfloor = Math.floor;
// Find the position based on the dimensions
switch(me.position) {
case "left":
x = insets;
y = mfloor(chartY + chartHeight / 2 - legendHeight / 2);
break;
case "right":
x = mfloor(surface.width - legendWidth) - insets;
y = mfloor(chartY + chartHeight / 2 - legendHeight / 2);
break;
case "top":
x = mfloor(chartX + chartWidth / 2 - legendWidth / 2);
y = insets;
break;
case "bottom":
x = mfloor(chartX + chartWidth / 2 - legendWidth / 2);
y = mfloor(surface.height - legendHeight) - insets;
break;
default:
x = mfloor(me.origX) + insets;
y = mfloor(me.origY) + insets;
}
return { x: x, y: y };
},
/**
* @private Update the position of all the legend's sprites to match its current x/y values
*/
updatePosition: function() {
var me = this,
items = me.items;
if (me.isDisplayed()) {
// Find the position based on the dimensions
var pos = me.calcPosition();
me.x = pos.x;
me.y = pos.y;
// Update the position of each item
for ( var i = 0, l = items.length; i < l; i++ ) {
items[i].updatePosition();
};
// Update the position of the outer box
me.boxSprite.setAttributes(me.getBBox(), true);
}
},
/**
* @private Creates single Legend Item
*/
createLegendItem: function(series, yFieldIndex) {
var me = this;
return Ext.create('Ext.ux.chart.LegendItem', {
legend: me,
series: series,
surface: me.chart.surface,
yFieldIndex: yFieldIndex
});
}
});