antvis / antvis/G2

[Discussion]: 使用 layer 绘制多个极坐标系图表,无法触发下层图表的 tooltip,求解决方案。

Open
#7,264 4 comments 0 reactions 0 assignees View on GitHub
P2 question waiting for maintainer
Dominant language
TypeScript
Stars
12.6k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

### Describe the bug / 问题描述

我使用了多个极坐标系图表来实现一个复合的图表,这种方式只能触发顶层图表的 tooltip,无法触发下层图表的 tooltip,请问有什么方案可以实现每个图表的 tooltip 都正确触发吗?
我尝试用事件监听+自定义tooltip实现展示 tooltip 的效果,但是 `chart.getDataByXY(point, { shared: true });` 无法正确获取到对应点位的数据,想知道我的代码该如何改进,求大佬指点,参见下述代码:
```
import { Chart } from '@antv/g2';

const data = [
{ year: '2000', '类型 A': 21.0, '类型 B': 16, '类型 C': 8 },
{ year: '2001', '类型 A': 25.0, '类型 B': 16, '类型 C': 8 },
{ year: '2002', '类型 A': 25.0, '类型 B': 15, '类型 C': 8 },
{ year: '2003', '类型 A': 25.0, '类型 B': 14, '类型 C': 7 },
{ year: '2004', '类型 A': 25.0, '类型 B': 14, '类型 C': 7 },
{ year: '2005', '类型 A': 24.0, '类型 B': 13, '类型 C': 8 },
{ year: '2006', '类型 A': 24.0, '类型 B': 14, '类型 C': 7 },
{ year: '2007', '类型 A': 26.0, '类型 B': 16, '类型 C': 7 },
{ year: '2008', '类型 A': 26.0, '类型 B': 15.2, '类型 C': 8 },
{ year: '2009', '类型 A': 27.1, '类型 B': 15.2, '类型 C': 10 },
{ year: '2010', '类型 A': 27.5, '类型 B': 15.4, '类型 C': 8 },
{ year: '2011', '类型 A': 26.4, '类型 B': 15.2, '类型 C': 9 },
{ year: '2012', '类型 A': 28.8, '类型 B': 15.4, '类型 C': 9 },
{ year: '2013', '类型 A': 33.3, '类型 B': 16.7, '类型 C': 12 },
{ year: '2014', '类型 A': 38.2, '类型 B': 19.5, '类型 C': 18 },
];
const chart = new Chart({
container: 'container',
autoFit: true,
width: 720,
height: 720,
});

const layer = chart.spaceLayer();

const drawSpacedPolar = (idx) => {
const startAngle = idx * Math.PI / 2 - Math.PI;
const endAngle = startAngle + Math.PI / 2;
console.log({ startAngle, endAngle })

layer
.interval()
.coordinate({
type: 'polar',
innerRadius: 0,
startAngle,
endAngle,
})
.data({
value: data,
transform: [
{
type: 'fold',
fields: ['类型 A', '类型 B', '类型 C'],
key: '难民类型',
value: 'count',
},
],
})
.encode('x', 'year')
.encode('y', 'count')
.encode('color', '难民类型')
.scale('x', { padding: 0 })
.style({
lineWidth: 1,
stroke: '#fff',
})
.transform([{ type: 'stackY' }])
.axis('x', false)
.axis('y', false)
.legend({
color: {
position: 'bottom',
layout: {
justifyContent: 'center',
},
},
})
.state('active', { stroke: 'black', lineWidth: 1, zIndex: 101 })
.state('inactive', { opacity: 0.5, zIndex: 100 });
}

drawSpacedPolar(0);
drawSpacedPolar(1);

chart.on('element:pointermove',e => {
const point = e.canvas;
data = chart.getDataByXY(point, { shared: true });
console.log(data);
});

chart.interaction('tooltip', {
body: false,
crosshairsStroke: 'red',
crosshairsStrokeWidth: 4,
});

chart.interaction('elementHighlight', true);

chart.render();
```

### Reproduction link / 复现链接

_No response_

### Steps to Reproduce the Bug or Issue / 重现步骤

_No response_

### Version / 版本

Please select / 请选择

### OS / 操作系统

- [ ] macOS
- [x] Windows
- [ ] Linux
- [ ] Others / 其他

### Browser / 浏览器

- [x] Chrome
- [ ] Edge
- [ ] Firefox
- [ ] Safari (Limited support / 有限支持)
- [ ] IE (Nonsupport / 不支持)
- [ ] Others / 其他

Contributor guide

Open the contributing guide

Research direction

Start with the supplied `spaceLayer` and multiple polar `interval` setup, then inspect the `element:pointermove` handler and its `chart.getDataByXY` call. Reproduce the behavior with the provided data in Chrome on Windows; done means tooltips can be triggered for both polar charts and the lookup returns the corresponding point data.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.