• 【echarts】07、echarts+vue2 - 环形图


    前言

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

    实现效果

    在这里插入图片描述

    代码实现

    <template>
      <div class="chart-wrap">
        <ul class="legend-list">
          <li
            v-for="(item, index) in data"
            :key="index"
            :class="['legend', item.selected ? '': 'un-active']"
            @mouseenter="enterHandler(item)"
            @mouseleave="leaveHandler(item)"
            @click="clickHandler(item)"
          >
            <i class="radius-rect" :style="{ backgroundColor: item.color }" />
            <span>{{ item.name }}span>
          li>
        ul>
        <div id="chart04" class="chart" />
      div>
    template>
    
    <script>
    export default {
      name: 'Index',
      data () {
        return {
          chart: null,
          data: [
            { name: '公益志愿者活动', value: 55, color: '#013AAA', selected: true },
            { name: '太极拳活动', value: 15, color: '#E92B97', selected: true },
            { name: '暑期出行计划', value: 30, color: '#0593F7', selected: true },
            { name: '其他', value: 30, color: '#F4AD43', selected: true }
          ]
        }
      },
      mounted() {
        this.createChartHandler()
      },
      methods: {
        // 创建图表
        createChartHandler () {
          this.chart = this.$echarts.init(document.getElementById('chart04'))
          this.chart.setOption(this.getChartOption(this.data, this.chart))
          window.addEventListener('resize', () => {
            setTimeout(() => {
              this.chart.resize()
            })
          })
        },
        // 获取图表配置项
        getChartOption (data, chart) {
          return {
            title: {
              text: [
                `{value|${data.reduce((t, s) => { return t + s.value }, 0)}}`,
                '{name|活动总数}'
              ].join('\n'),
              left: 'center',
              top: 'center',
              textStyle: {
                rich: {
                  value: {
                    fontSize: '20',
                    color: '#00D7DA',
                    align: 'center',
                    lineHeight: 28,
                    fontFamily: 'PingFangBold',
                    fontWeight: 'bold'
                  },
                  name: {
                    fontSize: '14',
                    color: '#fff',
                    align: 'center',
                    fontFamily: 'PingFangReg',
                    fontWeight: '400'
                  }
                }
              }
            },
            tooltip: {
              trigger: 'item',
              position: 'right',
              extraCssText:
                'color:#fff;background: rgba(0, 38, 118, 0.5);border:none; box-shadow: 0px 0px 8px 1px rgba(0, 145, 255, 0.5);border-radius: 2px;z-index:99',
              formatter: function (e) {
                return e.name + ':  ' + e.percent + '%'
              }
            },
            legend: {
              show: false,
              data: data.map(i => i.name)
            },
            color: data.map(i => i.color),
            series: [
              {
                name: '',
                type: 'pie',
                radius: ['45%', '60%'],
                center: ['50%', '50%'],
                label: {
                  alignTo: 'edge',
                  formatter: '{name|{d}%}\n',
                  minMargin: 5,
                  edgeDistance: 10,
                  lineHeight: 30,
                  rich: {
                    name: {
                      fontSize: 14,
                      color: '#fff',
                      fontFamily: 'PingFangReg',
                      fontWeight: '400'
                    }
                  }
                },
                labelLine: {
                  length: 15,
                  length2: 0,
                  maxSurfaceAngle: 80
                },
                labelLayout: (params) => {
                  const isLeft = params.labelRect.x < chart.getWidth() / 2
                  const points = params.labelLinePoints
                  points[2][0] = isLeft
                    ? params.labelRect.x
                    : params.labelRect.x + params.labelRect.width
                  return {
                    labelLinePoints: points
                  }
                },
                data
              }
            ]
          }
        },
        // 鼠标移入图例触发
        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
  • 相关阅读:
    sqli-labs/Less-60
    excel求和怎么操作?这三个简单操作方法,轻松掌握
    基于HTML制作的闲置交易网站设计
    Java基于基于微信小程序的快递柜管理系统
    什么是FastAPI异步框架?(全面了解)
    向函数传递参数(传值、传引用、传const引用)
    证书+证书链的简单理解
    湖州OLED透明拼接屏技术应用引领现代化旅游观光方式
    vue2升级vue3: TSX Vue 3 Composition API Refs
    架构面试题汇总(一)
  • 原文地址:https://blog.csdn.net/weixin_44402694/article/details/126001647