动态创建多个echarts图表

article/2025/9/8 5:56:17

效果:

 

<template>
<div class="wrapper"><Row v-for="(items, index) in secondeData" :key="index"><Col  span="12" v-for="m in items" :key="m"><div class="chart" :ref="m.targetName" style="height: 400px;margin: 10px;box-shadow: 0 0 8px"></div></Col></Row>               
</div>
</template>
<script>
var appName='';
//引入Echarts主模块let echarts=require('echarts/lib/echarts');//引入柱状图require('echarts/lib/chart/bar');//引入圆饼图require('echarts/lib/chart/pie');//引入所需组件require('echarts/lib/component/tooltip');require('echarts/lib/component/legend');import  'static/js/macarons.js'
export default {data(){return {charts:{},echartsData:[],appCode:'',appName:'',monitorAlarmMessage:{startAlarmTime:new Date(new Date().getTime()-600000),endAlarmTime:new Date()}}},computed: {// 计算属性的 gettersecondeData: function () {let newData = [], c = this.echartsData.length % 2 == 0, l = c ? this.echartsData.length : this.echartsData.length - 1 ;for (let i = 0; i < l; i = i+2) {newData.push([this.echartsData[i], this.echartsData[i+1]]);}if(!c){newData.push([this.echartsData[this.echartsData.length - 1]]);}return newData;}},methods:{setData(arrays){let _this = this;_this.echartsData = arrays;_this.$nextTick(function () {let newTargets = [];for (let index = 0; index < arrays.length; index++) {let item = arrays[index], doms = _this.$refs[item.targetName];newTargets[index] = item.targetName;if(!_this.charts[item.targetName]){console.log("不存在,开始重新绘制div ->" + item.targetName);_this.createChartOne(item.targetName, doms[0]);}_this.chartMonitorTargetUpdate(item.targetName, item); }for (const key in _this.charts) {if (newTargets.indexOf(key) < 0) {console.log("删除 ->" + key);delete _this.charts[key];}}})},showInfluxDBMonitorAlarmByAppCode(){this.$http.post('/influxDBMonitorAnalyze/showInfluxDBMonitorAlarmByAppCode',{//查询请求接口appName:this.appName,appCode:this.appCode}).then((res)=>{//alert(JSON.stringify(res.data.data));this.setData(res.data.data.filter(function(item){return item != null;}));//this.echartsDataSize= this.echartsData.length;console.log(this.echartsData);}).catch((err)=>{this.$Modal.error({ title: "请求失败", content: err });})},createChartOne(targetName, ref){//this.chartMonitorTarget=echarts.init(this.$refs.chartOne);this.charts[targetName] = echarts.init(ref, 'macarons');this.charts[targetName].setOption({tooltip: {trigger: 'axis'},grid:{top:100,bottom:40,left:40,right:45},xAxis:  {name:'时间',               //坐标轴名称type: 'category',nameGap:15,                  //坐标轴名称与轴线之间的距离nameRotate:0,                //坐标轴名字旋转,角度值boundaryGap: true,data: [],axisTick:{          //坐标轴刻度是否朝内,默认朝外。length:5                //坐标轴刻度的长度。},axisLabel: {interval:"auto",rotate:30  } },yAxis: {name:'',               //坐标轴名称type: 'value',axisLabel: {formatter: '{value}'}},series: []})},chartMonitorTargetUpdate(targetName, data){this.charts[targetName].setOption({title: {text: targetName,textStyle: {fontWeight: 'bolder',              //标题颜色color: '#408829'},subtext: ''},legend: {left:'center',top:'30',bottom:'auto',orient:'horizontal',data:data.legend.data},xAxis: {data: data.xaxisData},series: data.influxDBMonitorAlarm.seriesList});},test(){var arrays = [], count = Math.round(Math.random()*10);console.log("随机生成个数:" + count);for (let index = 0; index < count; index++) {//let count = Math.round(Math.random()*10);let xaxis = [10];for (let k = 0; k < 10; k++) {xaxis[k] = "8:" + Math.round(Math.random()*60);}let datas = [4];for (let j = 0; j < 4; j++) {let is = [10];for (let i = 0; i < 10; i++) {is[i] = Math.round(Math.random()*100);}datas[j] = is;  }arrays.push({"legend": {"data": ["基线值", "当前值", "基线上浮值", "基线下浮值"]},"targetName": "targetName" + index,"appName": null,"xaxisData": xaxis,"influxDBMonitorAlarm": {"seriesList": [{"name": "基线值","type": "line","data": datas[3],"markPoint": {"data": [{"type": "max","name": "最大值"}, {"type": "min","name": "最小值"}]}}, {"name": "当前值","type": "line","data": datas[0],"markPoint": {"data": [{"type": "max","name": "最大值"}, {"type": "min","name": "最小值"}]}}, {"name": "基线上浮值","type": "line","data": datas[1],"markPoint": {"data": [{"type": "max","name": "最大值"}, {"type": "min","name": "最小值"}]}}, {"name": "基线下浮值","type": "line","data": datas[2],"markPoint": {"data": [{"type": "max","name": "最大值"}, {"type": "min","name": "最小值"}]}}]}});}  this.setData(arrays);}},created(){},activated(){let _this = this;appName=_this.$route.query.appName_this.appCode=_this.$route.query.appCode_this.appName=_this.$route.query.appName_this.showInfluxDBMonitorAlarmByAppCode();// _this.test();window.setInterval(function(){_this.showInfluxDBMonitorAlarmByAppCode();}, 5000);},destroyed(){}}
</script>
<style lang="scss" scoped>//-----------------------------------------------------------css样式代码start
//编写样式
.ivu-form-item {margin-bottom: 24px;
}
</style>

js:macarons.js

(function (root, factory) {if (typeof define === 'function' && define.amd) {// AMD. Register as an anonymous module.define(['exports', 'echarts'], factory);} else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {// CommonJSfactory(exports, require('echarts'));} else {// Browser globalsfactory({}, root.echarts);}
}(this, function (exports, echarts) {var log = function (msg) {if (typeof console !== 'undefined') {console && console.error && console.error(msg);}};if (!echarts) {log('ECharts is not Loaded');return;}var colorPalette = ['#2ec7c9','#b6a2de','#5ab1ef','#ffb980','#d87a80','#8d98b3','#e5cf0d','#97b552','#95706d','#dc69aa','#07a2a4','#9a7fd1','#588dd5','#f5994e','#c05050','#59678c','#c9ab00','#7eb00a','#6f5553','#c14089'];var theme = {color: colorPalette,title: {textStyle: {fontWeight: 'normal',color: '#008acd'}},visualMap: {itemWidth: 15,color: ['#5ab1ef','#e0ffff']},toolbox: {iconStyle: {normal: {borderColor: colorPalette[0]}}},tooltip: {backgroundColor: 'rgba(50,50,50,0.5)',axisPointer : {type : 'line',lineStyle : {color: '#008acd'},crossStyle: {color: '#008acd'},shadowStyle : {color: 'rgba(200,200,200,0.2)'}}},dataZoom: {dataBackgroundColor: '#efefff',fillerColor: 'rgba(182,162,222,0.2)',handleColor: '#008acd'},grid: {borderColor: '#eee'},categoryAxis: {axisLine: {lineStyle: {color: '#008acd'}},splitLine: {lineStyle: {color: ['#eee']}}},valueAxis: {axisLine: {lineStyle: {color: '#008acd'}},splitArea : {show : true,areaStyle : {color: ['rgba(250,250,250,0.1)','rgba(200,200,200,0.1)']}},splitLine: {lineStyle: {color: ['#eee']}}},timeline : {lineStyle : {color : '#008acd'},controlStyle : {normal : { color : '#008acd'},emphasis : { color : '#008acd'}},symbol : 'emptyCircle',symbolSize : 3},line: {smooth : true,symbol: 'emptyCircle',symbolSize: 3},candlestick: {itemStyle: {normal: {color: '#d87a80',color0: '#2ec7c9',lineStyle: {color: '#d87a80',color0: '#2ec7c9'}}}},scatter: {symbol: 'circle',symbolSize: 4},map: {label: {normal: {textStyle: {color: '#d87a80'}}},itemStyle: {normal: {borderColor: '#eee',areaColor: '#ddd'},emphasis: {areaColor: '#fe994e'}}},graph: {color: colorPalette},gauge : {axisLine: {lineStyle: {color: [[0.2, '#2ec7c9'],[0.8, '#5ab1ef'],[1, '#d87a80']],width: 10}},axisTick: {splitNumber: 10,length :15,lineStyle: {color: 'auto'}},splitLine: {length :22,lineStyle: {color: 'auto'}},pointer : {width : 5}}};echarts.registerTheme('macarons', theme);
}));

 


http://chatgpt.dhexx.cn/article/TS1wJov9.shtml

相关文章

echarts基础应用

第一步&#xff1a;下载相应的js文件&#xff1a;echarts.min.js和macarons.js&#xff0c;其中macarons.js文件时主题文件。 第二步&#xff1a;编写index.html <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8">&…

关于在vue中动态渲染echart组件,渲染失败的问题

以下chart是在正常页面中&#xff08;不在蒙层中&#xff09;&#xff1a; 用echart组件时&#xff0c;渲染数据多数情况下会是动态添加&#xff0c;也就是前台获取数据&#xff0c;通过props传递给echart组件。对于vue生命周期函数执行顺序不熟悉的小伙伴儿&#xff0c;会碰…

echart vue

文档&#xff1a; https://echarts.apache.org/zh/tutorial.html#5%20%E5%88%86%E9%92%9F%E4%B8%8A%E6%89%8B%20ECharts 1、条形 <template><div> <!-- 包其他内容需要有个外div --><div :class"className" :style"{height:height,width:…

Vue + Echarts自定义图标颜色及柱状图动态伸缩(定时)

1、效果图&#xff1a; 2、Vue代码 <template><!--区域分布数量柱状图--><div class"emp-area"></div> </template><script>require(/assets/theme/chalk)require(echarts/theme/macarons)export default {name: "StaffA…

如何使用pyecharts中的主题样式?

如何使用pyecharts中的主题样式&#xff1f; pyechart为用户提供了一套使用方便的主题风格。 本篇图文将总结pyecharts.globals中ThemeType所有主题风格并进行详细的解释。 class _ThemeType:BUILTIN_THEMES ["light", "dark", "white"]LIGH…

python亲和数_用 Python 分析了 5 万条相亲数据,告诉你男女相亲背后的秘密

作者| 叶庭云 责编 | 张文 来源 | 转载自修炼 Python(ID&#xff1a;yetingyun_python) 前言 数据来源&#xff1a;https://www.zhenai.com/zhenghun/ 本文利用 Python 分析了按城市寻找所有地区的征婚信息&#xff0c;看看相亲男女的画像。 数据查看和预处理 导入用到的库 …

vue中echarts使用案例:饼图(可直接使用)

目录 功能、效果图&#xff1a; 代码实现 1、下载echarts包&#xff1a;终端运行 2、代码 3.运行项目&#xff1a;终端运行 4、可能遇到的错误 具体代码详解&#xff1a; 功能、效果图&#xff1a; 1.点击对应模块&#xff0c;放大并显示数据 2、点击下方图标&#xff0c…

Python绘制柱状图之可视化神器pyecharts(一)

目录 pyecharts介绍 特性 优势 展示 柱状图模板系列 水晶柱状图 解决X轴标签过长的柱状图 自定义平均刻度标签&#xff08;方便查看超出范围&#xff09; 翻转X Y轴柱状图 可以移动的X轴柱状图&#xff08;适合数据类别过多&#xff09; 可以移动的Y轴柱状图&#x…

已解决:vue echart 动态获取数据且定时刷新

文章目录 写在前面需求①需求②需求③无效的解决办法1&#xff1a;无效的解决办法2有效的解决办法 写在前面 我太难了&#xff0c;搞一个echart仪表盘&#xff0c;需求一步步分解&#xff1a; ①一开始是写死的假数据&#xff0c;图表成功显示在页面上; ②和后台约定数据类型格…

Python绘制柱状图之可视化神器pyecharts(二)

目录 主题介绍 图表参数 主题详解 柱状图模板系列 海量数据柱状图动画展示 收入支出柱状图&#xff08;适用于记账&#xff09; 我们在日常的生活中&#xff0c;有收入也有支出&#xff0c;这个模板就是把收入和支出直观的展示在读者面前。 三维数据叠加 柱状图与折线…

day07 pyecharts制图

目录 Python配置安装pyecharts 一、柱状图的使用二、折线图三、饼图四、地图五、词云六、组合图 Python配置安装pyecharts Python配置国内镜像源 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple pycharm配置pyecharts 一、柱状图的使用 # 导入…

vue-echarts主题切换+选择面板实现

通过实现主题选择面板&#xff0c;更方便的对echarts图表进行主题的切换&#xff0c;并且支持饼状图、折线图、柱状图等多类型echarts图表。 目录 1、实现过程 1.1、导入echarts主题JS文件 1.2、添加选择按钮图片 1.3、添加按钮 1.4、主题选择面板实现 1.5、初始化echart…

echarts 2.0 macarons主题安装

2019独角兽企业重金招聘Python工程师标准>>> 主题下载: http://echarts.baidu.com/doc/example/theme/macarons.js 其他主题改名字就好了.[无责任猜测] 这里采用AMD加载方式 [博主用的是requirejs] 在main.js中 require.config({paths:{echarts:js/echarts/echarts…

魔法值及魔法值处理方法

魔法值 什么是魔法值 所谓的魔法值就是在代码中直接出现的数字。   PS&#xff1a;代码中有魔法值会造成代码可读性低&#xff08;与代码量成正比&#xff09;。还会造成维护困难&#xff0c;改动一个数值便要大动干戈&#xff0c;牵一发而动全身。应当尽力消灭或减少魔法值…

别让【魔法值】毁了你的代码!

二分钟&#xff0c;教你一个写代码的好习惯。 大家好&#xff0c;我是鱼皮&#xff0c;今天通过一件事情&#xff0c;分享写代码时的一个重要小技巧。 前段时间&#xff0c;我在编程导航项目中开源了一段代码&#xff0c;实现的功能是&#xff0c;当用户的操作失败时&#xff…

【java】万恶的魔法值

在最近的小云通知项目中&#xff0c;我负责维护后端的开发&#xff0c;也是深刻体会到用魔法值会给维护人员带来多大的痛苦&#xff01;可谓一个bug改一天…… 文章目录 什么是魔法值怎么处理魔法值 什么是魔法值 首先我们说一下什么是魔法值&#xff0c;所谓魔法值&#xff0…

什么是java中的魔法值

自从在idea上用了 《阿里巴巴Java开发手册》的插件之后 这里附上对应的插件安装方法&#xff1a;https://developer.aliyun.com/article/224817 偶然发现自己的代码中出现了所谓的 “魔法值” 什么是魔法值呢 所谓魔法值&#xff0c;就是在代码中直接出现的数值&#xff0c;只…

Java中的魔法值和解决方法

目录 一、什么是魔法值 二、解决方法 一、什么是魔法值 魔法数值、魔法数字、魔法值&#xff0c;这是一个东西&#xff0c;不同的叫法。 所谓魔法值&#xff0c;是指在代码中直接出现的数值&#xff0c;只有在这个数值记述的那部分代码中才能明确了解其含义&#xff0c;数字…

java 不要使用魔法值_为什么阿里巴巴Java开发手册中不允许魔法值出现在代码中?...

武培轩 推荐搜索 JavaSpring Boot数据结构MySQLElasticsearchNginx 在阅读《阿里巴巴Java开发手册》时&#xff0c;发现有一条关于关于常量定义的规约&#xff0c;具体内容如下&#xff1a; 图中的反例是将数据缓存起来&#xff0c;并使用魔法值加链路 id 组成 key&#xff0c;…

java 项目减少魔法值_项目代码中魔法值的优雅处理

经常在代码里面会用一些表示有意义的数字&#xff0c;但是如果不写注释的话&#xff0c;过了一段时间我们自己再回去优化代码的时候&#xff0c;可能自己也不清楚这些数字表示的是什么意思了。 如果写注释的话&#xff0c;就要每次使用数字的时候都要写注释了&#xff0c;这样的…