• 【echarts】13、echarts+vue2 - 折线图


    前言

    基于echarts5.x和vue2实现
    记录以便日后查阅
    
    • 1
    • 2

    实现效果

    在这里插入图片描述

    代码实现

    <template>
      <div class="chart-wrap">
        <ul class="legend-list">
          <li
            v-for="(item, index) in yData"
            :key="index"
            :class="['legend', item.selected ? '': 'un-active']"
            @mouseenter="enterHandler(item)"
            @mouseleave="leaveHandler(item)"
            @click="clickHandler(item)"
          >
            <i class="rect" :style="{ backgroundColor: item.color }" />
            <span>{{ item.name }}span>
          li>
        ul>
        <div id="chart10" class="chart" />
      div>
    template>
    
    <script>
    import echarts from '@/utils/echarts'
    
    export default {
      name: 'Index',
      data () {
        return {
          chart: null,
          yData: [
            { name: '综合能耗', value: [20, 80, 30, 40, 60, 40], color: '#0091FF', selected: true },
            { name: 'GDP能耗', value: [20, 60, 20, 100, 35, 90], color: '#45DE81', selected: true }
          ],
          xData: ['00:05', '00:10', '00:15', '00:20', '00:25', '00:30']
        }
      },
      mounted() {
        this.createChartHandler()
      },
      methods: {
        // 创建图表
        createChartHandler () {
          this.chart = this.$echarts.init(document.getElementById('chart10'))
          this.chart.setOption(this.getChartOption(this.yData, this.xData))
          window.addEventListener('resize', () => {
            setTimeout(() => {
              this.chart.resize()
            })
          })
        },
        // 获取图表配置项
        getChartOption (yData, xData) {
          const hexToRgba = (hex, opacity) => {
            let rgbaColor = ''
            const reg = /^#[\da-f]{6}$/i
            if (reg.test(hex)) {
              rgbaColor = `rgba(${parseInt('0x' + hex.slice(1, 3))},${parseInt(
                '0x' + hex.slice(3, 5)
              )},${parseInt('0x' + hex.slice(5, 7))},${opacity})`
            }
            return rgbaColor
          }
          return {
            tooltip: {
              trigger: 'axis',
              axisPointer: {
                type: 'shadow'
              },
              extraCssText:
                'fontFamily: PingFangReg;color:#fff;background:rgba(0,0,0,0.6000);border: unset;border-radius: 2px;z-index:99',
              formatter: (e) => {
                const div = document.createElement('div')
                div.innerHTML = `
              

    近期接能

    ${e .map((i) => { return `

    ${i.color};\"> ${i.seriesName}:${i.value}

    `
    }) .join('')}
    `
    return div } }, legend: { show: false, data: yData.map((i) => i.name) }, grid: { top: '5%', left: '10%', right: '5%', bottom: '15%' }, xAxis: [ { type: 'category', data: xData, boundaryGap: false, axisLabel: { interval: 0, fontFamily: 'PingFangReg', lineHeight: 17, fontSize: 14, color: 'rgba(255,255,255,0.6500)' }, axisTick: { show: true, alignWithLabel: true, lineStyle: { color: 'rgba(255,255,255,0.1500)' } }, axisLine: { show: true, lineStyle: { color: 'rgba(255,255,255,0.1500)' } } } ], yAxis: [ { type: 'value', axisLabel: { fontFamily: 'PingFangReg', fontSize: 14, color: 'rgba(255,255,255,0.6500)' }, axisLine: { show: false, lineStyle: { color: '#24DCF0' } }, splitLine: { show: true, lineStyle: { color: ' rgba(255,255,255,0.1500)' } } } ], series: yData.map((i) => { return { name: i.name, type: 'line', symbol: 'none', smooth: true, data: i.value, itemStyle: { color: i.color }, lineStyle: { shadowColor: i.color, // 透明的颜色 shadowOffsetX: 0, shadowOffsetY: 0, opacity: 1, // 透明度 shadowBlur: 20, // 阴影大小 type: 'solid', // 实线 width: 3 }, areaStyle: { normal: { color: new echarts.graphic.LinearGradient( 0, 0, 0, 1, [{ offset: 0, color: hexToRgba(i.color, 0.3) }, { offset: 1, color: hexToRgba(i.color, 0.01) } ], false ) } } } }) } }, // 鼠标移入图例触发 enterHandler (item) { if (!this.chart) return this.chart.dispatchAction({ type: 'highlight', seriesName: item.name }) }, // 鼠标移出图例触发 leaveHandler (item) { if (!this.chart) return this.chart.dispatchAction({ type: 'downplay', seriesName: item.name }) }, // 选中切换 clickHandler (item) { if (!this.chart) return item.selected = !item.selected this.chart.dispatchAction({ type: 'legendToggleSelect', name: item.name }) } } }
    script>
    • 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
  • 相关阅读:
    AVL树的插入详解
    团队管理之性能实施团队日志10
    错误: 找不到或无法加载主类 com.example.demo.DemoApplication 解决方案
    lintcode 567 · 最大得分 【动态规划 中等 】
    C++多线程学习06
    redis相关知识点
    【编程实践】使用pcl提取给定点云的三维边界点
    java毕业设计选题基于SSM毕业设计管理系统|毕设管理文档成绩Shiro
    wavesummit2024发布飞桨3.0版本
    0918 框架理论知识
  • 原文地址:https://blog.csdn.net/weixin_44402694/article/details/126001830