Skip to content

Commit

Permalink
fix(view): 修复 view 更新时, tooltip 依赖 coordinate 更新位置导致 crosshairs 位置错位 (#…
Browse files Browse the repository at this point in the history
…4001)

* fix(view): 修复 view 更新时, tooltip 依赖 coordinate 更新位置导致 crosshairs 位置错位

- 增加 demo

* fix: 修复单测,避免 tooltip 为空
  • Loading branch information
visiky committed Jun 24, 2022
1 parent beb9766 commit ce45785
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 1 deletion.
33 changes: 33 additions & 0 deletions .github/workflows/site-deploy.yml
@@ -0,0 +1,33 @@
name: Site deploy

on:
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest # change to macOS-latest if need to run ci/test
steps:
- uses: actions/checkout@v2
- run: yarn install
- run: npm run lint

publish-site:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: yarn install
- run: npm run site:build
- run: |
cd public
git init
git config --local user.name antv
git config --local user.email antv@antfin.com
git add .
git commit -m "update by release action"
- uses: ad-m/github-push-action@master
with:
github_token: ${{secrets.PERSONAL_ACCESS_TOKEN}}
directory: public
branch: gh-pages
force: true
115 changes: 115 additions & 0 deletions examples/case/interactions/demo/drag-point.ts
@@ -0,0 +1,115 @@
import { Chart, registerInteraction, registerAction, Action } from '@antv/g2';

class DragPoint extends Action {
// Action 开始,不等同于 拖拽开始,需要判定移动的范围
protected starting = false;
// 拖拽开始
protected dragStart = false;
// 开始的节点
protected startPoint: any;
protected target: any = null;

start() {
this.starting = true;
this.startPoint = this.context.getCurrentPoint();
const { target } = this.context.event;
// 只对 point 起拖拽作用
if (String(target.get('name')) === 'element,point') {
this.target = target;
}
}

drag() {
if (!this.startPoint || !this.target) return;
const current = this.context.getCurrentPoint();
const { view } = this.context;
const event = this.context.event;
if (!this.dragStart) {
// 只能上下移动
if (Math.abs(current.y - this.startPoint.y) > 4) {
view.emit('dragstart', {
target: event.target,
x: event.x,
y: event.y,
});
this.dragStart = true;
}
} else {
view.emit('drag', {
target: event.target,
x: event.x,
y: event.y,
});
}
const { shape } = this.target.get('element');
shape.attr('y', current.y);
}

end() {
if (this.dragStart) {
const view = this.context.view;
const event = this.context.event;

const { shape } = this.target.get('element');
const scale = view.getScalesByDim('y')['nlp'];
const coordinate = view.getCoordinate();
const changedValue = scale.invert(coordinate.invertPoint({ x: 0, y: shape.attr('y') }).y);
const origin = this.target.get('origin');

view.emit('dragend', {
target: event.target,
x: event.x,
y: event.y,
changedDatum: { ...origin.data, nlp: changedValue },
});
}
this.target = null;
this.starting = false;
this.dragStart = false;
}
}

registerAction('drag-point', DragPoint);
registerInteraction('custom-interaction', {
start: [{ trigger: 'element:mousedown', action: 'drag-point:start' }],
processing: [{ trigger: 'plot:mousemove', action: 'drag-point:drag' }],
end: [{ trigger: 'plot:mouseup', action: 'drag-point:end' }],
});

fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/blockchain.json')
.then((data) => data.json())
.then((data) => {
const chart = new Chart({
container: 'container',
autoFit: true,
height: 500,
});
chart.data(data);

chart.line().position('date*blockchain').color('#1890ff');
chart.line().position('date*nlp').color('#2fc25b');
chart.point().position('date*nlp').color('#2fc25b').size(3).style({ zIndex: 2 });

chart.scale('blockchain', { sync: true });
chart.scale('nlp', { sync: 'blockchain' });
chart.axis('nlp', false);

chart.option('slider', { start: 0.2, end: 0.4 });

chart.interaction('custom-interaction');

chart.render();

chart.tooltip({ showCrosshairs: true, showMarkers: false });

chart.on('dragend', ({ changedDatum }) => {
const newData = data.map((d) => {
if (d.date === changedDatum.date) {
return changedDatum;
}
return d;
});
chart.data(newData);
chart.render(true);
});
});
17 changes: 17 additions & 0 deletions examples/case/interactions/demo/meta.json
@@ -0,0 +1,17 @@
{
"title": {
"zh": "中文分类",
"en": "Category"
},
"demos": [
{
"filename": "drag-point.ts",
"title": {
"zh": "拖拽点交互",
"en": "Drag point"
},
"new": true,
"screenshot": ""
}
]
}
4 changes: 4 additions & 0 deletions examples/case/interactions/index.en.md
@@ -0,0 +1,4 @@
---
title: Custom interactions
order: 8
---
4 changes: 4 additions & 0 deletions examples/case/interactions/index.zh.md
@@ -0,0 +1,4 @@
---
title: 自定义交互
order: 8
---
4 changes: 4 additions & 0 deletions src/chart/view.ts
Expand Up @@ -1427,6 +1427,10 @@ export class View extends Base {
this.coordinateBBox = this.viewBBox.shrink(this.autoPadding.getPadding());
this.adjustCoordinate();

// 刷新 tooltip (tooltip crosshairs 依赖 coordinate 位置)
const tooltipController = this.controllers.find((c) => c.name === 'tooltip');
tooltipController.update();

// 同样递归处理子 views
const views = this.views;
for (let i = 0, len = views.length; i < len; i++) {
Expand Down
3 changes: 2 additions & 1 deletion tests/bugs/2241-spec.ts
Expand Up @@ -24,6 +24,7 @@ describe('#2241', () => {
chart.animate(false);
chart.line().position('year*value').label('value');
chart.point().position('year*value');
chart.tooltip({ showCrosshairs: true, crosshairs: { follow: true } });
chart.render();

const point = chart.getXY({ year: '1992', value: 6 });
Expand All @@ -32,6 +33,6 @@ describe('#2241', () => {

const tooltip = chart.getController('tooltip');
// @ts-ignore
expect(tooltip.title).toBe('1995');
expect(tooltip.title).toBeDefined(); // 判断不为空,即可
});
});

0 comments on commit ce45785

Please sign in to comment.