This is an automated email from the ASF dual-hosted git repository. ovilia pushed a commit to branch feat-toolbar-snap in repository https://gitbox.apache.org/repos/asf/echarts.git
commit 25b0b2fc6c1fbf58ecfc42249bda36d1d86da8cf Author: Ovilia <zwl.s...@gmail.com> AuthorDate: Tue Mar 22 17:22:38 2022 +0800 feat(tooltip): tooltip with snap --- src/component/tooltip/TooltipModel.ts | 6 +++++- src/component/tooltip/TooltipView.ts | 37 +++++++++++++++++++++++++++++++++-- test/tooltip-snap.html | 2 +- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/component/tooltip/TooltipModel.ts b/src/component/tooltip/TooltipModel.ts index 413b52f..3264a20 100644 --- a/src/component/tooltip/TooltipModel.ts +++ b/src/component/tooltip/TooltipModel.ts @@ -73,6 +73,8 @@ export interface TooltipOption extends CommonTooltipOption<TopLevelFormatterPara className?: string order?: TooltipOrderMode + + snapSize?: number } class TooltipModel extends ComponentModel<TooltipOption> { @@ -171,7 +173,9 @@ class TooltipModel extends ComponentModel<TooltipOption> { textStyle: { color: '#666', fontSize: 14 - } + }, + + snapSize: 25 }; } diff --git a/src/component/tooltip/TooltipView.ts b/src/component/tooltip/TooltipView.ts index 51b5200..ded1c2f 100644 --- a/src/component/tooltip/TooltipView.ts +++ b/src/component/tooltip/TooltipView.ts @@ -497,11 +497,44 @@ class TooltipView extends ComponentView { } } else { - this._lastDataByCoordSys = null; - this._hide(dispatchAction); + const snapSize = tooltipModel.get('snapSize'); + const nearestEl = this._findNearestData(this._lastX, this._lastY, snapSize); + if (!nearestEl) { + this._lastDataByCoordSys = null; + this._hide(dispatchAction); + } + else { + this._showSeriesItemTooltip(e, nearestEl, dispatchAction); + } } } + private _findNearestData(x: number, y: number, snapSize: number): Element { + let minDistance = snapSize * snapSize; + let minDataEl: Element = null; + this._ecModel.eachSeries(seriesModel => { + const data = seriesModel.getData(); + const coordSys = seriesModel.coordinateSystem; + // TODO: also consider snapSize as tolerance? + if (coordSys.containPoint([x, y])) { + let minDataId: number = null; + data.each(id => { + const values = data.getValues(id); + const pts = coordSys.dataToPoint(values); + const distance = (pts[0] - x) * (pts[0] - x) + (pts[1] - y) * (pts[1] - y); + if (distance < minDistance) { + minDistance = distance; + minDataId = id; + } + }); + if (minDataId != null) { + minDataEl = data.getItemGraphicEl(minDataId); + } + } + }); + return minDataEl; + } + private _showOrMove( tooltipModel: Model<TooltipOption>, cb: () => void diff --git a/test/tooltip-snap.html b/test/tooltip-snap.html index 285c7b7..b5136d5 100644 --- a/test/tooltip-snap.html +++ b/test/tooltip-snap.html @@ -112,7 +112,7 @@ under the License. }], tooltip: { show: true, - hideDelay: 1000, + // hideDelay: 1000, snap: true } }; --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@echarts.apache.org For additional commands, e-mail: commits-h...@echarts.apache.org