0

在 VictoryPie 图表中需要使用 CustomLabel 显示 VictoryTooltip 和 VictoryLabel 但 CustomLabel 组件仅显示 VictoryLabel而不是 VictoryTooltip

VictoryTooltip 和 VictoryLabel 与直接使用labelComponent道具
示例一起工作正常: labelComponent={<VictoryTooltip />}labelComponent={<VictoryLabel />}

这是代码

import React from 'react'
import { StyleSheet, View, Text } from 'react-native'
import {
 VictoryChart,
 VictoryTheme,
 VictoryPie,
 VictoryAxis,
 VictoryTooltip,
 VictoryLabel,
} from 'victory-native'
import { useSelector } from 'react-redux'
import { useTranslation } from 'react-i18next'
import Svg, {G} from 'react-native-svg'

// This is CustomLabel which include VictoryLabel and VictoryTooltip
const CustomLabel = (props) => {
const { datum, text, y, style } = props
const { t, i18n } = useTranslation()
const selectedLanguage = i18n.language
return (
  <G>
    <VictoryLabel
      {...props}
      text={`${datum.displayName[selectedLanguage]} [${datum.percent.toFixed(0)}%]`}
    />
    <VictoryTooltip
      {...props}
      label={`${text}\n`}
      renderInPortal={false}
      x={200} y={250}
      orientation="top"
      pointerLength={0}
      cornerRadius={50}
      flyoutWidth={100}
      flyoutHeight={100}
      flyoutStyle={{ fill: "red" }}
    />
  </G> 
 )
}

const Chart = ({pieAnalysisData}) => {
const { minusTags } = useSelector((state) => state.settings)
const { t, i18n } = useTranslation()
const selectedLanguage = i18n.language
return (
  <View style={styles.container}>
    <View>
        <VictoryChart
          padding={{ top: 0, bottom: 10, right: 110, left: 110 }}
          theme={VictoryTheme.material}
        >
          <VictoryPie
            data={pieAnalysisData.minusData || []}
            labels={({ datum }) => {
              let label = ''
              for(let i of datum.tags) {
                let key = Object.keys(i)[0]
                label = label + '\n' + `${minusTags[selectedLanguage][key]}: [${i[key].toFixed(0)}%]`
              }
              return label
              }                    
            }
            padding={0}
            innerRadius={1}
            labelRadius={105}
            padAngle={1}
            animate={{
              duration: 2000,
            }}
            labelComponent={<CustomLabel />}
          />
        </VictoryChart>
    </View>
  </View>
 )
}

export default Chart

这是没有显示工具提示的 ios 模拟器的屏幕截图 在此处输入图像描述

4

1 回答 1

0

通过VictoryVoronoiContainer作为containerComponentprop in传递来显示工具提示VictoryChart

containerComponent={
   <VictoryVoronoiContainer/>
}
于 2021-12-31T02:33:12.773 回答