-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathTooltipView.ts
1228 lines (1083 loc) · 42.8 KB
/
TooltipView.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 { bind, each, clone, trim, isString, isFunction, isArray, isObject, extend } from 'zrender/src/core/util';
import env from 'zrender/src/core/env';
import TooltipHTMLContent from './TooltipHTMLContent';
import TooltipRichContent from './TooltipRichContent';
import { convertToColorString, formatTpl, TooltipMarker } from '../../util/format';
import { parsePercent } from '../../util/number';
import { Rect } from '../../util/graphic';
import findPointFromSeries from '../axisPointer/findPointFromSeries';
import { getLayoutRect } from '../../util/layout';
import Model from '../../model/Model';
import * as globalListener from '../axisPointer/globalListener';
import * as axisHelper from '../../coord/axisHelper';
import * as axisPointerViewHelper from '../axisPointer/viewHelper';
import { getTooltipRenderMode, preParseFinder, queryReferringComponents } from '../../util/model';
import ComponentView from '../../view/Component';
import { format as timeFormat } from '../../util/time';
import {
HorizontalAlign,
VerticalAlign,
ZRRectLike,
BoxLayoutOptionMixin,
CallbackDataParams,
TooltipRenderMode,
ECElement,
CommonTooltipOption,
ZRColor,
ComponentMainType,
ComponentItemTooltipOption
} from '../../util/types';
import GlobalModel from '../../model/Global';
import ExtensionAPI from '../../core/ExtensionAPI';
import TooltipModel, { TooltipOption } from './TooltipModel';
import Element from 'zrender/src/Element';
import { AxisBaseModel } from '../../coord/AxisBaseModel';
import { ECData, getECData } from '../../util/innerStore';
import { shouldTooltipConfine } from './helper';
import { DataByCoordSys, DataByAxis } from '../axisPointer/axisTrigger';
import { normalizeTooltipFormatResult } from '../../model/mixin/dataFormat';
import { createTooltipMarkup, buildTooltipMarkup, TooltipMarkupStyleCreator } from './tooltipMarkup';
import { findEventDispatcher } from '../../util/event';
import { clear, createOrUpdate } from '../../util/throttle';
const proxyRect = new Rect({
shape: { x: -1, y: -1, width: 2, height: 2 }
});
interface DataIndex {
seriesIndex: number
dataIndex: number
dataIndexInside: number
}
interface ShowTipPayload {
type?: 'showTip'
from?: string
// Type 1
tooltip?: ECData['tooltipConfig']['option']
// Type 2
dataByCoordSys?: DataByCoordSys[]
tooltipOption?: CommonTooltipOption<TooltipCallbackDataParams | TooltipCallbackDataParams[]>
// Type 3
seriesIndex?: number
dataIndex?: number
// Type 4
name?: string // target item name that enable tooltip.
// legendIndex: 0,
// toolboxId: 'some_id',
// geoName: 'some_name',
x?: number
y?: number
position?: TooltipOption['position']
dispatchAction?: ExtensionAPI['dispatchAction']
}
interface HideTipPayload {
type?: 'hideTip'
from?: string
dispatchAction?: ExtensionAPI['dispatchAction']
}
interface TryShowParams {
target?: ECElement,
offsetX?: number
offsetY?: number
/**
* Used for axis trigger.
*/
dataByCoordSys?: DataByCoordSys[]
tooltipOption?: ComponentItemTooltipOption<TooltipCallbackDataParams | TooltipCallbackDataParams[]>
position?: TooltipOption['position']
/**
* If `position` is not set in payload nor option, use it.
*/
positionDefault?: TooltipOption['position']
}
type TooltipCallbackDataParams = CallbackDataParams & {
axisDim?: string
axisIndex?: number
axisType?: string
axisId?: string
// TODO: TYPE Value type
axisValue?: string | number
axisValueLabel?: string
marker?: TooltipMarker
};
class TooltipView extends ComponentView {
static type = 'tooltip' as const;
type = TooltipView.type;
private _renderMode: TooltipRenderMode;
private _tooltipModel: TooltipModel;
private _ecModel: GlobalModel;
private _api: ExtensionAPI;
private _alwaysShowContent: boolean;
private _tooltipContent: TooltipHTMLContent | TooltipRichContent;
private _refreshUpdateTimeout: number;
private _lastX: number;
private _lastY: number;
private _ticket: string;
private _showTimout: number;
private _lastDataByCoordSys: DataByCoordSys[];
private _cbParamsList: TooltipCallbackDataParams[];
init(ecModel: GlobalModel, api: ExtensionAPI) {
if (env.node || !api.getDom()) {
return;
}
const tooltipModel = ecModel.getComponent('tooltip') as TooltipModel;
const renderMode = this._renderMode = getTooltipRenderMode(tooltipModel.get('renderMode'));
this._tooltipContent = renderMode === 'richText'
? new TooltipRichContent(api)
: new TooltipHTMLContent(api.getDom(), api, {
appendToBody: tooltipModel.get('appendToBody', true)
});
}
render(
tooltipModel: TooltipModel,
ecModel: GlobalModel,
api: ExtensionAPI
) {
if (env.node || !api.getDom()) {
return;
}
// Reset
this.group.removeAll();
this._tooltipModel = tooltipModel;
this._ecModel = ecModel;
this._api = api;
/**
* @private
* @type {boolean}
*/
this._alwaysShowContent = tooltipModel.get('alwaysShowContent');
const tooltipContent = this._tooltipContent;
tooltipContent.update(tooltipModel);
tooltipContent.setEnterable(tooltipModel.get('enterable'));
this._initGlobalListener();
this._keepShow();
// PENDING
// `mousemove` event will be triggered very frequently when the mouse moves fast,
// which causes that the `updatePosition` function was also called frequently.
// In Chrome with devtools open and Firefox, tooltip looks laggy and shakes. See #14695 #16101
// To avoid frequent triggering,
// consider throttling it in 50ms when transition is enabled
if (this._renderMode !== 'richText' && tooltipModel.get('transitionDuration')) {
createOrUpdate(this, '_updatePosition', 50, 'fixRate');
}
else {
clear(this, '_updatePosition');
}
}
private _initGlobalListener() {
const tooltipModel = this._tooltipModel;
const triggerOn = tooltipModel.get('triggerOn');
globalListener.register(
'itemTooltip',
this._api,
bind(function (currTrigger, e, dispatchAction) {
// If 'none', it is not controlled by mouse totally.
if (triggerOn !== 'none') {
if (triggerOn.indexOf(currTrigger) >= 0) {
this._tryShow(e, dispatchAction);
}
else if (currTrigger === 'leave') {
this._hide(dispatchAction);
}
}
}, this)
);
}
private _keepShow() {
const tooltipModel = this._tooltipModel;
const ecModel = this._ecModel;
const api = this._api;
// Try to keep the tooltip show when refreshing
if (this._lastX != null
&& this._lastY != null
// When user is willing to control tooltip totally using API,
// self.manuallyShowTip({x, y}) might cause tooltip hide,
// which is not expected.
&& tooltipModel.get('triggerOn') !== 'none'
) {
const self = this;
clearTimeout(this._refreshUpdateTimeout);
this._refreshUpdateTimeout = setTimeout(function () {
// Show tip next tick after other charts are rendered
// In case highlight action has wrong result
// FIXME
!api.isDisposed() && self.manuallyShowTip(tooltipModel, ecModel, api, {
x: self._lastX,
y: self._lastY,
dataByCoordSys: self._lastDataByCoordSys
});
}) as any;
}
}
/**
* Show tip manually by
* dispatchAction({
* type: 'showTip',
* x: 10,
* y: 10
* });
* Or
* dispatchAction({
* type: 'showTip',
* seriesIndex: 0,
* dataIndex or dataIndexInside or name
* });
*
* TODO Batch
*/
manuallyShowTip(
tooltipModel: TooltipModel,
ecModel: GlobalModel,
api: ExtensionAPI,
payload: ShowTipPayload
) {
if (payload.from === this.uid || env.node || !api.getDom()) {
return;
}
const dispatchAction = makeDispatchAction(payload, api);
// Reset ticket
this._ticket = '';
// When triggered from axisPointer.
const dataByCoordSys = payload.dataByCoordSys;
const cmptRef = findComponentReference(payload, ecModel, api);
if (cmptRef) {
const rect = cmptRef.el.getBoundingRect().clone();
rect.applyTransform(cmptRef.el.transform);
this._tryShow({
offsetX: rect.x + rect.width / 2,
offsetY: rect.y + rect.height / 2,
target: cmptRef.el,
position: payload.position,
// When manully trigger, the mouse is not on the el, so we'd better to
// position tooltip on the bottom of the el and display arrow is possible.
positionDefault: 'bottom'
}, dispatchAction);
}
else if (payload.tooltip && payload.x != null && payload.y != null) {
const el = proxyRect as unknown as ECElement;
el.x = payload.x;
el.y = payload.y;
el.update();
getECData(el).tooltipConfig = {
name: null,
option: payload.tooltip
};
// Manually show tooltip while view is not using zrender elements.
this._tryShow({
offsetX: payload.x,
offsetY: payload.y,
target: el
}, dispatchAction);
}
else if (dataByCoordSys) {
this._tryShow({
offsetX: payload.x,
offsetY: payload.y,
position: payload.position,
dataByCoordSys: dataByCoordSys,
tooltipOption: payload.tooltipOption
}, dispatchAction);
}
else if (payload.seriesIndex != null) {
if (this._manuallyAxisShowTip(tooltipModel, ecModel, api, payload)) {
return;
}
const pointInfo = findPointFromSeries(payload, ecModel);
const cx = pointInfo.point[0];
const cy = pointInfo.point[1];
if (cx != null && cy != null) {
this._tryShow({
offsetX: cx,
offsetY: cy,
target: pointInfo.el,
position: payload.position,
// When manully trigger, the mouse is not on the el, so we'd better to
// position tooltip on the bottom of the el and display arrow is possible.
positionDefault: 'bottom'
}, dispatchAction);
}
}
else if (payload.x != null && payload.y != null) {
// FIXME
// should wrap dispatchAction like `axisPointer/globalListener` ?
api.dispatchAction({
type: 'updateAxisPointer',
x: payload.x,
y: payload.y
});
this._tryShow({
offsetX: payload.x,
offsetY: payload.y,
position: payload.position,
target: api.getZr().findHover(payload.x, payload.y).target
}, dispatchAction);
}
}
manuallyHideTip(
tooltipModel: TooltipModel,
ecModel: GlobalModel,
api: ExtensionAPI,
payload: HideTipPayload
) {
const tooltipContent = this._tooltipContent;
if (!this._alwaysShowContent && this._tooltipModel) {
tooltipContent.hideLater(this._tooltipModel.get('hideDelay'));
}
this._lastX = this._lastY = this._lastDataByCoordSys = null;
if (payload.from !== this.uid) {
this._hide(makeDispatchAction(payload, api));
}
}
// Be compatible with previous design, that is, when tooltip.type is 'axis' and
// dispatchAction 'showTip' with seriesIndex and dataIndex will trigger axis pointer
// and tooltip.
private _manuallyAxisShowTip(
tooltipModel: TooltipModel,
ecModel: GlobalModel,
api: ExtensionAPI,
payload: ShowTipPayload
) {
const seriesIndex = payload.seriesIndex;
const dataIndex = payload.dataIndex;
// @ts-ignore
const coordSysAxesInfo = ecModel.getComponent('axisPointer').coordSysAxesInfo;
if (seriesIndex == null || dataIndex == null || coordSysAxesInfo == null) {
return;
}
const seriesModel = ecModel.getSeriesByIndex(seriesIndex);
if (!seriesModel) {
return;
}
const data = seriesModel.getData();
const tooltipCascadedModel = buildTooltipModel([
data.getItemModel<TooltipableOption>(dataIndex),
seriesModel as Model<TooltipableOption>,
(seriesModel.coordinateSystem || {}).model as Model<TooltipableOption>
], this._tooltipModel);
if (tooltipCascadedModel.get('trigger') !== 'axis') {
return;
}
api.dispatchAction({
type: 'updateAxisPointer',
seriesIndex: seriesIndex,
dataIndex: dataIndex,
position: payload.position
});
return true;
}
private _tryShow(
e: TryShowParams,
dispatchAction: ExtensionAPI['dispatchAction']
) {
const el = e.target;
const tooltipModel = this._tooltipModel;
if (!tooltipModel) {
return;
}
// Save mouse x, mouse y. So we can try to keep showing the tip if chart is refreshed
this._lastX = e.offsetX;
this._lastY = e.offsetY;
const dataByCoordSys = e.dataByCoordSys;
if (dataByCoordSys && dataByCoordSys.length) {
this._showAxisTooltip(dataByCoordSys, e);
}
else if (el) {
this._lastDataByCoordSys = null;
let seriesDispatcher: Element;
let cmptDispatcher: Element;
findEventDispatcher(el, (target) => {
// Always show item tooltip if mouse is on the element with dataIndex
if (getECData(target).dataIndex != null) {
seriesDispatcher = target;
return true;
}
// Tooltip provided directly. Like legend.
if (getECData(target).tooltipConfig != null) {
cmptDispatcher = target;
return true;
}
}, true);
if (seriesDispatcher) {
this._showSeriesItemTooltip(e, seriesDispatcher, dispatchAction);
}
else if (cmptDispatcher) {
this._showComponentItemTooltip(e, cmptDispatcher, dispatchAction);
}
else {
this._hide(dispatchAction);
}
}
else {
this._lastDataByCoordSys = null;
this._hide(dispatchAction);
}
}
private _showOrMove(
tooltipModel: Model<TooltipOption>,
cb: () => void
) {
// showDelay is used in this case: tooltip.enterable is set
// as true. User intent to move mouse into tooltip and click
// something. `showDelay` makes it easier to enter the content
// but tooltip do not move immediately.
const delay = tooltipModel.get('showDelay');
cb = bind(cb, this);
clearTimeout(this._showTimout);
delay > 0
? (this._showTimout = setTimeout(cb, delay) as any)
: cb();
}
private _showAxisTooltip(
dataByCoordSys: DataByCoordSys[],
e: TryShowParams
) {
const ecModel = this._ecModel;
const globalTooltipModel = this._tooltipModel;
const point = [e.offsetX, e.offsetY];
const singleTooltipModel = buildTooltipModel(
[e.tooltipOption],
globalTooltipModel
);
const renderMode = this._renderMode;
const cbParamsList: TooltipCallbackDataParams[] = [];
const articleMarkup = createTooltipMarkup('section', {
blocks: [],
noHeader: true
});
// Only for legacy: `Serise['formatTooltip']` returns a string.
const markupTextArrLegacy: string[] = [];
const markupStyleCreator = new TooltipMarkupStyleCreator();
each(dataByCoordSys, function (itemCoordSys) {
each(itemCoordSys.dataByAxis, function (axisItem) {
const axisModel = ecModel.getComponent(axisItem.axisDim + 'Axis', axisItem.axisIndex) as AxisBaseModel;
const axisValue = axisItem.value;
if (!axisModel || axisValue == null) {
return;
}
const axisValueLabel = axisPointerViewHelper.getValueLabel(
axisValue, axisModel.axis, ecModel,
axisItem.seriesDataIndices,
axisItem.valueLabelOpt
);
const axisSectionMarkup = createTooltipMarkup('section', {
header: axisValueLabel,
noHeader: !trim(axisValueLabel),
sortBlocks: true,
blocks: []
});
articleMarkup.blocks.push(axisSectionMarkup);
each(axisItem.seriesDataIndices, function (idxItem) {
const series = ecModel.getSeriesByIndex(idxItem.seriesIndex);
const dataIndex = idxItem.dataIndexInside;
const cbParams = series.getDataParams(dataIndex) as TooltipCallbackDataParams;
// Can't find data.
if (cbParams.dataIndex < 0) {
return;
}
cbParams.axisDim = axisItem.axisDim;
cbParams.axisIndex = axisItem.axisIndex;
cbParams.axisType = axisItem.axisType;
cbParams.axisId = axisItem.axisId;
cbParams.axisValue = axisHelper.getAxisRawValue(
axisModel.axis, { value: axisValue as number }
);
cbParams.axisValueLabel = axisValueLabel;
// Pre-create marker style for makers. Users can assemble richText
// text in `formatter` callback and use those markers style.
cbParams.marker = markupStyleCreator.makeTooltipMarker(
'item', convertToColorString(cbParams.color), renderMode
);
const seriesTooltipResult = normalizeTooltipFormatResult(
series.formatTooltip(dataIndex, true, null)
);
const frag = seriesTooltipResult.frag;
if (frag) {
const valueFormatter = buildTooltipModel(
[series as Model<TooltipableOption>],
globalTooltipModel
).get('valueFormatter');
axisSectionMarkup.blocks.push(valueFormatter ? extend({ valueFormatter }, frag) : frag);
}
if (seriesTooltipResult.text) {
markupTextArrLegacy.push(seriesTooltipResult.text);
}
cbParamsList.push(cbParams);
});
});
});
// In most cases, the second axis is displays upper on the first one.
// So we reverse it to look better.
articleMarkup.blocks.reverse();
markupTextArrLegacy.reverse();
const positionExpr = e.position;
const orderMode = singleTooltipModel.get('order');
const builtMarkupText = buildTooltipMarkup(
articleMarkup, markupStyleCreator, renderMode, orderMode, ecModel.get('useUTC'),
singleTooltipModel.get('textStyle')
);
builtMarkupText && markupTextArrLegacy.unshift(builtMarkupText);
const blockBreak = renderMode === 'richText' ? '\n\n' : '<br/>';
const allMarkupText = markupTextArrLegacy.join(blockBreak);
this._showOrMove(singleTooltipModel, function (this: TooltipView) {
if (this._updateContentNotChangedOnAxis(dataByCoordSys, cbParamsList)) {
this._updatePosition(
singleTooltipModel,
positionExpr,
point[0], point[1],
this._tooltipContent,
cbParamsList
);
}
else {
this._showTooltipContent(
singleTooltipModel, allMarkupText, cbParamsList, Math.random() + '',
point[0], point[1], positionExpr, null, markupStyleCreator
);
}
});
// Do not trigger events here, because this branch only be entered
// from dispatchAction.
}
private _showSeriesItemTooltip(
e: TryShowParams,
dispatcher: ECElement,
dispatchAction: ExtensionAPI['dispatchAction']
) {
const ecModel = this._ecModel;
const ecData = getECData(dispatcher);
// Use dataModel in element if possible
// Used when mouseover on a element like markPoint or edge
// In which case, the data is not main data in series.
const seriesIndex = ecData.seriesIndex;
const seriesModel = ecModel.getSeriesByIndex(seriesIndex);
// For example, graph link.
const dataModel = ecData.dataModel || seriesModel;
const dataIndex = ecData.dataIndex;
const dataType = ecData.dataType;
const data = dataModel.getData(dataType);
const renderMode = this._renderMode;
const positionDefault = e.positionDefault;
const tooltipModel = buildTooltipModel(
[
data.getItemModel<TooltipableOption>(dataIndex),
dataModel,
seriesModel && (seriesModel.coordinateSystem || {}).model as Model<TooltipableOption>
],
this._tooltipModel,
positionDefault ? { position: positionDefault } : null
);
const tooltipTrigger = tooltipModel.get('trigger');
if (tooltipTrigger != null && tooltipTrigger !== 'item') {
return;
}
const params = dataModel.getDataParams(dataIndex, dataType);
const markupStyleCreator = new TooltipMarkupStyleCreator();
// Pre-create marker style for makers. Users can assemble richText
// text in `formatter` callback and use those markers style.
params.marker = markupStyleCreator.makeTooltipMarker(
'item', convertToColorString(params.color), renderMode
);
const seriesTooltipResult = normalizeTooltipFormatResult(
dataModel.formatTooltip(dataIndex, false, dataType)
);
const orderMode = tooltipModel.get('order');
const valueFormatter = tooltipModel.get('valueFormatter');
const frag = seriesTooltipResult.frag;
const markupText = frag ? buildTooltipMarkup(
valueFormatter ? extend({ valueFormatter }, frag) : frag,
markupStyleCreator,
renderMode,
orderMode,
ecModel.get('useUTC'),
tooltipModel.get('textStyle')
)
: seriesTooltipResult.text;
const asyncTicket = 'item_' + dataModel.name + '_' + dataIndex;
this._showOrMove(tooltipModel, function (this: TooltipView) {
this._showTooltipContent(
tooltipModel, markupText, params, asyncTicket,
e.offsetX, e.offsetY, e.position, e.target,
markupStyleCreator
);
});
// FIXME
// duplicated showtip if manuallyShowTip is called from dispatchAction.
dispatchAction({
type: 'showTip',
dataIndexInside: dataIndex,
dataIndex: data.getRawIndex(dataIndex),
seriesIndex: seriesIndex,
from: this.uid
});
}
private _showComponentItemTooltip(
e: TryShowParams,
el: ECElement,
dispatchAction: ExtensionAPI['dispatchAction']
) {
const ecData = getECData(el);
const tooltipConfig = ecData.tooltipConfig;
let tooltipOpt = tooltipConfig.option || {};
if (isString(tooltipOpt)) {
const content = tooltipOpt;
tooltipOpt = {
content: content,
// Fixed formatter
formatter: content
};
}
const tooltipModelCascade = [tooltipOpt] as TooltipModelOptionCascade[];
const cmpt = this._ecModel.getComponent(ecData.componentMainType, ecData.componentIndex);
if (cmpt) {
tooltipModelCascade.push(cmpt as Model<TooltipableOption>);
}
// In most cases, component tooltip formatter has different params with series tooltip formatter,
// so that they can not share the same formatter. Since the global tooltip formatter is used for series
// by convension, we do not use it as the default formatter for component.
tooltipModelCascade.push({ formatter: tooltipOpt.content });
const positionDefault = e.positionDefault;
const subTooltipModel = buildTooltipModel(
tooltipModelCascade,
this._tooltipModel,
positionDefault ? { position: positionDefault } : null
);
const defaultHtml = subTooltipModel.get('content');
const asyncTicket = Math.random() + '';
// PENDING: this case do not support richText style yet.
const markupStyleCreator = new TooltipMarkupStyleCreator();
// Do not check whether `trigger` is 'none' here, because `trigger`
// only works on coordinate system. In fact, we have not found case
// that requires setting `trigger` nothing on component yet.
this._showOrMove(subTooltipModel, function (this: TooltipView) {
// Use formatterParams from element defined in component
// Avoid users modify it.
const formatterParams = clone(subTooltipModel.get('formatterParams') as any || {});
this._showTooltipContent(
subTooltipModel, defaultHtml, formatterParams,
asyncTicket, e.offsetX, e.offsetY, e.position, el, markupStyleCreator
);
});
// If not dispatch showTip, tip may be hide triggered by axis.
dispatchAction({
type: 'showTip',
from: this.uid
});
}
private _showTooltipContent(
// Use Model<TooltipOption> insteadof TooltipModel because this model may be from series or other options.
// Instead of top level tooltip.
tooltipModel: Model<TooltipOption>,
defaultHtml: string,
params: TooltipCallbackDataParams | TooltipCallbackDataParams[],
asyncTicket: string,
x: number,
y: number,
positionExpr: TooltipOption['position'],
el: ECElement,
markupStyleCreator: TooltipMarkupStyleCreator
) {
// Reset ticket
this._ticket = '';
if (!tooltipModel.get('showContent') || !tooltipModel.get('show')) {
return;
}
const tooltipContent = this._tooltipContent;
tooltipContent.setEnterable(tooltipModel.get('enterable'));
const formatter = tooltipModel.get('formatter');
positionExpr = positionExpr || tooltipModel.get('position');
let html: string | HTMLElement | HTMLElement[] = defaultHtml;
const nearPoint = this._getNearestPoint(
[x, y],
params,
tooltipModel.get('trigger'),
tooltipModel.get('borderColor')
);
const nearPointColor = nearPoint.color;
if (formatter) {
if (isString(formatter)) {
const useUTC = tooltipModel.ecModel.get('useUTC');
const params0 = isArray(params) ? params[0] : params;
const isTimeAxis = params0 && params0.axisType && params0.axisType.indexOf('time') >= 0;
html = formatter;
if (isTimeAxis) {
html = timeFormat(params0.axisValue, html, useUTC);
}
html = formatTpl(html, params, true);
}
else if (isFunction(formatter)) {
const callback = bind(function (cbTicket: string, html: string | HTMLElement | HTMLElement[]) {
if (cbTicket === this._ticket) {
tooltipContent.setContent(html, markupStyleCreator, tooltipModel, nearPointColor, positionExpr);
this._updatePosition(
tooltipModel, positionExpr, x, y, tooltipContent, params, el
);
}
}, this);
this._ticket = asyncTicket;
html = formatter(params, asyncTicket, callback);
}
else {
html = formatter;
}
}
tooltipContent.setContent(html, markupStyleCreator, tooltipModel, nearPointColor, positionExpr);
tooltipContent.show(tooltipModel, nearPointColor);
this._updatePosition(
tooltipModel, positionExpr, x, y, tooltipContent, params, el
);
}
private _getNearestPoint(
point: number[],
tooltipDataParams: TooltipCallbackDataParams | TooltipCallbackDataParams[],
trigger: TooltipOption['trigger'],
borderColor: ZRColor
): {
color: ZRColor;
} {
if (trigger === 'axis' || isArray(tooltipDataParams)) {
return {
color: borderColor || (this._renderMode === 'html' ? '#fff' : 'none')
};
}
if (!isArray(tooltipDataParams)) {
return {
color: borderColor || tooltipDataParams.color || tooltipDataParams.borderColor
};
}
}
_updatePosition(
tooltipModel: Model<TooltipOption>,
positionExpr: TooltipOption['position'],
x: number, // Mouse x
y: number, // Mouse y
content: TooltipHTMLContent | TooltipRichContent,
params: TooltipCallbackDataParams | TooltipCallbackDataParams[],
el?: Element
) {
const viewWidth = this._api.getWidth();
const viewHeight = this._api.getHeight();
positionExpr = positionExpr || tooltipModel.get('position');
const contentSize = content.getSize();
let align = tooltipModel.get('align');
let vAlign = tooltipModel.get('verticalAlign');
const rect = el && el.getBoundingRect().clone();
el && rect.applyTransform(el.transform);
if (isFunction(positionExpr)) {
// Callback of position can be an array or a string specify the position
positionExpr = positionExpr([x, y], params, content.el, rect, {
viewSize: [viewWidth, viewHeight],
contentSize: contentSize.slice() as [number, number]
});
}
if (isArray(positionExpr)) {
x = parsePercent(positionExpr[0], viewWidth);
y = parsePercent(positionExpr[1], viewHeight);
}
else if (isObject(positionExpr)) {
const boxLayoutPosition = positionExpr as BoxLayoutOptionMixin;
boxLayoutPosition.width = contentSize[0];
boxLayoutPosition.height = contentSize[1];
const layoutRect = getLayoutRect(
boxLayoutPosition, { width: viewWidth, height: viewHeight }
);
x = layoutRect.x;
y = layoutRect.y;
align = null;
// When positionExpr is left/top/right/bottom,
// align and verticalAlign will not work.
vAlign = null;
}
// Specify tooltip position by string 'top' 'bottom' 'left' 'right' around graphic element
else if (isString(positionExpr) && el) {
const pos = calcTooltipPosition(
positionExpr, rect, contentSize, tooltipModel.get('borderWidth')
);
x = pos[0];
y = pos[1];
}
else {
const pos = refixTooltipPosition(
x, y, content, viewWidth, viewHeight, align ? null : 20, vAlign ? null : 20
);
x = pos[0];
y = pos[1];
}
align && (x -= isCenterAlign(align) ? contentSize[0] / 2 : align === 'right' ? contentSize[0] : 0);
vAlign && (y -= isCenterAlign(vAlign) ? contentSize[1] / 2 : vAlign === 'bottom' ? contentSize[1] : 0);
if (shouldTooltipConfine(tooltipModel)) {
const pos = confineTooltipPosition(
x, y, content, viewWidth, viewHeight
);
x = pos[0];
y = pos[1];
}
content.moveTo(x, y);
}
// FIXME
// Should we remove this but leave this to user?
private _updateContentNotChangedOnAxis(
dataByCoordSys: DataByCoordSys[],
cbParamsList: TooltipCallbackDataParams[]
) {
const lastCoordSys = this._lastDataByCoordSys;
const lastCbParamsList = this._cbParamsList;
let contentNotChanged = !!lastCoordSys
&& lastCoordSys.length === dataByCoordSys.length;
contentNotChanged && each(lastCoordSys, (lastItemCoordSys, indexCoordSys) => {
const lastDataByAxis = lastItemCoordSys.dataByAxis || [] as DataByAxis[];
const thisItemCoordSys = dataByCoordSys[indexCoordSys] || {} as DataByCoordSys;
const thisDataByAxis = thisItemCoordSys.dataByAxis || [] as DataByAxis[];
contentNotChanged = contentNotChanged && lastDataByAxis.length === thisDataByAxis.length;
contentNotChanged && each(lastDataByAxis, (lastItem, indexAxis) => {
const thisItem = thisDataByAxis[indexAxis] || {} as DataByAxis;
const lastIndices = lastItem.seriesDataIndices || [] as DataIndex[];
const newIndices = thisItem.seriesDataIndices || [] as DataIndex[];
contentNotChanged = contentNotChanged
&& lastItem.value === thisItem.value
&& lastItem.axisType === thisItem.axisType
&& lastItem.axisId === thisItem.axisId
&& lastIndices.length === newIndices.length;
contentNotChanged && each(lastIndices, (lastIdxItem, j) => {
const newIdxItem = newIndices[j];
contentNotChanged = contentNotChanged
&& lastIdxItem.seriesIndex === newIdxItem.seriesIndex
&& lastIdxItem.dataIndex === newIdxItem.dataIndex;
});
// check is cbParams data value changed
lastCbParamsList && each(lastItem.seriesDataIndices, (idxItem) => {
const seriesIdx = idxItem.seriesIndex;
const cbParams = cbParamsList[seriesIdx];
const lastCbParams = lastCbParamsList[seriesIdx];
if (cbParams && lastCbParams && lastCbParams.data !== cbParams.data) {
contentNotChanged = false;
}
});
});
});
this._lastDataByCoordSys = dataByCoordSys;
this._cbParamsList = cbParamsList;
return !!contentNotChanged;