react中使用echarts封装组件

react+antd 中使用echarts封装组件

下载依赖包

npm install echarts --save

封装组件

import React,{ Component } from 'react';
import echarts from 'echarts/lib/echarts';
import 'echarts/lib/chart/line';
import 'echarts/lib/chart/pie';  //  如需饼图需要在此引入饼图 ,同理其他类型图形需引入其他类型 ,如需世界地图,可引入 'echarts/map/js/world'
import 'echarts/lib/component/tooltip';  // 如有时发现option里面的某些配置项不起作用,需在此处引入
import 'echarts/lib/component/legend';

class EchartsTest extends Component {
	constructor(props) {
		super(props);
		let id = ('_' + Math.random()).replace('.','_');
		this.state = {
			chartId: 'chart' + id
		}
	}
	componentDidMount() {
		this.initChart(this.state.chartId)
	}
	initChart(id) {
		let myChart = echarts.getInstanceByDom(document.getElementById(id));
		if (myChart == undefined) {
			myChart = echarts.init(document.getElementById(id));
		}
		myChart.setOption(this.props.option,true)  // this.props.option 为父组件传的option配置 或者在此定义option配置 
	}
	componentDidUpdate() {
		this.initChart(this.state.chartId);
	}
	render() {
		const height = this.props.height;
		const width = this.props.width ? this.props.width:'98%';
		return (
			<div id={this.state.chartId} style={{width: width, height: height}}  ></div>
		)
	}
}
export default EchartsTest

// 父组件调用该组件
import EchartsTest from 'components/Echarts/lineCharts';  // 引入上述组件

const option = { echarts 配置项  }  //  将后台数据放入echarts配置项中组织好echarts配置项 此处省略可查看echarts官网option各类配置项
<EchartsTest option={option} width={'90%'} />
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值