react中echarts让两个或多个图联动

前言:

最近在做一个react项目,涉及到统计模块,让两个折线图一起联动,拖动一个,另一个跟着动。

实现过程:

效果图:

效果图

就是拖动底下的滑块(echarts专业术语dataZoom),上下两个图一起动。(不光dataZoom,tootltip等都可以联动。

实现思路就是使用init实例化两个图表,然后用connect链接实现。具体配置项可查看官方API

上代码:

引入echarts:import echarts from 'echarts';

 render() {
    return (
      <div className={styles.dpmain}>
          <div id="leftTj"  style={{ width: '100%', height: 200 }}></div>//实例化统计图挂在在这上面
          <div id="rightTj"  style={{ width: '100%', height: 200 }}></div>
      
        {/* </div> */}
        </div>
      </div>
    )
  }

然后再componentDidMount()生命周期里写如下代码

 var myChart1 = echarts.init(document.getElementById('leftTj'));//init实例化统计图
 var myChart2 = echarts.init(document.getElementById('rightTj'));
var option1=	{textStyle: {
			color: '#545454',
			fontFamily: 'Source Han Sans',
			fontWeight: 'lighter'
	},
	tooltip: {
		trigger: 'axis',
		axisPointer: {
				type: 'cross',
				crossStyle: {
						color: '#999'
				}
		}
},
	legend: {
		data: ['蓝', '红'],
		left: '80%',
		top:'2%',
		itemWidth: 10,
		itemHeight: 5
	},
	xAxis: {
		type: 'category',
		data:xAxisDatas,
		axisLabel:{
            margin:"-50",
		},
		name:"线路左",
        nameLocation:"start",
        axisTick:{
			alignWithLabel:true,
			inside:true
        },
},
	yAxis: {
		name: 'mm/y',
		type: 'value',
		splitLine:{
            show:false
        },
},
dataZoom: [
	{
		show: false,
		realtime: true,
		start: 0,
		end: 30
	},
	{
		type: 'inside',
		realtime: true,
		start: 0,
		end: 30
	}
],
series:series
	}
}
var option2={
	textStyle: {
			color: '#545454',
			fontFamily: 'Source Han Sans',
			fontWeight: 'lighter'
	},
	tooltip: {
		trigger: 'axis',
		axisPointer: {
				type: 'cross',
				crossStyle: {
						color: '#999'
				}
		}
},
	legend: {
		data: ['蓝', '红'],
		left: '80%',
		top:'2%',
		itemWidth: 10,
		itemHeight: 5
	},
	xAxis: {
		type: 'category',
		data:xAxisDatas,
		axisLabel:{
            margin:"-50",
		},
		name:"线路左",
        nameLocation:"start",
        axisTick:{
			alignWithLabel:true,
			inside:true
        },
},
	yAxis: {
		name: 'mm/y',
		type: 'value',
		splitLine:{
            show:false
        },
},
dataZoom: [
	{
		show: true,
		realtime: true,
		start: 0,
		end: 30
	},
	{
		type: 'inside',
		realtime: true,
		start: 0,
		end: 30
	}
],
series:series
	}
}
myChart1.setOption(option1);
myChart2.setOption(option2);
echarts.connect([myChart2,myChart1]);//连接起来

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
首先,你需要在 React 项目安装 echarts: ``` npm install echarts --save ``` 然后,在需要使用饼的组件引入 echarts,并在组件的生命周期函数 `componentDidMount` 初始化 echarts 实例,并使用数据渲染饼。 例如,以下是一个简单的饼组件的代码: ``` import React, { Component } from 'react'; import echarts from 'echarts'; class PieChart extends Component { componentDidMount() { this.initChart(); } initChart = () => { const { data } = this.props; const chart = echarts.init(this.chartRef); chart.setOption({ tooltip: { trigger: 'item', formatter: '{a} <br/>{b}: {c} ({d}%)', }, series: [ { name: '访问来源', type: 'pie', radius: ['50%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center', }, emphasis: { label: { show: true, fontSize: '30', fontWeight: 'bold', }, }, labelLine: { show: false, }, data, }, ], }); }; render() { return ( <div ref={(ref) => { this.chartRef = ref; }} style={{ width: '100%', height: '300px' }} /> ); } } export default PieChart; ``` 在上面的代码,我们使用 `componentDidMount` 函数初始化 echarts 实例,并使用传递进来的数据渲染饼。注意,我们在组件的 `render` 函数返回一个 `div` 元素,这个元素的 `ref` 属性绑定了一个回调函数,用来获取这个元素的引用,以便后续使用 echarts 来渲染表。 在父组件,我们可以使用以下代码来渲染这个饼组件: ``` import React, { Component } from 'react'; import PieChart from './PieChart'; class App extends Component { state = { data: [ { value: 335, name: '直接访问' }, { value: 310, name: '邮件营销' }, { value: 234, name: '联盟广告' }, { value: 135, name: '视频广告' }, { value: 1548, name: '搜索引擎' }, ], }; render() { const { data } = this.state; return ( <div> <PieChart data={data} /> </div> ); } } export default App; ``` 在父组件,我们传递一个 `data` 属性给饼组件,这个属性包含了用于渲染饼的数据。最终,我们可以在页面看到一个简单的饼
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

崽崽的谷雨

漫漫前端路,摸爬滚打

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

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

打赏作者

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

抵扣说明:

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

余额充值