feat(tooltip): tooltip with snap
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 @@
className?: string
order?: TooltipOrderMode
+
+ snapSize?: number
}
class TooltipModel extends ComponentModel<TooltipOption> {
@@ -171,7 +173,9 @@
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 @@
}
}
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 @@
}],
tooltip: {
show: true,
- hideDelay: 1000,
+ // hideDelay: 1000,
snap: true
}
};