前言
基于echarts5.x和vue2实现
记录以便日后查阅
实现效果

代码实现
<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