zoukankan      html  css  js  c++  java
  • react 中Echarts不自适应问题

    import * as React from 'react'
    import Echarts from 'echarts'
    
    export interface IProps {
    
    }
    interface IState {
        myEchart?: any
    }
    
    class CapitalBudget extends React.Component<IProps, IState> {
        LineRef: any = undefined
        constructor(props: IProps) {
            super(props)
            this.state = {
                myEchart: null
            }
            this.LineRef = React.createRef()
        }
        componentDidMount() {
            const myEchart = Echarts.init(document.getElementById('echart-Line'))
            const option = {
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    axisLabel: {
                        formatter: '{value} 年'
                    },
                    axisTick: {
                        alignWithLabel: true
                    },
                    data: ['2012', '2013', '2014', '2015', '2016', '2017', '2018']
                },
                yAxis: {
                    type: 'value',
                    axisLabel: {
                        formatter: '{value} 万'
                    },
                },
                series: [{
                    data: [820, 932, 901, 934, 1290, 1330, 1320],
                    type: 'line'
                }]
            }
            this.setState({ 
                myEchart, 
             })
            myEchart.setOption(option)
            // window.addEventListener('resize', () => {
            //     myEchart.resize()
            // })
        }
         // 获取Echart图宽高
         getEchartWidth = () => { 
            this.LineRef.current.style.width = this.LineRef.current.offsetWidth
            this.LineRef.current.style.height = this.LineRef.current.offsetHeight     
               this.resized()      
        }
        componentDidUpdate() {  //点击菜单栏触发这个事假
            setTimeout(() => {
                 this.getEchartWidth()  // 延迟获取容器的宽高,缺点有移动效果
             }, 200)
         }
         resized = () => {  
             const { myEchart } = this.state
              myEchart.resize()
         }
        render() {
            return (
                <div ref ={this.LineRef} id='echart-Line' style={{ height: '236px' }} />     
            )
        }
    }
    
    export default CapitalBudget
    

      

  • 相关阅读:
    SpringMVC ModelAndView方法与模板传参接收不到问题
    DB2单个DB重启
    DB2 数据库绑定用户授权命令
    [转]sublime配置全攻略
    [转]Sublime text2安装php beautifier
    [转]php及xdebug使用小结
    [转]Pear、PHPUnit安装
    [转]WIN下成功安装PEAR
    [转]PHP单元测试利器:PHPUNIT初探
    [转]sublime使用xdebug调试php
  • 原文地址:https://www.cnblogs.com/whlBooK/p/11171356.html
Copyright © 2011-2022 走看看