You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native中点击VictoryArea区域如何获取Y轴数值?

问题描述

图表示例

希望点击VictoryArea区域时获取对应的Y轴数值,使用了如下props:

events={[{ target: 'data', eventHandlers: { onPressIn: (props) => { console.log(props); } } }]}

但在回调的props中找不到所需的Y轴数值。

解决方法

在Victory的事件回调参数里,props.datum字段包含了当前点击位置对应的原始数据点,直接从中提取Y轴字段即可(比如你的数据中Y值对应的键是y,就用props.datum.y)。

示例代码:

<VictoryArea
  // 假设你的数据结构是类似这样的数组
  data={[{ x: 1, y: 15 }, { x: 2, y: 25 }, { x: 3, y: 20 }]}
  events={[
    {
      target: 'data',
      eventHandlers: {
        onPressIn: (props) => {
          // 获取对应Y轴数值
          const yValue = props.datum.y;
          console.log('点击的Y轴数值:', yValue);
          return null;
        }
      }
    }
  ]}
/>

如果你的数据中Y值的键不是y(比如是value),替换成对应的键名即可。如果还是无法获取,检查以下几点:

  • 确认target: 'data'是否匹配需求,若点击的是区域空白处而非数据点,可尝试使用VictoryVoronoiContainer实现任意区域点击的数值拾取。
  • 检查数据是否正确传入VictoryArea组件,确保每个数据点都包含Y轴字段。

内容的提问来源于stack exchange,提问作者priyadarshi kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 15:02:16