【问题标题】:Positioning icon in SVG in React NativeReact Native 中 SVG 中的定位图标
【发布时间】:2020-09-12 19:13:46
【问题描述】:

背景: 我正在尝试按照本教程向 react-native-svg 图表添加工具提示。 教程链接:Link

当前代码实现:

import React, {useState} from 'react';
import {View, Text, Dimensions} from 'react-native';
import {LineChart} from 'react-native-chart-kit';
import {Rect, Text as TextSVG, Svg} from 'react-native-svg';

const Charts = () => {
  let [tooltipPos, setTooltipPos] = useState({
    x: 0,
    y: 0,
    visible: false,
    value: 0,
  });

  return (
    <View>
      <LineChart
        data={{
          labels: ['January', 'February', 'March', 'April', 'May', 'June'],
          datasets: [
            {
              data: [100, 110, 90, 130, 80, 103],
            },
          ],
        }}
        width={Dimensions.get('window').width}
        height={250}
        yAxisLabel="$"
        yAxisSuffix="k"
        yAxisInterval={1}
        chartConfig={{
          backgroundColor: 'white',
          backgroundGradientFrom: '#fbfbfb',
          backgroundGradientTo: '#fbfbfb',
          decimalPlaces: 2,
          color: (opacity = 1) => `rgba(0, 0, 0, ${opacity})`,
          labelColor: (opacity = 1) => `rgba(0, 0, 0, ${opacity})`,
          style: {
            borderRadius: 0,
          },
          propsForDots: {
            r: '6',
            strokeWidth: '0',
            stroke: '#fbfbfb',
          },
        }}
        bezier
        style={{
          marginVertical: 8,
          borderRadius: 6,
        }}
        decorator={() => {
          return tooltipPos.visible ? (
            <View>
              <Svg>
                <Rect
                  x={tooltipPos.x - 15}
                  y={tooltipPos.y + 10}
                  width="40"
                  height="30"
                  fill="black"
                />
                <MaterialCommunityIcons
                  name="run"
                  size={32}
                  color="rgb(67, 67, 67)"
                />
                <TextSVG
                  x={tooltipPos.x + 5}
                  y={tooltipPos.y + 30}
                  fill="white"
                  fontSize="16"
                  fontWeight="bold"
                  textAnchor="middle">
                  {tooltipPos.value}
                </TextSVG>
              </Svg>
            </View>
          ) : null;
        }}
        onDataPointClick={(data) => {
          let isSamePoint = tooltipPos.x === data.x && tooltipPos.y === data.y;

          isSamePoint
            ? setTooltipPos((previousState) => {
                return {
                  ...previousState,
                  value: data.value,
                  visible: !previousState.visible,
                };
              })
            : setTooltipPos({
                x: data.x,
                value: data.value,
                y: data.y,
                visible: true,
              });
        }}
      />
    </View>
  );
};

问题: 我想添加如上图所示的图标(运行图标),位于工具提示文本旁边。 图标,然后将矩形内的文本填充为黑色。当我尝试定位它时,它出于某种原因出现在最左上角。如何定位?

【问题讨论】:

  • 一直在测试这个,当点击一个点时,工具提示会抛出一个错误。发送 repo 或一些示例代码?
  • 您好,感谢您抽出宝贵时间。这是一个有效的小吃链接:snack.expo.io/@vijayr0753/chart-draft PS:工具提示不会在点击浏览器时显示,如果您在设备上运行它确实有效。

标签: reactjs react-native tooltip react-native-svg react-native-chart-kit


【解决方案1】:

您可以使用 react-native-svg 中的 ForeignObject 组件并将您的 decorator 更改为以下内容:

decorator={() => {
  return tooltipPos.visible ? (
    <ForeignObject x={tooltipPos.x} y={tooltipPos.y}>
      <View
        style={{
          width: 70,
          flexDirection: 'row',
          backgroundColor: 'black',
        }}>
        <MaterialCommunityIcons
          name="run"
          size={32}
          color="rgb(67, 67, 67)"
        />
        <Text
          style={{
            color: 'white',
            fontSize: 16,
            fontWeight: 'bold',
            textAnchor: 'middle',
          }}>
          {tooltipPos.value}
        </Text>
      </View>
    </ForeignObject>
  ) : null;
}}

您之前遇到的问题是 react-native-svg TextRect 组件使用 xy 坐标,而您的图标没有,因此定位将关闭。

上述方法的好处是您只需指定ForeignObjectxy 属性。 ForeignObject 中的所有内容都可以是常规视图,按照您通常的位置 (https://github.com/react-native-community/react-native-svg#foreignobject)。

我为ForeignObjectxy 属性值分别选择了tooltipPos.xtooltipPos.y,但如果需要,您可以添加偏移量。


一定要从 react-native-svg 导入 ForeignObject

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-24
  • 2019-02-17
  • 1970-01-01
  • 2019-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多