• 【echarts】09、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="chart06" class="chart" />
      div>
    template>
    
    <script>
    export default {
      name: 'Index',
      data () {
        return {
          chart: null,
          yData: [
            { name: '参与人数', value: [280, 180, 30, 230, 240, 50], color: '#24DCF0', selected: true },
            { name: '活动次数', value: [140, 60, 20, 120, 40, 130], color: '#F7B500', selected: true }
          ],
          xData: ['研学活动', '老年大学', '兴趣教育', '技能培训', '公益帮扶', '艺术创作']
        }
      },
      mounted() {
        this.createChartHandler()
      },
      methods: {
        // 创建图表
        createChartHandler () {
          this.chart = this.$echarts.init(document.getElementById('chart06'))
          this.chart.setOption(this.getChartOption(this.yData, this.xData))
          window.addEventListener('resize', () => {
            setTimeout(() => {
              this.chart.resize()
            })
          })
        },
        // 获取图表配置项
        getChartOption (yData, xData) {
          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: '8%', right: '5%', bottom: '15%' }, xAxis: [ { type: 'category', data: xData, axisLabel: { interval: 0, fontFamily: 'PingFangReg', lineHeight: 17, fontSize: 12, color: '#fff' }, axisTick: { show: true, alignWithLabel: true, lineStyle: { color: '#24DCF0' } }, axisLine: { show: true, lineStyle: { color: '#24DCF0' } } } ], yAxis: [ { type: 'value', interval: 50, axisLabel: { fontFamily: 'PingFangReg', fontSize: 12, color: '#24DCF0' }, axisLine: { show: true, lineStyle: { color: '#24DCF0' } }, splitLine: { show: true, lineStyle: { color: '#24DCF0', type: 'dashed' } } } ], series: yData.map((i) => { return { name: i.name, type: 'bar', barWidth: 9, barGap: 0.2, data: i.value, itemStyle: { color: i.color } } }) } }, // 鼠标移入图例触发 enterHandler (item) { if (!this.chart) return this.chart.dispatchAction({ type: 'highlight', name: item.name }) }, // 鼠标移出图例触发 leaveHandler (item) { if (!this.chart) return this.chart.dispatchAction({ type: 'downplay', name: 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
  • 相关阅读:
    java的Exception.getMessage为null
    亿级流量高并发春晚互动前端技术揭秘
    5分钟彻底搞懂this指向问题 (附练习题)
    .NET周刊【2月第2期 2024-02-11】
    网安朝阳·西门子白帽黑客大赛 | 聚焦实战攻防竞赛 促进网安人才发展
    泛型与Gson解析
    【随笔】移动端input type|语义与IOS按键
    深度学习P1——实现mnist手写数字识别
    Win Docker Desktop + WSL2 部署PyTorch-CUDA服务至k8s算力集群
    为什么表数据删一半,表文件大小不变?
  • 原文地址:https://blog.csdn.net/weixin_44402694/article/details/126001696