echarts模板化开发,简易版配置大屏组件-根据配置文件输出图形和模板(vue2+echarts5.0)

news2025/4/23 16:05:00

实现结果
在这里插入图片描述
项目结构
在这里插入图片描述
根据我的目录和代码 复制到项目中

echartsTemplate-echarts图形

pie实例

<template>
	<div :id="echartsId"></div>
</template>
<script>
export default {
	name: '',
	components: {},
	mixins: [],
	props: ['echartsId', 'echartsJsonData'],
	data() {
		return {};
	},
	computed: {},
	watch: {
		echartsJsonData: {
			handler(to) {
				// console.log(to, 'to');

				if (to) {
					let _this = this;
					this.$nextTick(() => {
						_this.getEcharts();
					});
				}
			},
			immediate: true,
		},
	},
	mounted() {},
	methods: {
		getEcharts() {
			const pieCharts = this.$echarts.init(
				document.querySelector('#' + this.echartsId)
			);
			let chartData = [
				95.65, 96.15, 96.82, 97.45, 97.85, 99.23, 99.79, 100.77, 100.03,
				101.75, 101.42, 101.54, 101.24, 102.65, 103.08, 103.22, 102.87,
				101.86, 101.3, 100.04, 101.08, 98.58, 98.42, 99, 98.94, 99.21,
				99.29, 100.2, 100.41, 100.26, 101.88, 103.26, 105.1, 109.39,
				108.48, 108.71, 108.18, 109.15, 109.6, 106.76, 107.35, 106.77,
				105.99, 105.48, 104.52, 104.25, 104.94, 103.35, 103.55,
			];
			const xData = Array.from(
				{ length: chartData.length },
				(_, index) => index
			);
			let options = {
				tooltip: {
					trigger: 'axis',
					axisPointer: {
						type: 'cross',
					},
				},
				grid: {
					top: 10,
					left: 10,
					bottom: 10,
					right: 10,
					containLabel: true,
				},
				xAxis: {
					type: 'category',
					data: xData,
				},
				yAxis: {
					scale: true,
				},
				series: [
					{
						symbolSize: 10,
						data: xData.map((x, i) => [x, chartData[i]]),
						type: 'scatter',
					},
				],
			};
			// 渲染 echarts
			pieCharts.setOption(options, true);
			pieCharts.resize();
		},
	},
};
</script>
<style lang="" scoped></style>

dataHandle.js-数据处理方法

export const handleSlpmle = (data) => {
	// console.log(data);
	return data;
};

echartsGet.vue-模板调用,图形模板根据配置文件调用

<template>
	<div>
		<!-- 如果echartsJsonSet useFlag等于true的话那就是省份的特殊样式-->
		<div
			v-if="echartsJsonSet"
			style="position: relative"
			:style="{
				height: echartsJsonSet.height,
				width: echartsJsonSet.width,
			}"
			id="combatTable"
			:class="
				echartsJsonSet.useFlag
					? `${echartsJsonSet.tempId} ${echartsJsonSet.tempId}_${echartsJsonSet.value}`
					: `${echartsJsonSet.tempId}`
			"
		>
			<!-- 使用 component 标签配合 is 属性 -->
			<div v-if="echartsJsonSet">
				<component
					v-for="(item, index) in echartsJsonSet.echartsList"
					:key="index"
					:is="item.type"
					:echartsId="item.tempName"
					:echartsJsonData="item"
					:style="{
						width: item.width,
						height: item.height,
					}"
					:class="`${item.tempName} echartsList`"
				></component>
			</div>
		</div>
	</div>
</template>
<script>
import pie from './echartsTemplate/pie.vue';
import areaEcharts from './echartsTemplate/areaEcharts.vue';
import bar from './echartsTemplate/bar.vue';
import lineBar from './echartsTemplate/lineBar.vue';
import lineEcharts from './echartsTemplate/lineEcharts.vue';
import mapEcharts from './echartsTemplate/mapEcharts.vue';
import radar from './echartsTemplate/radar.vue';
import stacked from './echartsTemplate/stacked.vue';
import trabecula from './echartsTemplate/trabecula.vue';
export default {
	name: '',
	components: {
		pie,
		areaEcharts,
		bar,
		lineBar,
		lineEcharts,
		mapEcharts,
		radar,
		stacked,
		trabecula,
	},
	mixins: [],
	// 'echartsJson'标准模板,默认值 'areaEchartsJson'地区模板,如果某个地区的某个图需要特殊方式显示
	props: ['echartsJson'],
	data() {
		return { echartsJsonSet: null };
	},
	computed: {},
	watch: {
		echartsJson: {
			handler(to) {
				if (to) {
					console.log(to, 'ttotooto');

					this.echartsJsonSet = to;
				}
			},
			immediate: true,
		},
	},
	mounted() {},
	methods: {},
};
</script>
<style lang="" scoped></style>

echartsTempCss.less模板css,根据规则生成的class名

.G000 {
	background: rgba(175, 199, 223);
	.echartsList {
		position: absolute;
	}
	.G00001 {
		top: 10px;
		left: 10px;
	}
	.G00002 {
		top:201px;
		left: 10px;
	}
	.G00003 {
		top: 10px;
		left: 267.5px;
	}
	.G00004 {
		top: 392px;
		left: 10px;
		border: 1px solid rgb(245, 246, 247);
	}
	.G00005 {
		top: 392px;
		left: 267.5px;
		border: 1px solid rgb(245, 246, 247);
	}
	.G00006 {
		top: 392px;
		left: 525px;
		border: 1px solid rgb(245, 246, 247);
	}
	.G00007 {
		top: 10px;
		left: 782.5px;
		border: 1px solid rgb(245, 246, 247);
	}
	.G00008 {
		top:201px;
		left: 782.5px;
		border: 1px solid rgb(245, 246, 247);
	}
	.G00009 {
		top: 392px;
		left: 782.5px;
		border: 1px solid rgb(245, 246, 247);
	}
}

functionEcharts.vue-操作按钮

<template>
	<div>
		<div class="block">
			<span class="demonstration"></span>
			<el-date-picker
				v-model="timeValue"
				type="month"
				placeholder="选择月"
			>
			</el-date-picker>
		</div>
		<el-select v-model="selectValue" placeholder="请选择">
			<el-option
				v-for="item in option"
				:key="item.value"
				:label="item.label"
				:value="item.value"
			>
			</el-option>
		</el-select>
		<el-button @click="query">查询</el-button>
		<!-- <div @click="generateAndDownloadReport">下载</div> -->
	</div>
</template>
<script>
import html2canvas from 'html2canvas';
import provinceSetting from './provinceSetting.json';
export default {
	name: '',
	components: {},
	mixins: [],
	props: {},
	data() {
		return {
			selectValue: '100000',
			timeValue: '2024-06',
			option: provinceSetting,
		};
	},
	computed: {},
	watch: {},
	mounted() {},
	methods: {
		query() {
			this.$eventBus.$emit('realDatas', {
				time: this.formatDate(this.timeValue),
				province: this.selectValue,
			});
		},
		formatDate(dateString) {
			try {
				const date = new Date(dateString);
				return [
					date.getFullYear(),
					(date.getMonth() + 1).toString().padStart(2, '0'),
				].join('');
			} catch (e) {
				return '格式错误';
			}
		},
		generateAndDownloadReport() {
			try {
				const targetElement = document.getElementById('combatContent');

				html2canvas(targetElement, {
					scale: 1, // 提高分辨率
					backgroundColor: '#ffffff', // 背景色(可自定义)
					useCORS: true, // 如果有加载图片或图标
				}).then((canvas) => {
					// 生成图片链接
					const link = document.createElement('a');
					link.download = 'combat-report.png';
					link.href = canvas.toDataURL('image/png', 0.0);

					// 下载图片
					document.body.appendChild(link);
					link.click();
					document.body.removeChild(link);
				});
			} catch (error) {
				console.error('生成报告失败:', error);
				alert('下载失败:' + error.message);
			}
		},
	},
};
</script>
<style lang="" scoped></style>

index.vue-入口页面

<template>
	<div style="padding: 10px">
		<paramEcharts v-if="functionFlag"></paramEcharts>
		<echartsGet id="combatContent" :echartsJson="echartsJson"></echartsGet>
	</div>
</template>
<script>
import echartsTempJson from './temp.json';
import provinceSetting from './provinceSetting.json';
import paramEcharts from './functionEcharts.vue';
import echartsGet from './echartsGet.vue';
import { handleSlpmle } from './dataHandle.js';

export default {
	name: '',
	components: { echartsGet, paramEcharts },
	mixins: [],
	props: {},
	data() {
		return {
			functionFlag: true,
			echartsJson: null,
			// echartsTempJson: null,
			time: '202406',
			province: '100000',
			echartsListData: {},
		};
	},
	computed: {},
	watch: {},
	mounted() {
		if (this.$route.query.province) {
			//根据省份信息去查provinceSetting配置信息,然后根据里面的模板id去查temp公共模板配置
			this.functionFlag = false;
			//如果传了模板id就不显示参数按钮
			//在后台调用echarts模板时 需要时间,省份之类的参数,前端参数按钮也需要
			//应该从省份区模板才对

			// this.echartsTempJson = echartsTempJson[graphId];
			this.getEchartsData('202406', this.$route.query.province);
		} else {
			this.functionFlag = true;
			// this.time = '2024';
			// this.province = '100000';
			this.getEchartsData('202406', '100000'); //默认
		}
		let _this = this;
		this.$eventBus.$on('realDatas', function (res) {
			// console.log(res);
			// _this.areaEchartsJson = res.alldata;
			_this.getEchartsData(res.time, res.province);
		});
	},
	methods: {
		//根据传来的tiem和province去查询接口
		getEchartsData(time, province) {
				let tempC = {};

			// 查找对应的省份模板配置
			for (const elementProvince of provinceSetting) {
				if (province !== elementProvince.value) continue;

				// 遍历模板配置(公共模板)
				for (const key in echartsTempJson) {
					if (elementProvince.tempId !== key) continue;

					tempC = { ...echartsTempJson[key] }; // 深拷贝公共模板
					tempC.value = elementProvince.value;
					tempC.useFlag = !!elementProvince.useFlag; // 是否使用地区特殊样式

					// 遍历地区模板中的 echartsList 并覆盖公共模板对应项
					for (const elementEchartsList of elementProvince.echartsList) {
						for (let j = 0; j < tempC.echartsList.length; j++) {
							if (
								elementEchartsList.tempName ===
								tempC.echartsList[j].tempName
							) {
								if (elementEchartsList.useFlag) {
									tempC.echartsList[j] = {
										...elementEchartsList,
									};
								}
								if (elementEchartsList.useFunction) {
									tempC.echartsList[j].useFunction =
										elementEchartsList.useFunction;
								}
							}
						}
					}
				}
			}

			// 构造接口参数
			let params = {
				dateStr: time,
				regionCode: province,
				topicId: tempC.tempId,
			};
			//调用接口传数据
			let echartsListData = {
				G0001_G000001: [
					{ name: 1111, value: 2222 },
					{ name: 1111, value: 2222 },
					{ name: 1111, value: 2222 },
					{ name: 2222, value: 2222 },
					{ name: 2222, value: 2222 },
				],
				G0001_G000002: [
					{ name: 4444, value: 3333 },
					{ name: 4444, value: 3333 },
					{ name: 4444, value: 3333 },
					{ name: 3333, value: 3333 },
					{ name: 3333, value: 3333 },
					{ name: 3333, value: 3333 },
				],
			};
			for (const key in echartsListData) {
				for (let index = 0; index < tempC.echartsList.length; index++) {
					const elements = tempC.echartsList[index];

					// 通过模板名匹配数据键名(去掉前缀)
					if (elements.tempName === key.split('_')[1]) {
						let funcName = elements.useFunction;
						console.log(funcName);

						// 动态调用指定的数据处理函数
						let thisData = funcName
							? handleFns[funcName](echartsListData[key])
							: handleFns.handleSlpmle(echartsListData[key]);

						// 设置处理后的数据
						tempC.echartsList[index].optionData = thisData;
					}
				}
			}
			this.echartsJson = tempC;
		},
	},
};
</script>
<style lang="less">
@import './echartsTempCss.less';
</style>

