我正在尝试使用本例中的react-native-svg-charts向我的面积图添加一个梯度:https://github.com/JesperLekland/react-native-svg-charts-examples/blob/master/storybook/stories/area-chart/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;发布于 2019-02-16 05:35:35
在您链接到Defs、LinearGradient和Stop组件的示例中,这些组件是从react-native-svg导入的。因此,这些组件可能是react-native-svg-charts库中的undefined,这可以解释您看到的错误。
尝试将您的导入修复为此(如果尚未安装react-native-svg,请安装它):
import { AreaChart } from 'react-native-svg-charts'
import { Defs, LinearGradient, Path, Stop } from 'react-native-svg'https://stackoverflow.com/questions/54642152
复制相似问题