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

如何让Victory Native图表实现自然的惯性滚动?

给Victory柱状图开启平移惯性滚动的方法

要解决滑动松开后图表立即停止的问题,只需在VictoryZoomContainer组件中添加inertia={true}属性即可开启惯性滚动。如果需要调整惯性减速的快慢,还可以搭配friction参数(默认值0.9,数值越小减速越快)。

修改后的容器组件代码

const getContainerComponent = () => {
  return (
    <VictoryZoomContainer 
          allowZoom={false}
          allowPan={true}
          zoomDimension="x"
          zoomDomain={{x: [props.data.length - 7, props.data.length]}}
          minimumZoom={{x: 1}}
          clipContainerComponent={<VictoryClipContainer />}
          inertia={true} // 开启惯性滚动
          friction={0.85} // 可选:调整减速速率,按需修改
        />
  )
}

完整代码示例

const getContainerComponent = () => {
  return (
    <VictoryZoomContainer 
          allowZoom={false}
          allowPan={true}
          zoomDimension="x"
          zoomDomain={{x: [props.data.length - 7, props.data.length]}}
          minimumZoom={{x: 1}}
          clipContainerComponent={<VictoryClipContainer />}
          inertia={true}
          friction={0.85}
        />
  )
}

return ( 
  <VictoryChart 
    theme={theme}
    minDomain={{ y: 0 }}
    containerComponent={ getContainerComponent() }
    domainPadding={{ x : [10, 10] }}
    >
    <VictoryAxis 
      dependentAxis 
      tickFormat={(t: number) => `${Math.round(t)}`}
      crossAxis={false}
    />

    <VictoryAxis
      tickLabelComponent={ <VictoryLabel dy={10} />}
     />

    <VictoryBar 
      data={arrayOfData} 
      x="timePeriod"
      y="amount"
      alignment="middle"
      barWidth={25}
      cornerRadius={{ top:12, bottom: 12 }}
      />
  </VictoryChart> 
)

Chart

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 10:35:47