-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathmodel.ts
1096 lines (996 loc) · 36.4 KB
/
model.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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import {
each,
isObject,
isArray,
createHashMap,
HashMap,
map,
assert,
isString,
indexOf,
isStringSafe,
isNumber
} from 'zrender/src/core/util';
import env from 'zrender/src/core/env';
import GlobalModel from '../model/Global';
import ComponentModel, {ComponentModelConstructor} from '../model/Component';
import SeriesData from '../data/SeriesData';
import {
ComponentOption,
ComponentMainType,
ComponentSubType,
DisplayStateHostOption,
OptionDataItem,
OptionDataValue,
TooltipRenderMode,
Payload,
OptionId,
OptionName,
InterpolatableValue
} from './types';
import { Dictionary } from 'zrender/src/core/types';
import SeriesModel from '../model/Series';
import CartesianAxisModel from '../coord/cartesian/AxisModel';
import GridModel from '../coord/cartesian/GridModel';
import { isNumeric, getRandomIdBase, getPrecision, round } from './number';
import { warn } from './log';
function interpolateNumber(p0: number, p1: number, percent: number): number {
return (p1 - p0) * percent + p0;
}
/**
* Make the name displayable. But we should
* make sure it is not duplicated with user
* specified name, so use '\0';
*/
const DUMMY_COMPONENT_NAME_PREFIX = 'series\0';
const INTERNAL_COMPONENT_ID_PREFIX = '\0_ec_\0';
/**
* If value is not array, then translate it to array.
* @param {*} value
* @return {Array} [value] or value
*/
export function normalizeToArray<T>(value?: T | T[]): T[] {
return value instanceof Array
? value
: value == null
? []
: [value];
}
/**
* Sync default option between normal and emphasis like `position` and `show`
* In case some one will write code like
* label: {
* show: false,
* position: 'outside',
* fontSize: 18
* },
* emphasis: {
* label: { show: true }
* }
*/
export function defaultEmphasis(
opt: DisplayStateHostOption,
key: string,
subOpts: string[]
): void {
// Caution: performance sensitive.
if (opt) {
opt[key] = opt[key] || {};
opt.emphasis = opt.emphasis || {};
opt.emphasis[key] = opt.emphasis[key] || {};
// Default emphasis option from normal
for (let i = 0, len = subOpts.length; i < len; i++) {
const subOptName = subOpts[i];
if (!opt.emphasis[key].hasOwnProperty(subOptName)
&& opt[key].hasOwnProperty(subOptName)
) {
opt.emphasis[key][subOptName] = opt[key][subOptName];
}
}
}
}
export const TEXT_STYLE_OPTIONS = [
'fontStyle', 'fontWeight', 'fontSize', 'fontFamily',
'rich', 'tag', 'color', 'textBorderColor', 'textBorderWidth',
'width', 'height', 'lineHeight', 'align', 'verticalAlign', 'baseline',
'shadowColor', 'shadowBlur', 'shadowOffsetX', 'shadowOffsetY',
'textShadowColor', 'textShadowBlur', 'textShadowOffsetX', 'textShadowOffsetY',
'backgroundColor', 'borderColor', 'borderWidth', 'borderRadius', 'padding'
] as const;
// modelUtil.LABEL_OPTIONS = modelUtil.TEXT_STYLE_OPTIONS.concat([
// 'position', 'offset', 'rotate', 'origin', 'show', 'distance', 'formatter',
// 'fontStyle', 'fontWeight', 'fontSize', 'fontFamily',
// // FIXME: deprecated, check and remove it.
// 'textStyle'
// ]);
/**
* The method does not ensure performance.
* data could be [12, 2323, {value: 223}, [1221, 23], {value: [2, 23]}]
* This helper method retrieves value from data.
*/
export function getDataItemValue(
dataItem: OptionDataItem
): OptionDataValue | OptionDataValue[] {
return (isObject(dataItem) && !isArray(dataItem) && !(dataItem instanceof Date))
? (dataItem as Dictionary<OptionDataValue>).value : dataItem;
}
/**
* data could be [12, 2323, {value: 223}, [1221, 23], {value: [2, 23]}]
* This helper method determine if dataItem has extra option besides value
*/
export function isDataItemOption(dataItem: OptionDataItem): boolean {
return isObject(dataItem)
&& !(dataItem instanceof Array);
// // markLine data can be array
// && !(dataItem[0] && isObject(dataItem[0]) && !(dataItem[0] instanceof Array));
}
// Compatible with previous definition: id could be number (but not recommended).
// number and string are treated the same when compared.
// number id will not be converted to string in option.
// number id will be converted to string in component instance id.
export interface MappingExistingItem {
id?: OptionId;
name?: string;
};
/**
* The array `MappingResult<T>[]` exactly represents the content of the result
* components array after merge.
* The indices are the same as the `existings`.
* Items will not be `null`/`undefined` even if the corresponding `existings` will be removed.
*/
type MappingResult<T> = MappingResultItem<T>[];
interface MappingResultItem<T extends MappingExistingItem = MappingExistingItem> {
// Existing component instance.
existing: T;
// The mapped new component option.
newOption: ComponentOption;
// Mark that the new component has nothing to do with any of the old components.
// So they won't share view. Also see `__requireNewView`.
brandNew: boolean;
// keyInfo for new component.
// All of them will be assigned to a created component instance.
keyInfo: {
name: string,
id: string,
mainType: ComponentMainType,
subType: ComponentSubType
};
}
type MappingToExistsMode = 'normalMerge' | 'replaceMerge' | 'replaceAll';
/**
* Mapping to existings for merge.
*
* Mode "normalMege":
* The mapping result (merge result) will keep the order of the existing
* component, rather than the order of new option. Because we should ensure
* some specified index reference (like xAxisIndex) keep work.
* And in most cases, "merge option" is used to update partial option but not
* be expected to change the order.
*
* Mode "replaceMege":
* (1) Only the id mapped components will be merged.
* (2) Other existing components (except internal components) will be removed.
* (3) Other new options will be used to create new component.
* (4) The index of the existing components will not be modified.
* That means their might be "hole" after the removal.
* The new components are created first at those available index.
*
* Mode "replaceAll":
* This mode try to support that reproduce an echarts instance from another
* echarts instance (via `getOption`) in some simple cases.
* In this scenario, the `result` index are exactly the consistent with the `newCmptOptions`,
* which ensures the component index referring (like `xAxisIndex: ?`) corrent. That is,
* the "hole" in `newCmptOptions` will also be kept.
* On the contrary, other modes try best to eliminate holes.
* PENDING: This is an experimental mode yet.
*
* @return See the comment of <MappingResult>.
*/
export function mappingToExists<T extends MappingExistingItem>(
existings: T[],
newCmptOptions: ComponentOption[],
mode: MappingToExistsMode
): MappingResult<T> {
const isNormalMergeMode = mode === 'normalMerge';
const isReplaceMergeMode = mode === 'replaceMerge';
const isReplaceAllMode = mode === 'replaceAll';
existings = existings || [];
newCmptOptions = (newCmptOptions || []).slice();
const existingIdIdxMap = createHashMap<number>();
// Validate id and name on user input option.
each(newCmptOptions, function (cmptOption, index) {
if (!isObject<ComponentOption>(cmptOption)) {
newCmptOptions[index] = null;
return;
}
if (__DEV__) {
// There is some legacy case that name is set as `false`.
// But should work normally rather than throw error.
if (cmptOption.id != null && !isValidIdOrName(cmptOption.id)) {
warnInvalidateIdOrName(cmptOption.id);
}
if (cmptOption.name != null && !isValidIdOrName(cmptOption.name)) {
warnInvalidateIdOrName(cmptOption.name);
}
}
});
const result = prepareResult(existings, existingIdIdxMap, mode);
if (isNormalMergeMode || isReplaceMergeMode) {
mappingById(result, existings, existingIdIdxMap, newCmptOptions);
}
if (isNormalMergeMode) {
mappingByName(result, newCmptOptions);
}
if (isNormalMergeMode || isReplaceMergeMode) {
mappingByIndex(result, newCmptOptions, isReplaceMergeMode);
}
else if (isReplaceAllMode) {
mappingInReplaceAllMode(result, newCmptOptions);
}
makeIdAndName(result);
// The array `result` MUST NOT contain elided items, otherwise the
// forEach will omit those items and result in incorrect result.
return result;
}
function prepareResult<T extends MappingExistingItem>(
existings: T[],
existingIdIdxMap: HashMap<number>,
mode: MappingToExistsMode
): MappingResultItem<T>[] {
const result: MappingResultItem<T>[] = [];
if (mode === 'replaceAll') {
return result;
}
// Do not use native `map` to in case that the array `existings`
// contains elided items, which will be omitted.
for (let index = 0; index < existings.length; index++) {
const existing = existings[index];
// Because of replaceMerge, `existing` may be null/undefined.
if (existing && existing.id != null) {
existingIdIdxMap.set(existing.id, index);
}
// For non-internal-componnets:
// Mode "normalMerge": all existings kept.
// Mode "replaceMerge": all existing removed unless mapped by id.
// For internal-components:
// go with "replaceMerge" approach in both mode.
result.push({
existing: (mode === 'replaceMerge' || isComponentIdInternal(existing))
? null
: existing,
newOption: null,
keyInfo: null,
brandNew: null
});
}
return result;
}
function mappingById<T extends MappingExistingItem>(
result: MappingResult<T>,
existings: T[],
existingIdIdxMap: HashMap<number>,
newCmptOptions: ComponentOption[]
): void {
// Mapping by id if specified.
each(newCmptOptions, function (cmptOption, index) {
if (!cmptOption || cmptOption.id == null) {
return;
}
const optionId = makeComparableKey(cmptOption.id);
const existingIdx = existingIdIdxMap.get(optionId);
if (existingIdx != null) {
const resultItem = result[existingIdx];
assert(
!resultItem.newOption,
'Duplicated option on id "' + optionId + '".'
);
resultItem.newOption = cmptOption;
// In both mode, if id matched, new option will be merged to
// the existings rather than creating new component model.
resultItem.existing = existings[existingIdx];
newCmptOptions[index] = null;
}
});
}
function mappingByName<T extends MappingExistingItem>(
result: MappingResult<T>,
newCmptOptions: ComponentOption[]
): void {
// Mapping by name if specified.
each(newCmptOptions, function (cmptOption, index) {
if (!cmptOption || cmptOption.name == null) {
return;
}
for (let i = 0; i < result.length; i++) {
const existing = result[i].existing;
if (!result[i].newOption // Consider name: two map to one.
// Can not match when both ids existing but different.
&& existing
&& (existing.id == null || cmptOption.id == null)
&& !isComponentIdInternal(cmptOption)
&& !isComponentIdInternal(existing)
&& keyExistAndEqual('name', existing, cmptOption)
) {
result[i].newOption = cmptOption;
newCmptOptions[index] = null;
return;
}
}
});
}
function mappingByIndex<T extends MappingExistingItem>(
result: MappingResult<T>,
newCmptOptions: ComponentOption[],
brandNew: boolean
): void {
each(newCmptOptions, function (cmptOption) {
if (!cmptOption) {
return;
}
// Find the first place that not mapped by id and not internal component (consider the "hole").
let resultItem;
let nextIdx = 0;
while (
// Be `!resultItem` only when `nextIdx >= result.length`.
(resultItem = result[nextIdx])
// (1) Existing models that already have id should be able to mapped to. Because
// after mapping performed, model will always be assigned with an id if user not given.
// After that all models have id.
// (2) If new option has id, it can only set to a hole or append to the last. It should
// not be merged to the existings with different id. Because id should not be overwritten.
// (3) Name can be overwritten, because axis use name as 'show label text'.
&& (
resultItem.newOption
|| isComponentIdInternal(resultItem.existing)
|| (
// In mode "replaceMerge", here no not-mapped-non-internal-existing.
resultItem.existing
&& cmptOption.id != null
&& !keyExistAndEqual('id', cmptOption, resultItem.existing)
)
)
) {
nextIdx++;
}
if (resultItem) {
resultItem.newOption = cmptOption;
resultItem.brandNew = brandNew;
}
else {
result.push({
newOption: cmptOption,
brandNew: brandNew,
existing: null,
keyInfo: null
});
}
nextIdx++;
});
}
function mappingInReplaceAllMode<T extends MappingExistingItem>(
result: MappingResult<T>,
newCmptOptions: ComponentOption[]
): void {
each(newCmptOptions, function (cmptOption) {
// The feature "reproduce" requires "hole" will also reproduced
// in case that component index referring are broken.
result.push({
newOption: cmptOption,
brandNew: true,
existing: null,
keyInfo: null
});
});
}
/**
* Make id and name for mapping result (result of mappingToExists)
* into `keyInfo` field.
*/
function makeIdAndName(
mapResult: MappingResult<MappingExistingItem>
): void {
// We use this id to hash component models and view instances
// in echarts. id can be specified by user, or auto generated.
// The id generation rule ensures new view instance are able
// to mapped to old instance when setOption are called in
// no-merge mode. So we generate model id by name and plus
// type in view id.
// name can be duplicated among components, which is convenient
// to specify multi components (like series) by one name.
// Ensure that each id is distinct.
const idMap = createHashMap();
each(mapResult, function (item) {
const existing = item.existing;
existing && idMap.set(existing.id, item);
});
each(mapResult, function (item) {
const opt = item.newOption;
// Force ensure id not duplicated.
assert(
!opt || opt.id == null || !idMap.get(opt.id) || idMap.get(opt.id) === item,
'id duplicates: ' + (opt && opt.id)
);
opt && opt.id != null && idMap.set(opt.id, item);
!item.keyInfo && (item.keyInfo = {} as MappingResultItem['keyInfo']);
});
// Make name and id.
each(mapResult, function (item, index) {
const existing = item.existing;
const opt = item.newOption;
const keyInfo = item.keyInfo;
if (!isObject<ComponentOption>(opt)) {
return;
}
// Name can be overwritten. Consider case: axis.name = '20km'.
// But id generated by name will not be changed, which affect
// only in that case: setOption with 'not merge mode' and view
// instance will be recreated, which can be accepted.
keyInfo.name = opt.name != null
? makeComparableKey(opt.name)
: existing
? existing.name
// Avoid that different series has the same name,
// because name may be used like in color pallet.
: DUMMY_COMPONENT_NAME_PREFIX + index;
if (existing) {
keyInfo.id = makeComparableKey(existing.id);
}
else if (opt.id != null) {
keyInfo.id = makeComparableKey(opt.id);
}
else {
// Consider this situatoin:
// optionA: [{name: 'a'}, {name: 'a'}, {..}]
// optionB [{..}, {name: 'a'}, {name: 'a'}]
// Series with the same name between optionA and optionB
// should be mapped.
let idNum = 0;
do {
keyInfo.id = '\0' + keyInfo.name + '\0' + idNum++;
}
while (idMap.get(keyInfo.id));
}
idMap.set(keyInfo.id, item);
});
}
function keyExistAndEqual(
attr: 'id' | 'name',
obj1: { id?: OptionId, name?: OptionName },
obj2: { id?: OptionId, name?: OptionName }
): boolean {
const key1 = convertOptionIdName(obj1[attr], null);
const key2 = convertOptionIdName(obj2[attr], null);
// See `MappingExistingItem`. `id` and `name` trade string equals to number.
return key1 != null && key2 != null && key1 === key2;
}
/**
* @return return null if not exist.
*/
function makeComparableKey(val: unknown): string {
if (__DEV__) {
if (val == null) {
throw new Error();
}
}
return convertOptionIdName(val, '');
}
export function convertOptionIdName(idOrName: unknown, defaultValue: string): string {
if (idOrName == null) {
return defaultValue;
}
return isString(idOrName)
? idOrName
: (isNumber(idOrName) || isStringSafe(idOrName))
? idOrName + ''
: defaultValue;
}
function warnInvalidateIdOrName(idOrName: unknown) {
if (__DEV__) {
warn('`' + idOrName + '` is invalid id or name. Must be a string or number.');
}
}
function isValidIdOrName(idOrName: unknown): boolean {
return isStringSafe(idOrName) || isNumeric(idOrName);
}
export function isNameSpecified(componentModel: ComponentModel): boolean {
const name = componentModel.name;
// Is specified when `indexOf` get -1 or > 0.
return !!(name && name.indexOf(DUMMY_COMPONENT_NAME_PREFIX));
}
/**
* @public
* @param {Object} cmptOption
* @return {boolean}
*/
export function isComponentIdInternal(cmptOption: { id?: MappingExistingItem['id'] }): boolean {
return cmptOption
&& cmptOption.id != null
&& makeComparableKey(cmptOption.id).indexOf(INTERNAL_COMPONENT_ID_PREFIX) === 0;
}
export function makeInternalComponentId(idSuffix: string) {
return INTERNAL_COMPONENT_ID_PREFIX + idSuffix;
}
export function setComponentTypeToKeyInfo(
mappingResult: MappingResult<MappingExistingItem & { subType?: ComponentSubType }>,
mainType: ComponentMainType,
componentModelCtor: ComponentModelConstructor
): void {
// Set mainType and complete subType.
each(mappingResult, function (item) {
const newOption = item.newOption;
if (isObject(newOption)) {
item.keyInfo.mainType = mainType;
item.keyInfo.subType = determineSubType(mainType, newOption, item.existing, componentModelCtor);
}
});
}
function determineSubType(
mainType: ComponentMainType,
newCmptOption: ComponentOption,
existComponent: { subType?: ComponentSubType },
componentModelCtor: ComponentModelConstructor
): ComponentSubType {
const subType = newCmptOption.type
? newCmptOption.type
: existComponent
? existComponent.subType
// Use determineSubType only when there is no existComponent.
: (componentModelCtor as ComponentModelConstructor).determineSubType(mainType, newCmptOption);
// tooltip, markline, markpoint may always has no subType
return subType;
}
type BatchItem = {
seriesId: OptionId,
dataIndex: number | number[]
};
/**
* A helper for removing duplicate items between batchA and batchB,
* and in themselves, and categorize by series.
*
* @param batchA Like: [{seriesId: 2, dataIndex: [32, 4, 5]}, ...]
* @param batchB Like: [{seriesId: 2, dataIndex: [32, 4, 5]}, ...]
* @return result: [resultBatchA, resultBatchB]
*/
export function compressBatches(
batchA: BatchItem[],
batchB: BatchItem[]
): [BatchItem[], BatchItem[]] {
type InnerMap = {
[seriesId: string]: {
[dataIndex: string]: 1
}
};
const mapA = {} as InnerMap;
const mapB = {} as InnerMap;
makeMap(batchA || [], mapA);
makeMap(batchB || [], mapB, mapA);
return [mapToArray(mapA), mapToArray(mapB)];
function makeMap(sourceBatch: BatchItem[], map: InnerMap, otherMap?: InnerMap): void {
for (let i = 0, len = sourceBatch.length; i < len; i++) {
const seriesId = convertOptionIdName(sourceBatch[i].seriesId, null);
if (seriesId == null) {
return;
}
const dataIndices = normalizeToArray(sourceBatch[i].dataIndex);
const otherDataIndices = otherMap && otherMap[seriesId];
for (let j = 0, lenj = dataIndices.length; j < lenj; j++) {
const dataIndex = dataIndices[j];
if (otherDataIndices && otherDataIndices[dataIndex]) {
otherDataIndices[dataIndex] = null;
}
else {
(map[seriesId] || (map[seriesId] = {}))[dataIndex] = 1;
}
}
}
}
function mapToArray(map: Dictionary<any>, isData?: boolean): any[] {
const result = [];
for (const i in map) {
if (map.hasOwnProperty(i) && map[i] != null) {
if (isData) {
result.push(+i);
}
else {
const dataIndices = mapToArray(map[i], true);
dataIndices.length && result.push({seriesId: i, dataIndex: dataIndices});
}
}
}
return result;
}
}
/**
* @param payload Contains dataIndex (means rawIndex) / dataIndexInside / name
* each of which can be Array or primary type.
* @return dataIndex If not found, return undefined/null.
*/
export function queryDataIndex(data: SeriesData, payload: Payload & {
dataIndexInside?: number | number[]
dataIndex?: number | number[]
name?: string | string[]
}): number | number[] {
if (payload.dataIndexInside != null) {
return payload.dataIndexInside;
}
else if (payload.dataIndex != null) {
return isArray(payload.dataIndex)
? map(payload.dataIndex, function (value) {
return data.indexOfRawIndex(value);
})
: data.indexOfRawIndex(payload.dataIndex);
}
else if (payload.name != null) {
return isArray(payload.name)
? map(payload.name, function (value) {
return data.indexOfName(value);
})
: data.indexOfName(payload.name);
}
}
/**
* Enable property storage to any host object.
* Notice: Serialization is not supported.
*
* For example:
* let inner = zrUitl.makeInner();
*
* function some1(hostObj) {
* inner(hostObj).someProperty = 1212;
* ...
* }
* function some2() {
* let fields = inner(this);
* fields.someProperty1 = 1212;
* fields.someProperty2 = 'xx';
* ...
* }
*
* @return {Function}
*/
export function makeInner<T, Host extends object>() {
const key = '__ec_inner_' + innerUniqueIndex++;
return function (hostObj: Host): T {
return (hostObj as any)[key] || ((hostObj as any)[key] = {});
};
}
let innerUniqueIndex = getRandomIdBase();
/**
* If string, e.g., 'geo', means {geoIndex: 0}.
* If Object, could contain some of these properties below:
* {
* seriesIndex, seriesId, seriesName,
* geoIndex, geoId, geoName,
* bmapIndex, bmapId, bmapName,
* xAxisIndex, xAxisId, xAxisName,
* yAxisIndex, yAxisId, yAxisName,
* gridIndex, gridId, gridName,
* ... (can be extended)
* }
* Each properties can be number|string|Array.<number>|Array.<string>
* For example, a finder could be
* {
* seriesIndex: 3,
* geoId: ['aa', 'cc'],
* gridName: ['xx', 'rr']
* }
* xxxIndex can be set as 'all' (means all xxx) or 'none' (means not specify)
* If nothing or null/undefined specified, return nothing.
* If both `abcIndex`, `abcId`, `abcName` specified, only one work.
* The priority is: index > id > name, the same with `ecModel.queryComponents`.
*/
export type ModelFinderIndexQuery = number | number[] | 'all' | 'none' | false;
export type ModelFinderIdQuery = OptionId | OptionId[];
export type ModelFinderNameQuery = OptionId | OptionId[];
// If string, like 'series', means { seriesIndex: 0 }.
export type ModelFinder = string | ModelFinderObject;
export type ModelFinderObject = {
seriesIndex?: ModelFinderIndexQuery, seriesId?: ModelFinderIdQuery, seriesName?: ModelFinderNameQuery
geoIndex?: ModelFinderIndexQuery, geoId?: ModelFinderIdQuery, geoName?: ModelFinderNameQuery
bmapIndex?: ModelFinderIndexQuery, bmapId?: ModelFinderIdQuery, bmapName?: ModelFinderNameQuery
xAxisIndex?: ModelFinderIndexQuery, xAxisId?: ModelFinderIdQuery, xAxisName?: ModelFinderNameQuery
yAxisIndex?: ModelFinderIndexQuery, yAxisId?: ModelFinderIdQuery, yAxisName?: ModelFinderNameQuery
gridIndex?: ModelFinderIndexQuery, gridId?: ModelFinderIdQuery, gridName?: ModelFinderNameQuery
dataIndex?: number, dataIndexInside?: number
// ... (can be extended)
};
/**
* {
* seriesModels: [seriesModel1, seriesModel2],
* seriesModel: seriesModel1, // The first model
* geoModels: [geoModel1, geoModel2],
* geoModel: geoModel1, // The first model
* ...
* }
*/
export type ParsedModelFinder = {
// other components
[key: string]: ComponentModel | ComponentModel[] | undefined;
};
export type ParsedModelFinderKnown = ParsedModelFinder & {
seriesModels?: SeriesModel[];
seriesModel?: SeriesModel;
xAxisModels?: CartesianAxisModel[];
xAxisModel?: CartesianAxisModel;
yAxisModels?: CartesianAxisModel[];
yAxisModel?: CartesianAxisModel;
gridModels?: GridModel[];
gridModel?: GridModel;
dataIndex?: number;
dataIndexInside?: number;
};
/**
* The same behavior as `component.getReferringComponents`.
*/
export function parseFinder(
ecModel: GlobalModel,
finderInput: ModelFinder,
opt?: {
// If no main type specified, use this main type.
defaultMainType?: ComponentMainType;
// If pervided, types out of this list will be ignored.
includeMainTypes?: ComponentMainType[];
enableAll?: boolean;
enableNone?: boolean;
}
): ParsedModelFinder {
const { mainTypeSpecified, queryOptionMap, others } = preParseFinder(finderInput, opt);
const result = others as ParsedModelFinderKnown;
const defaultMainType = opt ? opt.defaultMainType : null;
if (!mainTypeSpecified && defaultMainType) {
queryOptionMap.set(defaultMainType, {});
}
queryOptionMap.each(function (queryOption, mainType) {
const queryResult = queryReferringComponents(
ecModel,
mainType,
queryOption,
{
useDefault: defaultMainType === mainType,
enableAll: (opt && opt.enableAll != null) ? opt.enableAll : true,
enableNone: (opt && opt.enableNone != null) ? opt.enableNone : true
}
);
result[mainType + 'Models'] = queryResult.models;
result[mainType + 'Model'] = queryResult.models[0];
});
return result;
}
export function preParseFinder(
finderInput: ModelFinder,
opt?: {
// If pervided, types out of this list will be ignored.
includeMainTypes?: ComponentMainType[];
}
): {
mainTypeSpecified: boolean;
queryOptionMap: HashMap<QueryReferringUserOption, ComponentMainType>;
others: Partial<Pick<ParsedModelFinderKnown, 'dataIndex' | 'dataIndexInside'>>
} {
let finder: ModelFinderObject;
if (isString(finderInput)) {
const obj = {};
(obj as any)[finderInput + 'Index'] = 0;
finder = obj;
}
else {
finder = finderInput;
}
const queryOptionMap = createHashMap<QueryReferringUserOption, ComponentMainType>();
const others = {} as Partial<Pick<ParsedModelFinderKnown, 'dataIndex' | 'dataIndexInside'>>;
let mainTypeSpecified = false;
each(finder, function (value, key) {
// Exclude 'dataIndex' and other illgal keys.
if (key === 'dataIndex' || key === 'dataIndexInside') {
others[key] = value as number;
return;
}
const parsedKey = key.match(/^(\w+)(Index|Id|Name)$/) || [];
const mainType = parsedKey[1];
const queryType = (parsedKey[2] || '').toLowerCase() as keyof QueryReferringUserOption;
if (
!mainType
|| !queryType
|| (opt && opt.includeMainTypes && indexOf(opt.includeMainTypes, mainType) < 0)
) {
return;
}
mainTypeSpecified = mainTypeSpecified || !!mainType;
const queryOption = queryOptionMap.get(mainType) || queryOptionMap.set(mainType, {});
queryOption[queryType] = value as any;
});
return { mainTypeSpecified, queryOptionMap, others };
}
export type QueryReferringUserOption = {
index?: ModelFinderIndexQuery,
id?: ModelFinderIdQuery,
name?: ModelFinderNameQuery,
};
export const SINGLE_REFERRING: QueryReferringOpt = { useDefault: true, enableAll: false, enableNone: false };
export const MULTIPLE_REFERRING: QueryReferringOpt = { useDefault: false, enableAll: true, enableNone: true };
export type QueryReferringOpt = {
// Whether to use the first component as the default if none of index/id/name are specified.
useDefault?: boolean;
// Whether to enable `'all'` on index option.
enableAll?: boolean;
// Whether to enable `'none'`/`false` on index option.
enableNone?: boolean;
};
export function queryReferringComponents(
ecModel: GlobalModel,
mainType: ComponentMainType,
userOption: QueryReferringUserOption,
opt?: QueryReferringOpt
): {
// Always be array rather than null/undefined, which is convenient to use.
models: ComponentModel[];
// Whether there is indexOption/id/name specified
specified: boolean;
} {
opt = opt || SINGLE_REFERRING as QueryReferringOpt;
let indexOption = userOption.index;
let idOption = userOption.id;
let nameOption = userOption.name;
const result = {
models: null as ComponentModel[],
specified: indexOption != null || idOption != null || nameOption != null
};
if (!result.specified) {
// Use the first as default if `useDefault`.
let firstCmpt;
result.models = (
opt.useDefault && (firstCmpt = ecModel.getComponent(mainType))
) ? [firstCmpt] : [];
return result;
}
if (indexOption === 'none' || indexOption === false) {
assert(opt.enableNone, '`"none"` or `false` is not a valid value on index option.');
result.models = [];
return result;
}
// `queryComponents` will return all components if
// both all of index/id/name are null/undefined.
if (indexOption === 'all') {
assert(opt.enableAll, '`"all"` is not a valid value on index option.');
indexOption = idOption = nameOption = null;
}
result.models = ecModel.queryComponents({
mainType: mainType,
index: indexOption as number | number[],
id: idOption,
name: nameOption
});
return result;
}
export function setAttribute(dom: HTMLElement, key: string, value: any) {
dom.setAttribute
? dom.setAttribute(key, value)
: ((dom as any)[key] = value);
}
export function getAttribute(dom: HTMLElement, key: string): any {
return dom.getAttribute
? dom.getAttribute(key)
: (dom as any)[key];
}
export function getTooltipRenderMode(renderModeOption: TooltipRenderMode | 'auto'): TooltipRenderMode {
if (renderModeOption === 'auto') {
// Using html when `document` exists, use richText otherwise
return env.domSupported ? 'html' : 'richText';
}
else {
return renderModeOption || 'html';
}
}
/**
* Group a list by key.
*/
export function groupData<T, R extends string | number>(
array: T[],