我react-native-chart-kit
在 React Native Expo 应用程序中使用来呈现图表。文档中提供的示例工作正常
问题:在我的应用程序中,需要图表来填充父容器,但似乎图表必须将height
道具作为固定值。
<LineChart
height={221}
...
/>
尝试将height
值设置为100%
或删除定义将导致与 NaN 相关的错误
Invariant Violation: [.....,"y1":"<>","x2":"375","y2":"<>"}] 不能用作本机方法参数
这个问题有解决方案吗?谢谢!
基于文档的工作示例
export default class Chart extends Component {
render() {
const data = {
datasets: [{
data: [
Math.random() * 100,
Math.random() * 100,
Math.random() * 100,
Math.random() * 100,
Math.random() * 100,
Math.random() * 100
]
}]
}
return (
<LineChart
data={data}
width={Dimensions.get('window').width}
height={221}
yAxisLabel={'$'}
chartConfig={{
backgroundColor: '#e26a00',
backgroundGradientFrom: '#fb8c00',
backgroundGradientTo: '#ffa726',
decimalPlaces: 2, // optional, defaults to 2dp
color: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
style: {
borderRadius: 16
}
}}
bezier
style={{
marginVertical: 8,
borderRadius: 16
}}
/>
)
}
}
@hong 开发
该react-native-chart-kit
组件<LineChart />
可以是另一个高度可变的组件的子组件。所以<LineChart />
不一定有 给出的高度Dimensions.get('window').height;
。例如,
render() {
<View>
<Header />
{ this.props.foo ? this.renderFoo() : null }
<View>
<LineChart
...
/>
</View>
</View>
}