echarts:tab切换效果

1、安装echarts:

npm install echarts --save

2、安装ant-design-vue:

yarn add ant-design-vue

(我用的tab组件是ant-desgin-vue的,所以要安装这个)
3、main.js引入以上两位

import Vue from 'vue'
import App from './App.vue'
import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';
Vue.use(Antd);
import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts
Vue.config.productionTip = false
 
new Vue({
  render: h => h(App),
}).$mount('#app')

4、.vue文件

<template>
	<div class="tab-box">
		<a-tabs class="tab" default-active-key="1" @change="callback">
			<a-tab-pane key="1" tab="Tab 1">
			</a-tab-pane>
			<a-tab-pane key="2" tab="Tab 2" force-render>
			</a-tab-pane>
			<a-tab-pane key="3" tab="Tab 3">
			</a-tab-pane>
		</a-tabs>
		<div id="chart"></div>
	</div>
 
</template>
<script>
	export default {
		data() {
			return {
				option: [],
				// chart1的配置
				option1: {
					// 去掉动画效果
					animation: false,
					title: {
						text: '基础雷达图'
					},
					tooltip: {},
					legend: {
						data: ['预算分配(Allocated Budget)', '实际开销(Actual Spending)']
					},
					radar: {
						// shape: 'circle',
						name: {
							textStyle: {
								color: '#fff',
								backgroundColor: '#999',
								borderRadius: 3,
								padding: [3, 5]
							}
						},
						indicator: [{
								name: '销售(sales)',
								max: 6500
							},
							{
								name: '管理(Administration)',
								max: 16000
							},
							{
								name: '信息技术(Information Techology)',
								max: 30000
							},
							{
								name: '客服(Customer Support)',
								max: 38000
							},
							{
								name: '研发(Development)',
								max: 52000
							},
							{
								name: '市场(Marketing)',
								max: 25000
							}
						]
					},
					series: [{
						name: '预算 vs 开销(Budget vs spending)',
						type: 'radar',
						// areaStyle: {normal: {}},
						data: [{
								value: [4300, 10000, 28000, 35000, 50000, 19000],
								name: '预算分配(Allocated Budget)'
							},
							{
								value: [5000, 14000, 28000, 31000, 42000, 21000],
								name: '实际开销(Actual Spending)'
							}
						]
					}]
				},
				// chart2的配置
				option2: {
					// 去掉动画效果
					animation: false,
					title: {
						text: '折线图堆叠'
					},
					tooltip: {
						trigger: 'axis'
					},
					legend: {
						data: ['邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎']
					},
					grid: {
						left: '3%',
						right: '4%',
						bottom: '3%',
						containLabel: true
					},
					toolbox: {
						feature: {
							saveAsImage: {}
						}
					},
					xAxis: {
						type: 'category',
						boundaryGap: false,
						data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
					},
					yAxis: {
						type: 'value'
					},
					series: [{
							name: '邮件营销',
							type: 'line',
							stack: '总量',
							data: [120, 132, 101, 134, 90, 230, 210]
						},
						{
							name: '联盟广告',
							type: 'line',
							stack: '总量',
							data: [220, 182, 191, 234, 290, 330, 310]
						},
						{
							name: '视频广告',
							type: 'line',
							stack: '总量',
							data: [150, 232, 201, 154, 190, 330, 410]
						},
						{
							name: '直接访问',
							type: 'line',
							stack: '总量',
							data: [320, 332, 301, 334, 390, 330, 320]
						},
						{
							name: '搜索引擎',
							type: 'line',
							stack: '总量',
							data: [820, 932, 901, 934, 1290, 1330, 1320]
						}
					]
				},
				// chart3的配置
				option3: {
					// 去掉动画效果
					animation: false,
					color: ['#80FFA5', '#00DDFF', '#37A2FF', '#FF0087', '#FFBF00'],
					title: {
						text: '渐变堆叠面积图'
					},
					tooltip: {
						trigger: 'axis',
						axisPointer: {
							type: 'cross',
							label: {
								backgroundColor: '#6a7985'
							}
						}
					},
					legend: {
						data: ['Line 1', 'Line 2', 'Line 3', 'Line 4', 'Line 5']
					},
					toolbox: {
						feature: {
							saveAsImage: {}
						}
					},
					grid: {
						left: '3%',
						right: '4%',
						bottom: '3%',
						containLabel: true
					},
					xAxis: [{
						type: 'category',
						boundaryGap: false,
						data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
					}],
					yAxis: [{
						type: 'value'
					}],
					series: [{
							name: 'Line 1',
							type: 'line',
							stack: '总量',
							smooth: true,
							lineStyle: {
								width: 0
							},
							showSymbol: false,
							areaStyle: {
								opacity: 0.8,
								color: this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
									offset: 0,
									color: 'rgba(128, 255, 165)'
								}, {
									offset: 1,
									color: 'rgba(1, 191, 236)'
								}])
							},
							emphasis: {
								focus: 'series'
							},
							data: [140, 232, 101, 264, 90, 340, 250]
						},
						{
							name: 'Line 2',
							type: 'line',
							stack: '总量',
							smooth: true,
							lineStyle: {
								width: 0
							},
							showSymbol: false,
							areaStyle: {
								opacity: 0.8,
								color: this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
									offset: 0,
									color: 'rgba(0, 221, 255)'
								}, {
									offset: 1,
									color: 'rgba(77, 119, 255)'
								}])
							},
							emphasis: {
								focus: 'series'
							},
							data: [120, 282, 111, 234, 220, 340, 310]
						},
						{
							name: 'Line 3',
							type: 'line',
							stack: '总量',
							smooth: true,
							lineStyle: {
								width: 0
							},
							showSymbol: false,
							areaStyle: {
								opacity: 0.8,
								color: this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
									offset: 0,
									color: 'rgba(55, 162, 255)'
								}, {
									offset: 1,
									color: 'rgba(116, 21, 219)'
								}])
							},
							emphasis: {
								focus: 'series'
							},
							data: [320, 132, 201, 334, 190, 130, 220]
						},
						{
							name: 'Line 4',
							type: 'line',
							stack: '总量',
							smooth: true,
							lineStyle: {
								width: 0
							},
							showSymbol: false,
							areaStyle: {
								opacity: 0.8,
								color: this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
									offset: 0,
									color: 'rgba(255, 0, 135)'
								}, {
									offset: 1,
									color: 'rgba(135, 0, 157)'
								}])
							},
							emphasis: {
								focus: 'series'
							},
							data: [220, 402, 231, 134, 190, 230, 120]
						},
						{
							name: 'Line 5',
							type: 'line',
							stack: '总量',
							smooth: true,
							lineStyle: {
								width: 0
							},
							showSymbol: false,
							label: {
								show: true,
								position: 'top'
							},
							areaStyle: {
								opacity: 0.8,
								color: this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
									offset: 0,
									color: 'rgba(255, 191, 0)'
								}, {
									offset: 1,
									color: 'rgba(224, 62, 76)'
								}])
							},
							emphasis: {
								focus: 'series'
							},
							data: [220, 302, 181, 234, 210, 290, 150]
						}
					]
				}
			};
		},
		created() {
			this.option = [this.option1, this.option2, this.option3];
		},
		mounted() {
			this.drawEcharts(this.option[0]);
		},
		methods: {
			callback(key) {
				console.log(key);
				this.drawEcharts(this.option[key-1]);
			},
			drawEcharts(option) {
				// 基于准备好的dom,初始化echarts实例
				var myChart = this.$echarts.init(document.getElementById("chart"));
				// 使用刚指定的配置项和数据显示图表。
				myChart.clear();
				this.option && myChart.setOption(option);
			}
		},
	};
