ant-design / ant-design-charts

A React Chart Library
https://ant-design-charts.antgroup.com/
MIT License
1.91k stars 361 forks source link

🧐[问题] 当点击条形图中柱子以外的区域,选中状态会被清除 #2588

Open TonyZhang1993 opened 2 months ago

TonyZhang1993 commented 2 months ago

🧐 问题描述 [详细地描述问题,让大家都能理解]

背景:为条形图设置了 elementSelect,当点击图中柱子会根据配置高亮展示,重复点击会清除选中状态

问题: 偶然发现当点击图表中 柱子以外的区域,选中状态有时会被清除,有时不会清除,如点击柱子附近【会清除】 和 点击坐标轴附近【不会清除】 柱状图也有同样问题,怀疑是其他图表也有,应该和图表事件有关

https://github.com/ant-design/ant-design-charts/assets/22140493/7bd70fc9-ded3-45f5-be55-934bb72e3717

tip: 注意浏览器控制台里的输出

💻 示例代码 [如果有必要,展示代码,线上示例,或仓库]

import { Bar } from '@ant-design/plots';
import React from 'react';
import ReactDOM from 'react-dom';

const DemoBar = () => {
  const config = {
    data: {
      type: 'fetch',
      value: 'https://render.alipay.com/p/yuyan/180020010001215413/antd-charts/bar-bar.json',
    },
    xField: 'letter',
    yField: 'frequency',
    sort: {
      reverse: true,
    },
  state: {
    unselected: { opacity: 0.5 },
    selected: { fill: '#0098FF' },
    active: { fill: '#52C5FF' },
  },
  interaction: {
    elementSelect: true,
    elementHighlight: false,
    elementHighlightByColor: false,
  },
  onReady: ({ chart }) => {

      chart.on('plot:click', (ev) => {
        //  一直都是空的
        console.log('plot', ev)
      })
      //  图表点击事件响应
      chart.on(`element:click`, (ev) => {
          console.log('element', ev)

      })
    },
    label: {
      text: 'frequency',
      formatter: '.1%',
      style: {
        textAnchor: (d) => (+d.frequency > 0.008 ? 'right' : 'start'),
        fill: (d) => (+d.frequency > 0.008 ? '#fff' : '#000'),
        dx: (d) => (+d.frequency > 0.008 ? -5 : 5),
      },
    },
    axis: {
      y: {
        labelFormatter: '.0%',
      },
    },
  };
  return <Bar {...config} />;
};

ReactDOM.render(<DemoBar />, document.getElementById('container'));

🚑 其他信息 [如截图等其他信息可以贴在这里]

期望: 1 图表内点击除了柱子区域,不会清除选中状态; 2 如果有可能被清除,可以通过监听什么方法获取 高亮状态被清除与否;目前通过监听 'plot:click' 事件都会响应图表内任意位置的点击,但是选中状态 不一定都会被清除,如点击柱子附近【会清除】 和 点击坐标轴附近【不会清除】

jsteenkamp commented 2 weeks ago

Related. I would like to know how to select and element programmatically and have the element style show the selected state styles. You can select an element using chart.emit('interval:click', { ... }) however the element will not show the selected state style, and other elements will not show the unselected styles.