provinceSetting.json-根据省份分别显示模板(强化公共模板)

[
	{
		"value": "100000",
		"label": "全国",
		"realValue": ["100000"],
		"tempId": "G000",
		"useFlag": true,
		"echartsList": [
			{
				"useFlag": false,
				"tempName": "G00001",
				"type": "pie",
				"width": "300px",
				"height": "400px"
			},
			{ "tempName": "G00002", "type": "pie" },
			{ "tempName": "G00003", "type": "pie" },
			{ "tempName": "G00004", "type": "pie" },
			{ "tempName": "G00005", "type": "pie" },
			{ "tempName": "G00006", "type": "pie" },
			{ "tempName": "G00007", "type": "pie" },
			{ "tempName": "G00008", "type": "pie" },
			{ "tempName": "G00009", "type": "pie" }
		]
	},
	{
		"value": "100001",
		"label": "中部地区",
		"tempId": "G0003",
		"realValue": [
			"140000",
			"340000",
			"360000",
			"410000",
			"420000",
			"430000"
		],
		"echartsList": [
			{
				"useFlag": false,
				"tempName": "G000301"
			}
		]
	}
]

temp.json-公共模板

{
	"G000": {
		"name": "全国指标速览",
		"show": true,
		"tempId": "G000",
		"width": "1030px",
		"height": "575px",
		"echartsList": [
			{
				"tempName": "G00001",
				"type": "pie",
				"width": "237.5px",
				"height": "171px",
				"useFunction": "handleSlpmle"
			},
			{
				"tempName": "G00002",
				"type": "radar",
				"width": "237.5px",
				"height": "171px"
			},
			{
				"tempName": "G00003",
				"type": "map",
				"width": "515px",
				"height": "382px"
			},
			{
				"tempName": "G00004",
				"type": "area",
				"width": "237.5px",
				"height": "171px"
			},
			{
				"tempName": "G00005",
				"type": "lineEcharts",
				"width": "237.5px",
				"height": "171px"
			},
			{
				"tempName": "G00006",
				"type": "bar",
				"width": "237.5px",
				"height": "171px"
			},
			{
				"tempName": "G00007",
				"type": "bar",
				"width": "237.5px",
				"height": "171px"
			},
			{
				"tempName": "G00008",
				"type": "trabecula",
				"width": "237.5px",
				"height": "171px"
			},
			{
				"tempName": "G00009",
				"type": "lineEcharts",
				"width": "237.5px",
				"height": "171px"
			}
		]
	},
	"G0003": {
		"name": "省份",
		"show": true,
		"tempId": "G0003",
		"width": "1030px",
		"height": "575px",
		"echartsList": [
			{
				"tempName": "G000301",
				"type": "map",
				"width": "237.5px",
				"height": "171px"
			}
		]
	}
}

项目中用到的ecentbus–https://blog.csdn.net/Shi_haoliu/article/details/146340424?spm=1001.2014.3001.5501

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2340885.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Qt项目——Tcp网络调试助手服务端与客户端

目录 前言结果预览工程文件源代码一、开发流程二、Tcp协议三、Socket四、Tcp服务器的关键流程五、Tcp客户端的关键流程六、Tcp服务端核心代码七、客户端核心代码总结 前言 这期要运用到计算机网络的知识&#xff0c;要搞清楚Tcp协议&#xff0c;学习QTcpServer &#xff0c;学…

