1

我正在尝试使用此示例中的 react-native-svg-charts 向我的面积图添加渐变:https ://github.com/JesperLekland/react-native-svg-charts-examples/blob/master/storybook/故事/面积图/with-gradient.js

代码编译但我在运行时收到以下错误:

https://imgur.com/nasKm9x(对不起,我还没有足够的声誉来发布图片)

我在我的 Android 手机上使用 Expo。我对编程很陌生,所以我不知道该尝试什么。

import * as shape from 'd3-shape'
import { View } from 'native-base';
import * as React from 'react';
import { AreaChart, Defs, LinearGradient, Path, Stop } from 'react-native-svg-charts'
// import styles from './styles';

class DashboardChart extends React.Component {
  render() {
    const data = [ 50, 10, 40, 95, 14, 24, 85, 91, 35, 53, 53, 24, 50, 20, 80 ]

        const ChartLine = ({line}) => (
            <Path
                key={'line'}
                d={line}
                stroke={'rgb(134, 65, 244)'}
                fill={'none'}
            />
        )

        const Gradient = ({ index }) => (
          <Defs key={index}>
              <LinearGradient id={'gradient'} x1={'0%'} y={'0%'} x2={'0%'} y2={'100%'}>
                  <Stop offset={'0%'} stopColor="blue" stopOpacity={1}/>
                  <Stop offset={'100%'} stopColor="white" stopOpacity={1}/>
              </LinearGradient>
          </Defs>
      )

    return (
          <View>
            <AreaChart
                style={{ height: 200 }}
                data={data}
                contentInset={{ top: 40, bottom: 10 }}
                curve={shape.curveNatural}
                svg={{
                  fill: 'url(#gradient)'
                }}
            >
                {/* <Grid/> */}
                <Gradient/>
                <ChartLine/>
            </AreaChart>
          </View>
    );
  }

}

export default DashboardChart;
4

1 回答 1

0

在您链接到 、 和 的示例中DefsLinearGradient组件Stop是从 导入的react-native-svg。所以这些组件可能undefinedreact-native-svg-charts库中,这可以解释您看到的错误。

尝试修复您的导入(react-native-svg如果您还没有安装):

import { AreaChart } from 'react-native-svg-charts'
import { Defs, LinearGradient, Path, Stop } from 'react-native-svg'
于 2019-02-15T21:35:35.423 回答