-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Legend.ts
1898 lines (1639 loc) · 57.1 KB
/
Legend.ts
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* *
*
* (c) 2010-2021 Torstein Honsi
*
* License: www.highcharts.com/license
*
* !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
*
* */
'use strict';
/* *
*
* Imports
*
* */
import type AnimationOptions from '../Animation/AnimationOptions';
import type BBoxObject from '../Renderer/BBoxObject';
import type BubbleLegendItem from '../../Series/Bubble/BubbleLegendItem';
import type Chart from '../Chart/Chart';
import type ColorAxis from '../Axis/Color/ColorAxis';
import type CSSObject from '../Renderer/CSSObject';
import type FontMetricsObject from '../Renderer/FontMetricsObject';
import type { HTMLDOMElement } from '../Renderer/DOMElementType';
import type LegendLike from './LegendLike';
import type LegendOptions from './LegendOptions';
import type Series from '../Series/Series';
import type { StatesOptionsKey } from '../Series/StatesOptions';
import type SVGAttributes from '../Renderer/SVG/SVGAttributes';
import type SVGElement from '../Renderer/SVG/SVGElement';
import A from '../Animation/AnimationUtilities.js';
const {
animObject,
setAnimation
} = A;
import F from '../Templating.js';
const { format } = F;
import H from '../Globals.js';
const {
marginNames
} = H;
import Point from '../Series/Point.js';
import R from '../Renderer/RendererUtilities.js';
const { distribute } = R;
import U from '../Utilities.js';
const {
addEvent,
createElement,
css,
defined,
discardElement,
find,
fireEvent,
isNumber,
merge,
pick,
relativeLength,
stableSort,
syncTimeout
} = U;
/* *
*
* Declarations
*
* */
declare module '../Chart/ChartLike' {
interface ChartLike {
legend: Legend;
}
}
declare module '../Series/SeriesOptions' {
interface SeriesOptions {
legendIndex?: number;
legendType?: ('point'|'series');
showCheckbox?: boolean;
showInLegend?: boolean;
}
}
interface BoxObject extends R.BoxObject {
item: Legend.Item;
}
/* *
*
* Class
*
* */
/**
* The overview of the chart's series. The legend object is instanciated
* internally in the chart constructor, and is available from the `chart.legend`
* property. Each chart has only one legend.
*
* @class
* @name Highcharts.Legend
*
* @param {Highcharts.Chart} chart
* The chart instance.
*
* @param {Highcharts.LegendOptions} options
* Legend options.
*/
class Legend {
/* *
*
* Constructors
*
* */
public constructor(chart: Chart, options: LegendOptions) {
this.chart = chart;
this.init(chart, options);
}
/* *
*
* Properties
*
* */
public allItems: Array<Legend.Item> = [];
public baseline?: number;
public box: SVGElement = void 0 as any;
public chart: Chart;
public clipHeight?: number;
public clipRect?: SVGElement;
public contentGroup: SVGElement = void 0 as any;
public currentPage?: number;
public display: boolean = false;
public down?: SVGElement;
public downTracker?: SVGElement;
public fontMetrics?: FontMetricsObject;
public fullHeight?: number;
public group: SVGElement = void 0 as any;
public initialItemY: number = 0;
public itemHeight: number = 0;
public itemHiddenStyle?: CSSObject;
public itemMarginBottom: number = 0;
public itemMarginTop: number = 0;
public itemStyle?: CSSObject;
public itemX: number = 0;
public itemY: number = 0;
public lastItemY: number = 0;
public lastLineHeight: number = 0;
public legendHeight: number = 0;
public legendWidth: number = 0;
public maxItemWidth: number = 0;
public maxLegendWidth: number = 0;
public nav?: SVGElement;
public offsetWidth: number = 0;
public options: LegendOptions = void 0 as any;
public padding: number = 0;
public pager?: SVGElement;
public pages: Array<number> = [];
public proximate: boolean = false;
public scrollGroup: SVGElement = void 0 as any;
public scrollOffset?: number;
public symbolHeight: number = 0;
public symbolWidth: number = 0;
public title?: SVGElement;
public titleHeight: number = 0;
public totalItemWidth: number = 0;
public up?: SVGElement;
public upTracker?: SVGElement;
public widthOption: number = 0;
/* *
*
* Functions
*
* */
/**
* Initialize the legend.
*
* @private
* @function Highcharts.Legend#init
*
* @param {Highcharts.Chart} chart
* The chart instance.
*
* @param {Highcharts.LegendOptions} options
* Legend options.
*/
public init(chart: Chart, options: LegendOptions): void {
/**
* Chart of this legend.
*
* @readonly
* @name Highcharts.Legend#chart
* @type {Highcharts.Chart}
*/
this.chart = chart;
this.setOptions(options);
const positionProximate = (): void => {
if (this.options.enabled && this.proximate) {
this.proximatePositions();
this.positionItems();
}
};
if (options.enabled) {
// Render it
this.render();
// Move checkboxes
addEvent(this.chart, 'endResize', function (): void {
this.legend.positionCheckboxes();
});
// On Legend.init and Legend.update, make sure that proximate layout
// events are either added or removed (#18362).
addEvent(this.chart, 'render', positionProximate);
}
// Need to position the legend after update, #19078.
addEvent(this.chart, 'redraw', positionProximate);
}
/**
* @private
* @function Highcharts.Legend#setOptions
* @param {Highcharts.LegendOptions} options
*/
public setOptions(options: LegendOptions): void {
const padding = pick(options.padding, 8) as number;
/**
* Legend options.
*
* @readonly
* @name Highcharts.Legend#options
* @type {Highcharts.LegendOptions}
*/
this.options = options;
if (!this.chart.styledMode) {
this.itemStyle = options.itemStyle;
this.itemHiddenStyle = merge(
this.itemStyle,
options.itemHiddenStyle
);
}
this.itemMarginTop = options.itemMarginTop;
this.itemMarginBottom = options.itemMarginBottom;
this.padding = padding;
this.initialItemY = padding - 5; // 5 is pixels above the text
this.symbolWidth = pick(options.symbolWidth, 16);
this.pages = [];
this.proximate = options.layout === 'proximate' && !this.chart.inverted;
// #12705: baseline has to be reset on every update
this.baseline = void 0;
}
/**
* Update the legend with new options. Equivalent to running `chart.update`
* with a legend configuration option.
*
* @sample highcharts/legend/legend-update/
* Legend update
*
* @function Highcharts.Legend#update
*
* @param {Highcharts.LegendOptions} options
* Legend options.
*
* @param {boolean} [redraw=true]
* Whether to redraw the chart after the axis is altered. If doing more
* operations on the chart, it is a good idea to set redraw to false and
* call {@link Chart#redraw} after. Whether to redraw the chart.
*
* @emits Highcharts.Legends#event:afterUpdate
*/
public update(options: DeepPartial<LegendOptions>, redraw?: boolean): void {
const chart = this.chart;
this.setOptions(merge(true, this.options, options));
this.destroy();
chart.isDirtyLegend = chart.isDirtyBox = true;
if (pick(redraw, true)) {
chart.redraw();
}
fireEvent(this, 'afterUpdate');
}
/**
* Set the colors for the legend item.
*
* @private
* @function Highcharts.Legend#colorizeItem
* @param {Highcharts.BubbleLegendItem|Highcharts.Point|Highcharts.Series} item
* A Series or Point instance
* @param {boolean} [visible=false]
* Dimmed or colored
*
* @todo
* Make events official: Fires the event `afterColorizeItem`.
*/
public colorizeItem(
item: Legend.Item,
visible?: boolean
): void {
const { group, label, line, symbol } = item.legendItem || {};
if (group) {
group[visible ? 'removeClass' : 'addClass'](
'highcharts-legend-item-hidden'
);
}
if (!this.chart.styledMode) {
const { itemHiddenStyle } = this,
hiddenColor = (itemHiddenStyle as any).color,
symbolColor = visible ?
(item.color || hiddenColor) :
hiddenColor,
markerOptions = item.options && (item.options as any).marker;
let symbolAttr: SVGAttributes = { fill: symbolColor };
label?.css(merge(visible ? this.itemStyle : itemHiddenStyle));
line?.attr({ stroke: symbolColor });
if (symbol) {
// Apply marker options
if (markerOptions && symbol.isMarker) { // #585
symbolAttr = (item as any).pointAttribs();
if (!visible) {
// #6769
symbolAttr.stroke = symbolAttr.fill = hiddenColor;
}
}
symbol.attr(symbolAttr);
}
}
fireEvent(this, 'afterColorizeItem', { item, visible });
}
/**
* @private
* @function Highcharts.Legend#positionItems
*/
public positionItems(): void {
// Now that the legend width and height are established, put the items
// in the final position
this.allItems.forEach(this.positionItem, this);
if (!this.chart.isResizing) {
this.positionCheckboxes();
}
}
/**
* Position the legend item.
*
* @private
* @function Highcharts.Legend#positionItem
* @param {Highcharts.BubbleLegendItem|Highcharts.Point|Highcharts.Series} item
* The item to position
*/
public positionItem(
item: Legend.Item
): void {
const legend = this,
{ group, x = 0, y = 0 } = item.legendItem || {},
options = legend.options,
symbolPadding = options.symbolPadding,
ltr = !options.rtl,
checkbox = item.checkbox;
if (group && group.element) {
const attribs = {
translateX: ltr ?
x :
legend.legendWidth - x - 2 * symbolPadding - 4,
translateY: y
};
const complete = (): void => {
fireEvent(this, 'afterPositionItem', { item });
};
group[defined(group.translateY) ? 'animate' : 'attr'](
attribs, void 0, complete
);
}
if (checkbox) {
checkbox.x = x;
checkbox.y = y;
}
}
/**
* Destroy a single legend item, used internally on removing series items.
*
* @private
* @function Highcharts.Legend#destroyItem
* @param {Highcharts.BubbleLegendItem|Highcharts.Point|Highcharts.Series} item
* The item to remove
*/
public destroyItem(
item: (
BubbleLegendItem|ColorAxis|ColorAxis.LegendItemObject|
Series|Point
)
): void {
const checkbox = item.checkbox,
legendItem = item.legendItem || {};
// destroy SVG elements
for (const key of ['group', 'label', 'line', 'symbol'] as const) {
if (legendItem[key]) {
legendItem[key] = legendItem[key].destroy();
}
}
if (checkbox) {
discardElement(checkbox);
}
item.legendItem = void 0;
}
/**
* Destroy the legend. Used internally. To reflow objects, `chart.redraw`
* must be called after destruction.
*
* @private
* @function Highcharts.Legend#destroy
*/
public destroy(): void {
const legend: AnyRecord = this;
// Destroy items
for (const item of this.getAllItems()) {
this.destroyItem(item);
}
// Destroy legend elements
for (const key of [
'clipRect',
'up',
'down',
'pager',
'nav',
'box',
'title',
'group'
] as const) {
if (legend[key]) {
legend[key] = legend[key].destroy();
}
}
this.display = null as any; // Reset in .render on update.
}
/**
* Position the checkboxes after the width is determined.
*
* @private
* @function Highcharts.Legend#positionCheckboxes
*/
public positionCheckboxes(): void {
const alignAttr = this.group && this.group.alignAttr,
clipHeight = this.clipHeight || this.legendHeight,
titleHeight = this.titleHeight;
let translateY: number;
if (alignAttr) {
translateY = alignAttr.translateY;
this.allItems.forEach(function (item): void {
const checkbox = item.checkbox;
let top;
if (checkbox) {
top = translateY + titleHeight + checkbox.y +
(this.scrollOffset || 0) + 3;
css(checkbox, {
left: (alignAttr.translateX + item.checkboxOffset +
checkbox.x - 20) + 'px',
top: top + 'px',
display: this.proximate || (
top > translateY - 6 &&
top < translateY + clipHeight - 6
) ?
'' :
'none'
});
}
}, this);
}
}
/**
* Render the legend title on top of the legend.
*
* @private
* @function Highcharts.Legend#renderTitle
*/
public renderTitle(): void {
const options = this.options,
padding = this.padding,
titleOptions = options.title;
let bBox,
titleHeight = 0;
if (titleOptions.text) {
if (!this.title) {
/**
* SVG element of the legend title.
*
* @readonly
* @name Highcharts.Legend#title
* @type {Highcharts.SVGElement}
*/
this.title = this.chart.renderer.label(
titleOptions.text,
padding - 3,
padding - 4,
void 0,
void 0,
void 0,
options.useHTML,
void 0,
'legend-title'
)
.attr({ zIndex: 1 });
if (!this.chart.styledMode) {
this.title.css(titleOptions.style);
}
this.title.add(this.group);
}
// Set the max title width (#7253)
if (!titleOptions.width) {
this.title.css({
width: this.maxLegendWidth + 'px'
});
}
bBox = this.title.getBBox();
titleHeight = bBox.height;
this.offsetWidth = bBox.width; // #1717
this.contentGroup.attr({ translateY: titleHeight });
}
this.titleHeight = titleHeight;
}
/**
* Set the legend item text.
*
* @function Highcharts.Legend#setText
* @param {Highcharts.Point|Highcharts.Series} item
* The item for which to update the text in the legend.
*/
public setText(
item: Legend.Item
): void {
const options = this.options;
(item.legendItem as any).label.attr({
text: options.labelFormat ?
format(options.labelFormat, item, this.chart) :
options.labelFormatter.call(item)
});
}
/**
* Render a single specific legend item. Called internally from the `render`
* function.
*
* @private
* @function Highcharts.Legend#renderItem
* @param {Highcharts.BubbleLegendItem|Highcharts.Point|Highcharts.Series} item
* The item to render.
*/
public renderItem(
item: Legend.Item
): void {
const legend = this,
legendItem = item.legendItem = item.legendItem || {},
chart = legend.chart,
renderer = chart.renderer,
options = legend.options,
horizontal = options.layout === 'horizontal',
symbolWidth = legend.symbolWidth,
symbolPadding = options.symbolPadding || 0,
itemStyle = legend.itemStyle,
itemHiddenStyle = legend.itemHiddenStyle,
itemDistance = horizontal ? pick(options.itemDistance, 20) : 0,
ltr = !options.rtl,
isSeries = !(item as any).series,
series = !isSeries && (item as any).series.drawLegendSymbol ?
(item as any).series :
item,
seriesOptions = series.options,
showCheckbox = (legend.createCheckboxForItem) &&
seriesOptions &&
seriesOptions.showCheckbox,
useHTML = options.useHTML,
itemClassName = item.options.className;
let label = legendItem.label,
// full width minus text width
itemExtraWidth = symbolWidth + symbolPadding +
itemDistance + (showCheckbox ? 20 : 0);
if (!label) { // generate it once, later move it
// Generate the group box, a group to hold the symbol and text. Text
// is to be appended in Legend class.
legendItem.group = renderer
.g('legend-item')
.addClass(
'highcharts-' + series.type + '-series ' +
'highcharts-color-' + (item as any).colorIndex +
(itemClassName ? ' ' + itemClassName : '') +
(
isSeries ?
' highcharts-series-' + (item as any).index :
''
)
)
.attr({ zIndex: 1 })
.add(legend.scrollGroup);
// Generate the list item text and add it to the group
legendItem.label = label = renderer.text(
'',
ltr ?
symbolWidth + symbolPadding :
-symbolPadding,
legend.baseline || 0,
useHTML
);
if (!chart.styledMode) {
// merge to prevent modifying original (#1021)
label.css(merge(
item.visible ?
itemStyle :
itemHiddenStyle
));
}
label
.attr({
align: ltr ? 'left' : 'right',
zIndex: 2
})
.add(legendItem.group);
// Get the baseline for the first item - the font size is equal for
// all
if (!legend.baseline) {
legend.fontMetrics = renderer.fontMetrics(label);
legend.baseline =
legend.fontMetrics.f + 3 + legend.itemMarginTop;
label.attr('y', legend.baseline);
legend.symbolHeight =
pick(options.symbolHeight, legend.fontMetrics.f);
if (options.squareSymbol) {
legend.symbolWidth = pick(
options.symbolWidth,
Math.max(legend.symbolHeight, 16)
);
itemExtraWidth = legend.symbolWidth + symbolPadding +
itemDistance + (showCheckbox ? 20 : 0);
if (ltr) {
label.attr('x', legend.symbolWidth + symbolPadding);
}
}
}
// Draw the legend symbol inside the group box
series.drawLegendSymbol(legend, item);
if (legend.setItemEvents) {
legend.setItemEvents(item, label, useHTML);
}
}
// Add the HTML checkbox on top
if (showCheckbox && !item.checkbox && legend.createCheckboxForItem) {
legend.createCheckboxForItem(item);
}
// Colorize the items
legend.colorizeItem(item, item.visible);
// Take care of max width and text overflow (#6659)
if (chart.styledMode || !(itemStyle as any).width) {
label.css({
width: ((
options.itemWidth ||
legend.widthOption ||
chart.spacingBox.width
) - itemExtraWidth) + 'px'
});
}
// Always update the text
legend.setText(item as Legend.Item);
// calculate the positions for the next line
const bBox = label.getBBox();
const fontMetricsH = (legend.fontMetrics && legend.fontMetrics.h) || 0;
item.itemWidth = item.checkboxOffset =
options.itemWidth ||
legendItem.labelWidth ||
bBox.width + itemExtraWidth;
legend.maxItemWidth = Math.max(
legend.maxItemWidth, (item.itemWidth as any)
);
legend.totalItemWidth += item.itemWidth as any;
legend.itemHeight = item.itemHeight = Math.round(
legendItem.labelHeight ||
// use bBox for multiline (#16398)
(bBox.height > fontMetricsH * 1.5 ? bBox.height : fontMetricsH)
);
}
/**
* Get the position of the item in the layout. We now know the
* maxItemWidth from the previous loop.
*
* @private
* @function Highcharts.Legend#layoutItem
* @param {Highcharts.BubbleLegendItem|Highcharts.Point|Highcharts.Series} item
*/
public layoutItem(
item: Legend.Item
): void {
const options = this.options,
padding = this.padding,
horizontal = options.layout === 'horizontal',
itemHeight = item.itemHeight,
itemMarginBottom = this.itemMarginBottom,
itemMarginTop = this.itemMarginTop,
itemDistance = horizontal ? pick(options.itemDistance, 20) : 0,
maxLegendWidth = this.maxLegendWidth,
itemWidth = (
options.alignColumns &&
this.totalItemWidth > maxLegendWidth
) ?
this.maxItemWidth :
item.itemWidth,
legendItem = item.legendItem || {};
// If the item exceeds the width, start a new line
if (
horizontal &&
this.itemX - padding + (itemWidth as any) > maxLegendWidth
) {
this.itemX = padding;
if (this.lastLineHeight) { // Not for the first line (#10167)
this.itemY += (
itemMarginTop +
this.lastLineHeight +
itemMarginBottom
);
}
this.lastLineHeight = 0; // reset for next line (#915, #3976)
}
// Set the edge positions
this.lastItemY = itemMarginTop + this.itemY + itemMarginBottom;
this.lastLineHeight = Math.max( // #915
itemHeight as any,
this.lastLineHeight
);
// cache the position of the newly generated or reordered items
legendItem.x = this.itemX;
legendItem.y = this.itemY;
// advance
if (horizontal) {
this.itemX += (itemWidth as any);
} else {
this.itemY +=
itemMarginTop + (itemHeight as any) + itemMarginBottom;
this.lastLineHeight = itemHeight as any;
}
// the width of the widest item
this.offsetWidth = this.widthOption || Math.max(
(
horizontal ? this.itemX - padding - (item.checkbox ?
// decrease by itemDistance only when no checkbox #4853
0 :
itemDistance
) : itemWidth as any
) + padding,
this.offsetWidth
);
}
/**
* Get all items, which is one item per series for most series and one
* item per point for pie series and its derivatives. Fires the event
* `afterGetAllItems`.
*
* @private
* @function Highcharts.Legend#getAllItems
* @return {Array<(Highcharts.BubbleLegendItem|Highcharts.Point|Highcharts.Series)>}
* The current items in the legend.
* @emits Highcharts.Legend#event:afterGetAllItems
*/
public getAllItems(): Array<Legend.Item> {
let allItems: Array<Legend.Item> = [];
this.chart.series.forEach(function (series): void {
const seriesOptions = series && series.options;
// Handle showInLegend. If the series is linked to another series,
// defaults to false.
if (series && pick(
seriesOptions.showInLegend,
!defined(seriesOptions.linkedTo) ? void 0 : false, true
)) {
// Use points or series for the legend item depending on
// legendType
allItems = allItems.concat(
(series.legendItem || {}).labels as any ||
(
seriesOptions.legendType === 'point' ?
series.data :
series
)
);
}
});
fireEvent(this, 'afterGetAllItems', { allItems });
return allItems;
}
/**
* Get a short, three letter string reflecting the alignment and layout.
*
* @private
* @function Highcharts.Legend#getAlignment
* @return {string}
* The alignment, empty string if floating
*/
public getAlignment(): string {
const options = this.options;
// Use the first letter of each alignment option in order to detect
// the side. (#4189 - use charAt(x) notation instead of [x] for IE7)
if (this.proximate) {
return options.align.charAt(0) + 'tv';
}
return options.floating ? '' : (
options.align.charAt(0) +
options.verticalAlign.charAt(0) +
options.layout.charAt(0)
);
}
/**
* Adjust the chart margins by reserving space for the legend on only one
* side of the chart. If the position is set to a corner, top or bottom is
* reserved for horizontal legends and left or right for vertical ones.
*
* @private
* @function Highcharts.Legend#adjustMargins
* @param {Array<number>} margin
* @param {Array<number>} spacing
*/
public adjustMargins(margin: Array<number>, spacing: Array<number>): void {
const chart = this.chart,
options = this.options,
alignment = this.getAlignment();
if (alignment) {
([
/(lth|ct|rth)/,
/(rtv|rm|rbv)/,
/(rbh|cb|lbh)/,
/(lbv|lm|ltv)/
]).forEach(function (alignments: RegExp, side: number): void {
if (alignments.test(alignment) && !defined(margin[side])) {
// Now we have detected on which side of the chart we should
// reserve space for the legend
(chart as any)[marginNames[side]] = Math.max(
(chart as any)[marginNames[side]],
(
chart.legend[
(side + 1) % 2 ? 'legendHeight' : 'legendWidth'
] +
[1, -1, -1, 1][side] * (options[
(side % 2) ? 'x' : 'y'
] as any) +
pick(options.margin as any, 12) +
spacing[side] +
(chart.titleOffset[side] || 0)
)
);
}
});
}
}
/**
* @private
* @function Highcharts.Legend#proximatePositions
*/
public proximatePositions(): void {
const chart = this.chart,
boxes: Array<BoxObject> = [],
alignLeft = this.options.align === 'left';
this.allItems.forEach(function (item): void {
let lastPoint: (Point|undefined),
height: number,
useFirstPoint = alignLeft,
target,
top;
if ((item as any).yAxis) {