react-native 一个页面使用多个Echarts图表 图表总有1、2个不显示 但数据是请求到后才渲染的图表

如下图所示,一共使用了2个图表(1柱状图、1饼图),正常情况是这样的: 

是的,2个图表都出现了,但是有时候图表只展示1个,有时候2个都不展示,如下图:

 

为什么会出现这种现象?

经排查,由于我们的echarts图表是用webview展示的,并且封装成了一个组件供调用,当多个组件同时调用echarts时,它只能处理一个,于是另外一个就出现渲染不出图表的情况,我称之为“竞引”

如何解决?使用setTimeout延迟另外一个图表的渲染

<DelayChart height={200} delay={250}>

        <Charts cellType='pie' chartOption={chartOption} chartHeight={200} />

</DelayChart>

class DelayChart extends Component<{

  delay?: number,

  height?: number,

}, any> {

  state = {

    show: false

  }

  componentDidMount() {

    setTimeout(() => {

      this.setState({

        show: true

      })

    }, this.props.delay || 500);

  }

  render() {

    let { show } = this.state;

    return !show ? <View style={[{ height: this.props.height || 0 }]}></View> : <View>{this.props.children}</View>

  }

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果 react-native-echarts-pro 组件在 React Native 中的图表溢出容器,可以通过以下两种方法进行解决: 1.设置容器的大小 可以在容器组件中设置宽度和高度,然后将宽度和高度作为 props 传递给 react-native-echarts-pro 组件。例如: ``` import React from 'react'; import { View, Dimensions } from 'react-native'; import Echarts from 'react-native-echarts-pro'; const windowWidth = Dimensions.get('window').width; const windowHeight = Dimensions.get('window').height; export default function App() { return ( <View style={{ width: windowWidth, height: windowHeight }}> <Echarts option={option} width={windowWidth} // 设置宽度 height={windowHeight} // 设置高度 /> </View> ); } ``` 2.使用 onLayout 事件设置图表的大小 可以在 react-native-echarts-pro 组件中使用 onLayout 事件来获取容器组件的宽度和高度,然后将宽度和高度设置给图表。例如: ``` import React, { useState } from 'react'; import { View, Dimensions } from 'react-native'; import Echarts from 'react-native-echarts-pro'; export default function App() { const [chartWidth, setChartWidth] = useState(0); const [chartHeight, setChartHeight] = useState(0); const onLayout = event => { const { width, height } = event.nativeEvent.layout; setChartWidth(width); setChartHeight(height); }; return ( <View style={{ flex: 1 }} onLayout={onLayout}> <Echarts option={option} width={chartWidth} height={chartHeight} /> </View> ); } ``` 以上两种方法均可将 react-native-echarts-pro 组件的大小设置为自适应容器的大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hzxOnlineOk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值