Commit 069d5032 authored by 郭铭瑶's avatar 郭铭瑶 🤘

图表组件加同心圆功能

parent 306eb16a
<template> <template>
<m-animate :enter="enter" :leave="leave"> <m-animate :enter="enter" :leave="leave">
<div class="monitor-card"> <div class="monitor-card">
<component :is="card" v-bind="$attrs"> <component :is="card" v-bind="$attrs" :title="title">
<slot/> <slot/>
</component> </component>
</div> </div>
...@@ -12,6 +12,9 @@ ...@@ -12,6 +12,9 @@
export default { export default {
name: 'MonitorCard', name: 'MonitorCard',
props: { props: {
title: {
type: String,
},
mode: { mode: {
type: [String, Number], type: [String, Number],
default: '1', default: '1',
......
...@@ -88,6 +88,7 @@ export default { ...@@ -88,6 +88,7 @@ export default {
/** 设置坐标轴 */ /** 设置坐标轴 */
setAxis(options) { setAxis(options) {
const {xAxis, yAxis, shape} = this.config const {xAxis, yAxis, shape} = this.config
if (shape[0].type === 'pie' || shape[0].type === 'donut') return
const config = { const config = {
data: this.data.map(item => item[(yAxis && yAxis.key) || xAxis.key]), data: this.data.map(item => item[(yAxis && yAxis.key) || xAxis.key]),
boundaryGap: !shape.every(item => item.type === 'line') boundaryGap: !shape.every(item => item.type === 'line')
...@@ -130,16 +131,16 @@ export default { ...@@ -130,16 +131,16 @@ export default {
/** 设置图例说明 */ /** 设置图例说明 */
setLegend(options) { setLegend(options) {
const {legend} = this.config const {legend} = this.config
if (legend.hide) { if (legend && legend.hide) {
options.grid.top = '5%' options.grid.top = '5%'
return return
} }
options.legend = Object.assign({}, this.defaultOptions.legend, this.options.legend) options.legend = Object.assign({}, this.defaultOptions.legend, this.options.legend)
if (legend.orient) { if (legend && legend.orient) {
options.legend.orient = legend.orient options.legend.orient = legend.orient
} }
options.legend.data = this.dataSource.map(item => item.name) options.legend.data = this.dataSource.map(item => item.name)
switch (legend.align) { switch (legend && legend.align) {
case 'left': case 'left':
options.legend.left = '5%' options.legend.left = '5%'
break break
...@@ -156,19 +157,11 @@ export default { ...@@ -156,19 +157,11 @@ export default {
const {shape} = this.config const {shape} = this.config
// 如果为饼图则数据需是[{name: 'name', value: 1}]格式,且不需要坐标轴 // 如果为饼图则数据需是[{name: 'name', value: 1}]格式,且不需要坐标轴
if (shape[0].type === 'pie') { if (shape[0].type === 'pie') {
options.color = this.colors this.setPie(options)
options.series = [{ return
label: { }
normal: { if (shape[0].type === 'donut') {
show: false, this.setDonut(options)
},
},
...shape[0],
}]
options.series[0].data = options.legend.data = this.data
options.tooltip.trigger = 'item'
this.$delete(options, 'xAxis')
this.$delete(options, 'yAxis')
return return
} }
...@@ -198,6 +191,90 @@ export default { ...@@ -198,6 +191,90 @@ export default {
return result return result
}) })
}, },
// 绘制饼图
setPie(options) {
const {shape} = this.config
options.color = this.colors
options.series = [{
label: {
normal: {
show: false,
},
},
...shape[0],
}]
if (options.legend && options.legend.data) {
options.series[0].data = options.legend.data = this.data
} else {
options.series[0].data = this.data
}
options.tooltip.trigger = 'item'
this.$delete(options, 'xAxis')
this.$delete(options, 'yAxis')
},
// 绘制同心圆图
setDonut(options) {
const {shape} = this.config
options.color = this.colors
options.series = this.getDonutConfig(shape)
},
getDonutConfig(args) {
const len = args.length
if (len <= 0 ) return
const total = this.data.reduce((acc, cur) => { // 计算数据的value总和
return acc + cur.value
}, 0)
return args.map((e, i) => {
const rate = Math.round((len - i) * this.fontSize)
let borderColor = this.colors[i]
if(Array.isArray(borderColor)) {
borderColor = {
type: 'linear',
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [{
offset: 0, color: borderColor[0] // 0% 处的颜色
}, {
offset: 1, color: borderColor[1] // 100% 处的颜色
}],
global: false, // 缺省为 false
}
}
return {
type: 'pie',
name: e.name,
clockWise: false, //顺时加载
hoverAnimation: false, //鼠标移入变大
radius: [rate, rate + 1],
itemStyle: {
normal: {
label: {
show: false,
},
labelLine: {
show: false,
},
borderWidth: this.borderWidth,
borderColor: borderColor,
}
},
data: [
this.data[i],
{
value: Math.round(total - this.data[i].value),
name: '',
itemStyle: this.blackStyle,
}
]
}
})
},
}, },
computed: { computed: {
colors() { colors() {
...@@ -290,6 +367,24 @@ export default { ...@@ -290,6 +367,24 @@ export default {
fontSize() { fontSize() {
return Math.floor(screen.height * 1.48 / 100) return Math.floor(screen.height * 1.48 / 100)
}, },
borderWidth() {
return this.fontSize * 0.6
},
blackStyle() {
return {
normal: {
label: {
show: false,
},
labelLine: {
show: false
},
color: 'transparent',
borderColor: 'rgba(0,0,0,0.1)',
borderWidth: this.borderWidth,
},
}
},
}, },
watch: { watch: {
data(cur, past) { data(cur, past) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment