VUE动态创建多个echarts图表

效果:

 

  1. <template>
  2. <div class="wrapper">
  3. <Row v-for="(items, index) in secondeData" :key="index">
  4. <Col span="12" v-for="m in items" :key="m">
  5. <div class="chart" :ref="m.targetName" style="height: 400px;margin: 10px;box-shadow: 0 0 8px"></div>
  6. </Col>
  7. </Row>
  8. </div>
  9. </template>
  10. <script>
  11. var appName='';
  12. //引入Echarts主模块
  13. let echarts=require('echarts/lib/echarts');
  14. //引入柱状图
  15. require('echarts/lib/chart/bar');
  16. //引入圆饼图
  17. require('echarts/lib/chart/pie');
  18. //引入所需组件
  19. require('echarts/lib/component/tooltip');
  20. require('echarts/lib/component/legend');
  21. import 'static/js/macarons.js'
  22. export default {
  23. data(){
  24. return {
  25. charts:{},
  26. echartsData:[],
  27. appCode:'',
  28. appName:'',
  29. monitorAlarmMessage:{
  30. startAlarmTime:new Date(new Date().getTime()-600000),
  31. endAlarmTime:new Date()
  32. }
  33. }
  34. },
  35. computed: {
  36. // 计算属性的 getter
  37. secondeData: function () {
  38. let newData = [], c = this.echartsData.length % 2 == 0, l = c ? this.echartsData.length : this.echartsData.length - 1 ;
  39. for (let i = 0; i < l; i = i+2) {
  40. newData.push([this.echartsData[i], this.echartsData[i+1]]);
  41. }
  42. if(!c){
  43. newData.push([this.echartsData[this.echartsData.length - 1]]);
  44. }
  45. return newData;
  46. }
  47. },
  48. methods:{
  49. setData(arrays){
  50. let _this = this;
  51. _this.echartsData = arrays;
  52. _this.$nextTick(function () {
  53. let newTargets = [];
  54. for (let index = 0; index < arrays.length; index++) {
  55. let item = arrays[index], doms = _this.$refs[item.targetName];
  56. newTargets[index] = item.targetName;
  57. if(!_this.charts[item.targetName]){
  58. console.log("不存在,开始重新绘制div ->" + item.targetName);
  59. _this.createChartOne(item.targetName, doms[0]);
  60. }
  61. _this.chartMonitorTargetUpdate(item.targetName, item);
  62. }
  63. for (const key in _this.charts) {
  64. if (newTargets.indexOf(key) < 0) {
  65. console.log("删除 ->" + key);
  66. delete _this.charts[key];
  67. }
  68. }
  69. })
  70. },
  71. showInfluxDBMonitorAlarmByAppCode(){
  72. this.$http.post('/influxDBMonitorAnalyze/showInfluxDBMonitorAlarmByAppCode',{//查询请求接口
  73. appName:this.appName,
  74. appCode:this.appCode
  75. }).then((res)=>{
  76. //alert(JSON.stringify(res.data.data));
  77. this.setData(res.data.data.filter(function(item){
  78. return item != null;
  79. }));
  80. //this.echartsDataSize= this.echartsData.length;
  81. console.log(this.echartsData);
  82. }).catch((err)=>{
  83. this.$Modal.error({ title: "请求失败", content: err });
  84. })
  85. },
  86. createChartOne(targetName, ref){
  87. //this.chartMonitorTarget=echarts.init(this.$refs.chartOne);
  88. this.charts[targetName] = echarts.init(ref, 'macarons');
  89. this.charts[targetName].setOption({
  90. tooltip: {
  91. trigger: 'axis'
  92. },
  93. grid:{
  94. top:100,
  95. bottom:40,
  96. left:40,
  97. right:45
  98. },
  99. xAxis: {
  100. name:'时间', //坐标轴名称
  101. type: 'category',
  102. nameGap:15, //坐标轴名称与轴线之间的距离
  103. nameRotate:0, //坐标轴名字旋转,角度值
  104. boundaryGap: true,
  105. data: [],
  106. axisTick:{ //坐标轴刻度是否朝内,默认朝外。
  107. length:5 //坐标轴刻度的长度。
  108. },
  109. axisLabel: {
  110. interval:"auto",
  111.    rotate:30  
  112. } 
  113. },
  114. yAxis: {
  115. name:'', //坐标轴名称
  116. type: 'value',
  117. axisLabel: {
  118. formatter: '{value}'
  119. }
  120. },
  121. series: []
  122. })
  123. },
  124. chartMonitorTargetUpdate(targetName, data){
  125. this.charts[targetName].setOption({
  126. title: {
  127. text: targetName,
  128. textStyle: {
  129. fontWeight: 'bolder', //标题颜色
  130. color: '#408829'
  131. },
  132. subtext: ''
  133. },
  134. legend: {
  135. left:'center',
  136. top:'30',
  137. bottom:'auto',
  138. orient:'horizontal',
  139. data:data.legend.data
  140. },
  141. xAxis: {
  142. data: data.xaxisData
  143. },
  144. series: data.influxDBMonitorAlarm.seriesList
  145. });
  146. },
  147. test(){
  148. var arrays = [], count = Math.round(Math.random()*10);
  149. console.log("随机生成个数:" + count);
  150. for (let index = 0; index < count; index++) {
  151. //let count = Math.round(Math.random()*10);
  152. let xaxis = [10];
  153. for (let k = 0; k < 10; k++) {
  154. xaxis[k] = "8:" + Math.round(Math.random()*60);
  155. }
  156. let datas = [4];
  157. for (let j = 0; j < 4; j++) {
  158. let is = [10];
  159. for (let i = 0; i < 10; i++) {
  160. is[i] = Math.round(Math.random()*100);
  161. }
  162. datas[j] = is;
  163. }
  164. arrays.push({
  165. "legend": {
  166. "data": ["基线值", "当前值", "基线上浮值", "基线下浮值"]
  167. },
  168. "targetName": "targetName" + index,
  169. "appName": null,
  170. "xaxisData": xaxis,
  171. "influxDBMonitorAlarm": {
  172. "seriesList": [{
  173. "name": "基线值",
  174. "type": "line",
  175. "data": datas[3],
  176. "markPoint": {
  177. "data": [{
  178. "type": "max",
  179. "name": "最大值"
  180. }, {
  181. "type": "min",
  182. "name": "最小值"
  183. }]
  184. }
  185. }, {
  186. "name": "当前值",
  187. "type": "line",
  188. "data": datas[0],
  189. "markPoint": {
  190. "data": [{
  191. "type": "max",
  192. "name": "最大值"
  193. }, {
  194. "type": "min",
  195. "name": "最小值"
  196. }]
  197. }
  198. }, {
  199. "name": "基线上浮值",
  200. "type": "line",
  201. "data": datas[1],
  202. "markPoint": {
  203. "data": [{
  204. "type": "max",
  205. "name": "最大值"
  206. }, {
  207. "type": "min",
  208. "name": "最小值"
  209. }]
  210. }
  211. }, {
  212. "name": "基线下浮值",
  213. "type": "line",
  214. "data": datas[2],
  215. "markPoint": {
  216. "data": [{
  217. "type": "max",
  218. "name": "最大值"
  219. }, {
  220. "type": "min",
  221. "name": "最小值"
  222. }]
  223. }
  224. }]
  225. }
  226. });
  227. }
  228. this.setData(arrays);
  229. }
  230. },
  231. created(){
  232. },
  233. activated(){
  234. let _this = this;
  235. appName=_this.$route.query.appName
  236. _this.appCode=_this.$route.query.appCode
  237. _this.appName=_this.$route.query.appName
  238. _this.showInfluxDBMonitorAlarmByAppCode();
  239. // _this.test();
  240. window.setInterval(function(){
  241. _this.showInfluxDBMonitorAlarmByAppCode();
  242. }, 5000);
  243. },destroyed(){
  244. }
  245. }
  246. </script>
  247. <style lang="scss" scoped>//-----------------------------------------------------------css样式代码start
  248. //编写样式
  249. .ivu-form-item {
  250. margin-bottom: 24px;
  251. }
  252. </style>