4.21 从0开始配置spark-local模式

首先准备好安装包 然后使用命令解压 使用source /etc/profile命令让环境变量生效 输入命令 spark-submit --class org.apache.spark.examples.SparkPi --master local[2] /opt/module/spark-local/examples/jars/spark-examples_2.12-3.1.1.jar 10 即在spark运行了第一个程序…

chili3d调试笔记3 加入c++ 大模型对话方法 cmakelists精读

加入 #include <emscripten/bind.h> #include <emscripten/val.h> #include <nlohmann/json.hpp> 怎么加包 函数直接用emscripten::function&#xff0c;如&#xff1a; emscripten::function("send_to_llm", &send_to_llm); set (CMAKE_C…

go语言八股文

1.go语言的接口是怎么实现 接口&#xff08;interface&#xff09;是一种类型&#xff0c;它定义了一组方法的集合。任何类型只要实现了接口中定义的所有方法&#xff0c;就被认为实现了该接口。 代码的实现 package mainimport "fmt"// 定义接口 type Shape inte…

基于 DeepSeek大模型 开发AI应用的理论和实战书籍推荐,涵盖基础理论、模型架构、实战技巧及对比分析,并附表格总结

以下是基于 DeepSeek大模型 开发AI应用的理论和实战书籍推荐&#xff0c;涵盖基础理论、模型架构、实战技巧及对比分析&#xff0c;并附表格总结&#xff1a; 1. 推荐书籍及内容说明 (1) 《深度学习》&#xff08;Deep Learning&#xff09; 作者&#xff1a;Ian Goodfellow…

从数字化到智能化,百度 SRE 数智免疫系统的演进和实践

1. 为什么 SRE 需要数智免疫系统&#xff1f; 2022 年 10 月&#xff0c;在 Gartner 公布的 2023 年十大战略技术趋势中提到了「数字免疫系统」的概念&#xff0c;旨在通过结合数据驱动的一系列手段来提高系统的弹性和稳定性。 在过去 2 年的时间里&#xff0c;百度基于该…

ArcGIS及其组件抛出 -- “Sorry, this application cannot run under a Virtual Machine.“

产生背景&#xff1a; 使用的是“破解版本”或“被套壳过”的非官方 ArcGIS 版本 破解版本作者为了防止&#xff1a; 被研究破解方式 被自动化抓包/提权/逆向 被企业环境中部署多机使用 通常会加入**“虚拟化环境检测阻断运行”机制** 原因解释&#xff1a; 说明你当前运…

进阶篇 第 5 篇:现代预测方法 - Prophet 与机器学习特征工程

进阶篇 第 5 篇&#xff1a;现代预测方法 - Prophet 与机器学习特征工程 (图片来源: ThisIsEngineering RAEng on Pexels) 在前几篇中&#xff0c;我们深入研究了经典的时间序列统计模型&#xff0c;如 ETS 和强大的 SARIMA 家族。它们在理论上成熟且应用广泛&#xff0c;但有…

影刀填写输入框(web) 时出错: Can not convert Array to String

环境&#xff1a; 影刀5.26.24 Win10专业版 问题描述&#xff1a; [错误来源]行12: 填写输入框(web) 执行 填写输入框(web) 时出错: Can not convert Array to String. 解决方案&#xff1a; 1. 检查变量内容 在填写输入框之前&#xff0c;打印BT和NR变量的值&#xff…

词语关系图谱模型

参数配置说明 sentences, # 分词后的语料&#xff08;列表嵌套列表&#xff09; vector_size100, # 每个词的向量维度 window5, # 词与上下文之间的最大距离&#xff08;滑动窗口大小&#xff09; min_count5, # 忽略出现次数小于5的…

HTTP的请求消息Request和响应消息Response

