• 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. yAxis: {
    114. name:'', //坐标轴名称
    115. type: 'value',
    116. axisLabel: {
    117. formatter: '{value}'
    118. }
    119. },
    120. series: []
    121. })
    122. },
    123. chartMonitorTargetUpdate(targetName, data){
    124. this.charts[targetName].setOption({
    125. title: {
    126. text: targetName,
    127. textStyle: {
    128. fontWeight: 'bolder', //标题颜色
    129. color: '#408829'
    130. },
    131. subtext: ''
    132. },
    133. legend: {
    134. left:'center',
    135. top:'30',
    136. bottom:'auto',
    137. orient:'horizontal',
    138. data:data.legend.data
    139. },
    140. xAxis: {
    141. data: data.xaxisData
    142. },
    143. series: data.influxDBMonitorAlarm.seriesList
    144. });
    145. },
    146. test(){
    147. var arrays = [], count = Math.round(Math.random()*10);
    148. console.log("随机生成个数:" + count);
    149. for (let index = 0; index < count; index++) {
    150. //let count = Math.round(Math.random()*10);
    151. let xaxis = [10];
    152. for (let k = 0; k < 10; k++) {
    153. xaxis[k] = "8:" + Math.round(Math.random()*60);
    154. }
    155. let datas = [4];
    156. for (let j = 0; j < 4; j++) {
    157. let is = [10];
    158. for (let i = 0; i < 10; i++) {
    159. is[i] = Math.round(Math.random()*100);
    160. }
    161. datas[j] = is;
    162. }
    163. arrays.push({
    164. "legend": {
    165. "data": ["基线值", "当前值", "基线上浮值", "基线下浮值"]
    166. },
    167. "targetName": "targetName" + index,
    168. "appName": null,
    169. "xaxisData": xaxis,
    170. "influxDBMonitorAlarm": {
    171. "seriesList": [{
    172. "name": "基线值",
    173. "type": "line",
    174. "data": datas[3],
    175. "markPoint": {
    176. "data": [{
    177. "type": "max",
    178. "name": "最大值"
    179. }, {
    180. "type": "min",
    181. "name": "最小值"
    182. }]
    183. }
    184. }, {
    185. "name": "当前值",
    186. "type": "line",
    187. "data": datas[0],
    188. "markPoint": {
    189. "data": [{
    190. "type": "max",
    191. "name": "最大值"
    192. }, {
    193. "type": "min",
    194. "name": "最小值"
    195. }]
    196. }
    197. }, {
    198. "name": "基线上浮值",
    199. "type": "line",
    200. "data": datas[1],
    201. "markPoint": {
    202. "data": [{
    203. "type": "max",
    204. "name": "最大值"
    205. }, {
    206. "type": "min",
    207. "name": "最小值"
    208. }]
    209. }
    210. }, {
    211. "name": "基线下浮值",
    212. "type": "line",
    213. "data": datas[2],
    214. "markPoint": {
    215. "data": [{
    216. "type": "max",
    217. "name": "最大值"
    218. }, {
    219. "type": "min",
    220. "name": "最小值"
    221. }]
    222. }
    223. }]
    224. }
    225. });
    226. }
    227. this.setData(arrays);
    228. }
    229. },
    230. created(){
    231. },
    232. activated(){
    233. let _this = this;
    234. appName=_this.$route.query.appName
    235. _this.appCode=_this.$route.query.appCode
    236. _this.appName=_this.$route.query.appName
    237. _this.showInfluxDBMonitorAlarmByAppCode();
    238. // _this.test();
    239. window.setInterval(function(){
    240. _this.showInfluxDBMonitorAlarmByAppCode();
    241. }, 5000);
    242. },destroyed(){
    243. }
    244. }
    245. </script>
    246. <style lang="scss" scoped>//-----------------------------------------------------------css样式代码start
    247. //编写样式
    248. .ivu-form-item {
    249. margin-bottom: 24px;
    250. }
    251. </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. }));
  • 相关阅读:
    Myeclipse运行报错:an out of memory error has occurred的解决方法
    关于UnsupportedClassVersionError的错误处理
    Java设置session超时(失效)的三种方式
    mybatis自增长插入id
    揭开Java IO流中的flush()的神秘面纱
    mybatis多表查询
    在Myeclipse中提交代码到GitHub中
    一台电脑安装多个版本的jdk
    使用Git的Push出现rejected
    暴力解决HTMLTestRunner出现dict() ->new empty dictionary的问题
  • 原文地址:https://www.cnblogs.com/owenzh/p/13213248.html
Copyright © 2020-2023  润新知