<template>
<!-- 折线图 -->
<div ref="line" class="line"></div>
</template>
<script>
// colorList 横条颜色数组
// data 数据 格式为: []
// areaStyle 渐变色
import * as echarts from 'echarts'
import { onMounted, reactive, toRefs, ref, watch } from 'vue'
export default {
props: {
areaStyle: {
type: Array,
default: () => {
return []
}
},
optionData: {
type: Array,
default: () => {
return []
}
},
colorList: {
type: Array,
default: () => {
return []
}
}
},
setup(props) {
let line = ref('')
const state = reactive({})
// let series = []
// props.data.forEach(item => {
// series.push({})
// })
const methods = {
drawLine() {
// 初始化
var myChart = echarts.init(line.value)
// 配置项
var color = ['#FC4567', '#2F8DF4', '#C25EC4']
let option = {
color: color,
// backgroundColor: '#0A173B',
title: {
show: false,
text: '南丁格尔',
left: 'center',
top: '50%',
textStyle: {
fontSize: 22,
color: '#fff',
fontWeight: 'normal'
}
},
tooltip: {
trigger: 'item'
},
legend: {
show: false,
orient: 'vertical',
right: 20,
top: 20
},
series: [
{
type: 'pie',
roseType: 'radius',
radius: ['30%', '60%'],
data: props.optionData,
label: {
normal: {
formatter: '{font|{b}}\n{hr|}\n{font|{d}%}',
rich: {
font: {
fontSize: 20,
padding: [5, 0],
color: '#fff'
},
hr: {
height: 0,
borderWidth: 1,
width: '100%',
borderColor: '#fff'
}
}
}
},
labelLine: {
// legend: 15,
// legend2: 1,
lineStyle: {
color: '#fff'
}
},
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0,0,0,0.5)'
}
}
}
]
}
myChart.clear()
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option)
window.onresize = function () {
myChart.resize()
}
}
}
onMounted(() => {
methods.drawLine()
})
watch(
props,
newVal => {
if (newVal) {
methods.drawLine() //调用ECharts的方法重新绘制
}
},
{
// 被侦听的内容需要是函数的写法 () => stuInfo.friend
deep: true
}
)
return {
...toRefs(state),
...methods,
line
}
}
// props: {
// styles: {
// type: Object,
// default: () => {
// return {}
// }
// },
// colorList: {
// type: Array,
// default: () => {
// return []
// }
// },
// datas: {
// type: Array,
// default: () => {
// return []
// }
// },
// title: {
// type: String,
// default: '标题'
// }
// },
}
</script>
<style lang="scss" scoped>
.line {
width: 100%;
height: 100%;
}
</style>
需要传入一个数值数组
optionData: [
{
name: '水',
value: 276
},
{
name: '电',
value: 188
},
{
name: '煤',
value: 88
},
{
name: '天然气',
value: 78
}
],