</script>
<style>
	.tab-box {
		width: 1000px;
		height: 500px;
		margin: 10px auto;
	}
 
	#chart {
		width: 100%;
		height: 100%;
	}
</style>

切换echart的本质,是切换option。

在created里面初始化option数组,在mounted里面初始化echart,否则会一直报invalid dom。

最后就是每次绘制echart前,要clear一下,否则图会重叠在一起。
在这里插入图片描述
原文:https://blog.csdn.net/lilycheng1986/article/details/116234316

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 ECharts 中实现多Tab切换,可以通过以下步骤实现: 1. 创建一个包含多个Tab的容器,比如使用Bootstrap的Tab组件或自定义实现一个Tab容器。 2. 在每个Tab面板中创建一个ECharts实例对象。 3. 为每个Tab面板的ECharts实例对象设置不同的数据和配置,以展示不同的图表。 4. 在Tab切换时,根据切换的面板,重新渲染对应的ECharts实例对象。 以下是一个简单的示例代码: HTML代码: ``` <div class="tab-container"> <!-- Tab导航 --> <ul class="nav nav-tabs"> <li class="active"><a href="#tab1">Tab 1</a></li> <li><a href="#tab2">Tab 2</a></li> <li><a href="#tab3">Tab 3</a></li> </ul> <!-- Tab面板 --> <div class="tab-content"> <div class="tab-pane active" id="tab1"> <div id="chart1" style="height: 400px;"></div> </div> <div class="tab-pane" id="tab2"> <div id="chart2" style="height: 400px;"></div> </div> <div class="tab-pane" id="tab3"> <div id="chart3" style="height: 400px;"></div> </div> </div> </div> ``` JavaScript代码: ``` // 初始化Tab组件 $('.nav-tabs a').click(function(e) { e.preventDefault(); $(this).tab('show'); }); // 初始化ECharts实例对象 var chart1 = echarts.init(document.getElementById('chart1')); var chart2 = echarts.init(document.getElementById('chart2')); var chart3 = echarts.init(document.getElementById('chart3')); // 设置不同的数据和配置 var option1 = {...}; var option2 = {...}; var option3 = {...}; chart1.setOption(option1); chart2.setOption(option2); chart3.setOption(option3); ``` 当Tab切换时,可以使用ECharts的`setOption`方法重新渲染对应的图表。例如,当切换Tab 2时,可以使用以下代码重新渲染第二个图表: ``` $('.nav-tabs a[href="#tab2"]').on('shown.bs.tab', function () { chart2.resize(); // 重新调整图表大小 chart2.setOption(option2); // 重新渲染图表 }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值