• vue大屏项目封装echarts


    在写大屏项目的时候 需要引入特别多的echarts图表
    所以可以把echarts封装一下 方便使用

    • 在main.js 中首先导入echarts(需要安装)
    • 然后把echatrs挂在到vue的原型上 这样就可以直接使用this.$echarts访问到实例了
    //引入echart
    import echarts from 'echarts'
    import 'echarts-liquidfill'
    Vue.prototype.$echarts = echarts
    复制代码
    • 1
    • 2
    • 3
    • 4
    • 5

    然后创建一个utils/resizeMixin.js文件去做大屏的自适应图表 并且创建一个utils/index文件做一个防抖的函数 去控制屏幕大小变化的频率

    • resizeMixin.js文件
    // 混入代码 resize-mixins.js
    import { debounce } from '@/utils';
    const resizeChartMethod = '$__resizeChartMethod';
    
    • 1
    • 2
    • 3
    • 4

    export default {
    data() {
    // 在组件内部将图表 init 的引用映射到 chart 属性上
    return {
    chart: null,
    };
    },
    created() {
    window.addEventListener(‘resize’, this[resizeChartMethod], false);
    },
    activated() {
    // 防止 keep-alive 之后图表变形
    if (this.chart) {
    this.chart.resize()
    }
    },
    beforeDestroy() {
    window.removeEventListener(‘reisze’, this[resizeChartMethod]);
    },
    methods: {
    // 防抖函数来控制 resize 的频率
    [resizeChartMethod]: debounce(function() {
    if (this.chart) {
    this.chart.resize();
    }
    }, 300),
    },
    };

    复制代码

    • utils/index.js文件
    export function debounce(fn, delay) {
      var timer;
      return function () {
        var context = this;
        var args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function () {
          fn.apply(context, args);
        }, delay);
      };
    }
    复制代码
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    然后就是封装echarts了 创建一个echarts的文件夹 里面一个index.vue是组件 还有一个theme.json是主题文件

    • index.vue
    
    
    • 1
    • 2
    • 3
    • 4

    <script>
    import tdTheme from ‘./theme.json’ // 引入默认主题
    import resizeMixins from “@/utils/resizeMixins”;
    import ‘…/map/fujian.js’

    export default {
    name: ‘echart’,
    mixins: [resizeMixins],
    props: {
    className: {
    type: String,
    default: ‘chart’
    },
    id: {
    type: String,
    default: ‘chart’
    },
    width: {
    type: String,
    default: ‘100%’
    },
    height: {
    type: String,
    default: ‘2.5rem’
    },
    options: {
    type: Object,
    default: ()=>({})
    }
    },
    data () {
    return {
    chart: null
    }
    },
    watch: {
    options: {
    handler (options) {
    // 设置true清空echart缓存
    this.chart.setOption(options, true)
    },
    deep: true
    }
    },
    mounted () {
    this.KaTeX parse error: Expected 'EOF', got '}' at position 281: …rt(); }̲, echarts.init(this. e l < / s p a n > , < s p a n c l a s s = " h l j s − s t r i n g " > ′ t d T h e m e ′ < / s p a n > ) < s p a n c l a s s = " h l j s − v a r i a b l e l a n g u a g e " > t h i s < / s p a n > . el, 'tdTheme') this. el</span>,<spanclass="hljsstring">tdTheme</span>)<spanclass="hljsvariablelanguage">this</span>.emit(‘myCharts’, this.chart) // 把实例丢出来
    this.chart.setOption(this.options, true)
    }
    }
    }
    script>

    <style>
    style>

    复制代码

    • theme.json
    {
      "color": ["#2d8cf0", "#19be6b", "#ff9900", "#E46CBB", "#9A66E4", "#ed3f14"],
      "backgroundColor": "rgba(0,0,0,0)",
      "textStyle": {},
      "title": {
        "textStyle": {
          "color": "#516b91"
        },
        "subtextStyle": {
          "color": "#93b7e3"
        }
      },
      "line": {
        "itemStyle": {
          "normal": {
            "borderWidth": "2"
          }
        },
        "lineStyle": {
          "normal": {
            "width": "2"
          }
        },
        "symbolSize": "6",
        "symbol": "emptyCircle",
        "smooth": true
      },
      "radar": {
        "itemStyle": {
          "normal": {
            "borderWidth": "2"
          }
        },
        "lineStyle": {
          "normal": {
            "width": "2"
          }
        },
        "symbolSize": "6",
        "symbol": "emptyCircle",
        "smooth": true
      },
      "bar": {
        "itemStyle": {
          "normal": {
            "barBorderWidth": 0,
            "barBorderColor": "#ccc"
          },
          "emphasis": {
            "barBorderWidth": 0,
            "barBorderColor": "#ccc"
          }
        }
      },
      "pie": {
        "itemStyle": {
          "normal": {
            "borderWidth": 0,
            "borderColor": "#ccc"
          },
          "emphasis": {
            "borderWidth": 0,
            "borderColor": "#ccc"
          }
        }
      },
      "scatter": {
        "itemStyle": {
          "normal": {
            "borderWidth": 0,
            "borderColor": "#ccc"
          },
          "emphasis": {
            "borderWidth": 0,
            "borderColor": "#ccc"
          }
        }
      },
      "boxplot": {
        "itemStyle": {
          "normal": {
            "borderWidth": 0,
            "borderColor": "#ccc"
          },
          "emphasis": {
            "borderWidth": 0,
            "borderColor": "#ccc"
          }
        }
      },
      "parallel": {
        "itemStyle": {
          "normal": {
            "borderWidth": 0,
            "borderColor": "#ccc"
          },
          "emphasis": {
            "borderWidth": 0,
            "borderColor": "#ccc"
          }
        }
      },
      "sankey": {
        "itemStyle": {
          "normal": {
            "borderWidth": 0,
            "borderColor": "#ccc"
          },
          "emphasis": {
            "borderWidth": 0,
            "borderColor": "#ccc"
          }
        }
      },
      "funnel": {
        "itemStyle": {
          "normal": {
            "borderWidth": 0,
            "borderColor": "#ccc"
          },
          "emphasis": {
            "borderWidth": 0,
            "borderColor": "#ccc"
          }
        }
      },
      "gauge": {
        "itemStyle": {
          "normal": {
            "borderWidth": 0,
            "borderColor": "#ccc"
          },
          "emphasis": {
            "borderWidth": 0,
            "borderColor": "#ccc"
          }
        }
      },
      "candlestick": {
        "itemStyle": {
          "normal": {
            "color": "#edafda",
            "color0": "transparent",
            "borderColor": "#d680bc",
            "borderColor0": "#8fd3e8",
            "borderWidth": "2"
          }
        }
      },
      "graph": {
        "itemStyle": {
          "normal": {
            "borderWidth": 0,
            "borderColor": "#ccc"
          }
        },
        "lineStyle": {
          "normal": {
            "width": 1,
            "color": "#aaa"
          }
        },
        "symbolSize": "6",
        "symbol": "emptyCircle",
        "smooth": true,
        "color": ["#2d8cf0", "#19be6b", "#f5ae4a", "#9189d5", "#56cae2", "#cbb0e3"],
        "label": {
          "normal": {
            "textStyle": {
              "color": "#eee"
            }
          }
        }
      },
      "map": {
        "itemStyle": {
          "normal": {
            "areaColor": "#f3f3f3",
            "borderColor": "#516b91",
            "borderWidth": 0.5
          },
          "emphasis": {
            "areaColor": "rgba(165,231,240,1)",
            "borderColor": "#516b91",
            "borderWidth": 1
          }
        },
        "label": {
          "normal": {
            "textStyle": {
              "color": "#000"
            }
          },
          "emphasis": {
            "textStyle": {
              "color": "rgb(81,107,145)"
            }
          }
        }
      },
      "geo": {
        "itemStyle": {
          "normal": {
            "areaColor": "#f3f3f3",
            "borderColor": "#516b91",
            "borderWidth": 0.5
          },
          "emphasis": {
            "areaColor": "rgba(165,231,240,1)",
            "borderColor": "#516b91",
            "borderWidth": 1
          }
        },
        "label": {
          "normal": {
            "textStyle": {
              "color": "#000"
            }
          },
          "emphasis": {
            "textStyle": {
              "color": "rgb(81,107,145)"
            }
          }
        }
      },
      "categoryAxis": {
        "axisLine": {
          "show": true,
          "lineStyle": {
            "color": "#cccccc"
          }
        },
        "axisTick": {
          "show": false,
          "lineStyle": {
            "color": "#333"
          }
        },
        "axisLabel": {
          "show": true,
          "textStyle": {
            "color": "#fff"
          }
        },
        "splitLine": {
          "show": false,
          "lineStyle": {
            "color": ["#eeeeee"]
          }
        },
        "splitArea": {
          "show": false,
          "areaStyle": {
            "color": ["rgba(250,250,250,0.05)", "rgba(200,200,200,0.02)"]
          }
        }
      },
      "valueAxis": {
        "axisLine": {
          "show": true,
          "lineStyle": {
            "color": "#cccccc"
          }
        },
        "axisTick": {
          "show": false,
          "lineStyle": {
            "color": "#333"
          }
        },
        "axisLabel": {
          "show": true,
          "textStyle": {
            "color": "#fff"
          }
        },
        "splitLine": {
          "show": false,
          "lineStyle": {
            "color": ["#eeeeee"]
          }
        },
        "splitArea": {
          "show": false,
          "areaStyle": {
            "color": ["rgba(250,250,250,0.05)", "rgba(200,200,200,0.02)"]
          }
        }
      },
      "logAxis": {
        "axisLine": {
          "show": true,
          "lineStyle": {
            "color": "#cccccc"
          }
        },
        "axisTick": {
          "show": false,
          "lineStyle": {
            "color": "#333"
          }
        },
        "axisLabel": {
          "show": true,
          "textStyle": {
            "color": "#999999"
          }
        },
        "splitLine": {
          "show": true,
          "lineStyle": {
            "color": ["#eeeeee"]
          }
        },
        "splitArea": {
          "show": false,
          "areaStyle": {
            "color": ["rgba(250,250,250,0.05)", "rgba(200,200,200,0.02)"]
          }
        }
      },
      "timeAxis": {
        "axisLine": {
          "show": true,
          "lineStyle": {
            "color": "#cccccc"
          }
        },
        "axisTick": {
          "show": false,
          "lineStyle": {
            "color": "#333"
          }
        },
        "axisLabel": {
          "show": true,
          "textStyle": {
            "color": "#999999"
          }
        },
        "splitLine": {
          "show": true,
          "lineStyle": {
            "color": ["#eeeeee"]
          }
        },
        "splitArea": {
          "show": false,
          "areaStyle": {
            "color": ["rgba(250,250,250,0.05)", "rgba(200,200,200,0.02)"]
          }
        }
      },
      "toolbox": {
        "iconStyle": {
          "normal": {
            "borderColor": "#999"
          },
          "emphasis": {
            "borderColor": "#666"
          }
        }
      },
      "legend": {
        "textStyle": {
          "color": "#fff"
        }
      },
      "tooltip": {
        "axisPointer": {
          "lineStyle": {
            "color": "#ccc",
            "width": 1
          },
          "crossStyle": {
            "color": "#ccc",
            "width": 1
          }
        }
      },
      "timeline": {
        "lineStyle": {
          "color": "#8fd3e8",
          "width": 1
        },
        "itemStyle": {
          "normal": {
            "color": "#8fd3e8",
            "borderWidth": 1
          },
          "emphasis": {
            "color": "#8fd3e8"
          }
        },
        "controlStyle": {
          "normal": {
            "color": "#8fd3e8",
            "borderColor": "#8fd3e8",
            "borderWidth": 0.5
          },
          "emphasis": {
            "color": "#8fd3e8",
            "borderColor": "#8fd3e8",
            "borderWidth": 0.5
          }
        },
        "checkpointStyle": {
          "color": "#8fd3e8",
          "borderColor": "rgba(138,124,168,0.37)"
        },
        "label": {
          "normal": {
            "textStyle": {
              "color": "#8fd3e8"
            }
          },
          "emphasis": {
            "textStyle": {
              "color": "#8fd3e8"
            }
          }
        }
      },
      "visualMap": {
        "color": ["#516b91", "#59c4e6", "#a5e7f0"]
      },
      "dataZoom": [
        {
          "type": "inside",
          "backgroundColor": "rgba(0,0,0,0)",
          "dataBackgroundColor": "rgba(255,255,255,0.3)",
          "fillerColor": "rgba(167,183,204,0.4)",
          "handleColor": "#a7b7cc",
          "handleSize": "100%",
          "textStyle": {
            "color": "#333"
          }
        }
      ],
      "markPoint": {
        "label": {
          "normal": {
            "textStyle": {
              "color": "#eee"
            }
          },
          "emphasis": {
            "textStyle": {
              "color": "#eee"
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456

    复制代码

    使用这个组件的方法

    import Echart from '@/echart' //引入echatrs组件
     components: { //并且注册组件
        Echart,
      },
    ===================  ===================  ===================  
    <Echart  :options="图表的配置" id="可选" height='100%' width="100%">Echart>
    复制代码
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    pandas中的数据结构
    Docker的安装部署
    知识图谱:语义网络、语义网、链接数据、知识图谱
    Biomedical knowledge graph-enhanced prompt generation for large language models
    windows11 利用vmware17 安装rocky9操作系统
    输入一个三位数,使用递归实现,按位输出
    谷粒商城十一商城系统及整合thymeleaf渲染商城首页
    Charles的功能操作
    求职阿里 Java 技术岗位的经历,三轮技术面 +HR 面,面试也不过如此
    RecycleView缓存复用详细解析
  • 原文地址:https://blog.csdn.net/weixin__BJ050106/article/details/126248600