一&#xff1a;介绍 &#xff08;1&#xff09;定义 service方法里的两个参数 &#xff08;2)过程 Request:获取请求数据 浏览器发送http请求数据&#xff08;字符串&#xff09;&#xff0c;字符串被tomcat解析&#xff0c;解析后tomcat会将请求数据放入request对象 Response:…

C++异步操作 - future async package_task promise

异步 异步编程是一种程序设计范式&#xff0c;​​允许任务在等待耗时操作&#xff08;如I/O、网络请求&#xff09;时暂停执行&#xff0c;转而处理其他任务&#xff0c;待操作完成后自动恢复​​。其核心目标是​​避免阻塞主线程​​&#xff0c;提升程序的并发性和响应速度…

数据结构——栈以及相应的操作

栈(Stack) 在维基百科中是这样定义的&#xff1a; 堆栈(stack) 又称为栈或堆叠&#xff0c;是计算机科学中的一种抽象资料类型&#xff0c;只允许在有序的线性资料集合中的一端&#xff08;称为堆栈顶端&#xff0c;top&#xff09;进行加入数据&#xff08;push&#xff09;和…

如何应对政策变化导致的项目风险

应对政策变化导致的项目风险&#xff0c;核心在于&#xff1a;加强政策研判机制、建立动态应对流程、构建合规应急预案、强化跨部门联动、提升项目柔性与调整能力。其中&#xff0c;加强政策研判机制 是所有防范工作中的“前哨哨兵”&#xff0c;可以让项目团队在政策风向转变之…

ASP.Net Web Api如何更改URL

1.找到appsettings.json 修改如下&#xff1a; 主要为urls的修改填本机私有地址即可 {"Logging": {"LogLevel": {"Default": "Information","Microsoft.AspNetCore": "Warning"}},"AllowedHosts": &q…

【HTTPS协议原理】数据加密、如何防止中间人攻击、证书和签名、HTTPS完整工作流程

⭐️个人主页&#xff1a;小羊 ⭐️所属专栏&#xff1a;Linux网络 很荣幸您能阅读我的文章&#xff0c;诚请评论指点&#xff0c;欢迎欢迎 ~ 目录 数据加密常见的加密方式数据摘要方案一&#xff1a;仅使用对称加密方案二&#xff1a;仅使用非对称加密方案三&#xff1a;双…

Java中链表的深入了解及实现

一、链表 1.链表的概念 1.1链表是⼀种物理存储结构上⾮连续存储结构&#xff0c;数据元素的逻辑顺序是通过链表中的引⽤链接次序实现的 实际中链表的结构⾮常多样&#xff0c;以下情况组合起来就有8种链表结构&#xff1a; 2.链表的实现 1.⽆头单向⾮循环链表实现 链表中的…

植物大战僵尸杂交版v3.6最新版本(附下载链接)

B站游戏作者潜艇伟伟迷于4月19日更新了植物大战僵尸杂交版3.6版本&#xff01;&#xff01;&#xff01;&#xff0c;有b站账户的记得要给作者三连关注一下呀&#xff01; 不多废话下载链接放上&#xff1a; 夸克网盘链接&#xff1a;&#xff1a;https://pan.quark.cn/s/1af9b…

【源码】【Java并发】【ThreadLocal】适合中学者体质的ThreadLocal源码阅读

&#x1f44b;hi&#xff0c;我不是一名外包公司的员工&#xff0c;也不会偷吃茶水间的零食&#xff0c;我的梦想是能写高端CRUD &#x1f525; 2025本人正在沉淀中… 博客更新速度 &#x1f44d; 欢迎点赞、收藏、关注&#xff0c;跟上我的更新节奏 &#x1f4da;欢迎订阅专栏…

背包问题模板

文章目录 01背包题意思路代码优化 完全背包题意思路代码优化 多重背包题意思路代码优化 分组背包题意思路代码 01背包 特点&#xff1a;每件物品最多只能用一次 01背包问题 题意 给出每件物品的体积v,价值w,求解能装入背包的的物品的最大价值&#xff0c;并且每件物品只能选一…