js:macarons.js

  1. (function (root, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. // AMD. Register as an anonymous module.
  4. define(['exports', 'echarts'], factory);
  5. } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
  6. // CommonJS
  7. factory(exports, require('echarts'));
  8. } else {
  9. // Browser globals
  10. factory({}, root.echarts);
  11. }
  12. }(this, function (exports, echarts) {
  13. var log = function (msg) {
  14. if (typeof console !== 'undefined') {
  15. console && console.error && console.error(msg);
  16. }
  17. };
  18. if (!echarts) {
  19. log('ECharts is not Loaded');
  20. return;
  21. }
  22. var colorPalette = [
  23. '#2ec7c9','#b6a2de','#5ab1ef','#ffb980','#d87a80',
  24. '#8d98b3','#e5cf0d','#97b552','#95706d','#dc69aa',
  25. '#07a2a4','#9a7fd1','#588dd5','#f5994e','#c05050',
  26. '#59678c','#c9ab00','#7eb00a','#6f5553','#c14089'
  27. ];
  28. var theme = {
  29. color: colorPalette,
  30. title: {
  31. textStyle: {
  32. fontWeight: 'normal',
  33. color: '#008acd'
  34. }
  35. },
  36. visualMap: {
  37. itemWidth: 15,
  38. color: ['#5ab1ef','#e0ffff']
  39. },
  40. toolbox: {
  41. iconStyle: {
  42. normal: {
  43. borderColor: colorPalette[0]
  44. }
  45. }
  46. },
  47. tooltip: {
  48. backgroundColor: 'rgba(50,50,50,0.5)',
  49. axisPointer : {
  50. type : 'line',
  51. lineStyle : {
  52. color: '#008acd'
  53. },
  54. crossStyle: {
  55. color: '#008acd'
  56. },
  57. shadowStyle : {
  58. color: 'rgba(200,200,200,0.2)'
  59. }
  60. }
  61. },
  62. dataZoom: {
  63. dataBackgroundColor: '#efefff',
  64. fillerColor: 'rgba(182,162,222,0.2)',
  65. handleColor: '#008acd'
  66. },
  67. grid: {
  68. borderColor: '#eee'
  69. },
  70. categoryAxis: {
  71. axisLine: {
  72. lineStyle: {
  73. color: '#008acd'
  74. }
  75. },
  76. splitLine: {
  77. lineStyle: {
  78. color: ['#eee']
  79. }
  80. }
  81. },
  82. valueAxis: {
  83. axisLine: {
  84. lineStyle: {
  85. color: '#008acd'
  86. }
  87. },
  88. splitArea : {
  89. show : true,
  90. areaStyle : {
  91. color: ['rgba(250,250,250,0.1)','rgba(200,200,200,0.1)']
  92. }
  93. },
  94. splitLine: {
  95. lineStyle: {
  96. color: ['#eee']
  97. }
  98. }
  99. },
  100. timeline : {
  101. lineStyle : {
  102. color : '#008acd'
  103. },
  104. controlStyle : {
  105. normal : { color : '#008acd'},
  106. emphasis : { color : '#008acd'}
  107. },
  108. symbol : 'emptyCircle',
  109. symbolSize : 3
  110. },
  111. line: {
  112. smooth : true,
  113. symbol: 'emptyCircle',
  114. symbolSize: 3
  115. },
  116. candlestick: {
  117. itemStyle: {
  118. normal: {
  119. color: '#d87a80',
  120. color0: '#2ec7c9',
  121. lineStyle: {
  122. color: '#d87a80',
  123. color0: '#2ec7c9'
  124. }
  125. }
  126. }
  127. },
  128. scatter: {
  129. symbol: 'circle',
  130. symbolSize: 4
  131. },
  132. map: {
  133. label: {
  134. normal: {
  135. textStyle: {
  136. color: '#d87a80'
  137. }
  138. }
  139. },
  140. itemStyle: {
  141. normal: {
  142. borderColor: '#eee',
  143. areaColor: '#ddd'
  144. },
  145. emphasis: {
  146. areaColor: '#fe994e'
  147. }
  148. }
  149. },
  150. graph: {
  151. color: colorPalette
  152. },
  153. gauge : {
  154. axisLine: {
  155. lineStyle: {
  156. color: [[0.2, '#2ec7c9'],[0.8, '#5ab1ef'],[1, '#d87a80']],
  157. width: 10
  158. }
  159. },
  160. axisTick: {
  161. splitNumber: 10,
  162. length :15,
  163. lineStyle: {
  164. color: 'auto'
  165. }
  166. },
  167. splitLine: {
  168. length :22,
  169. lineStyle: {
  170. color: 'auto'
  171. }
  172. },
  173. pointer : {
  174. width : 5
  175. }
  176. }
  177. };
  178. echarts.registerTheme('macarons', theme);
  179. }));

 

posted @ 2020-06-30 13:35  奋斗的中年人哈哈哈  阅读(5165)  评论(0)    收藏  举报