Skip to content

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

@anchor-sherry

Description

@anchor-sherry

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
  • Windows
  • Linux
  • Others / 其他

Browser / 浏览器

  • Chrome
  • Edge
  • Firefox
  • Safari (Limited support / 有限支持)
  • IE (Nonsupport / 不支持)
  • Others / 其他

Metadata

Metadata

Assignees

No one assigned

    Labels

    waiting for maintainerTriage or intervention needed from a maintainer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions