
<template>
<div
id="diseases-distribution"
ref="diseases_distribution"
:style="{height:height,width:width}"
></div>
</template>
<script>
import echarts from 'echarts'
import resize from '@/util/resize'
export default {
name: "DiseasesDistribution",
mixins:[resize],
props:{
width: {
type: String,
default: '100%'
},
height: {
type: String,
default: '100%'
},
title:{
type: String,
default: ''
},
data:{
type:Array,
default:()=>[]
},
},
mounted() {
this.initChart()
},
watch:{
data: {
deep: true,
handler() {
this.initChart()
}
}
},
beforeDestroy() {
if (!this.chart) {
return
}
this.chart.dispose()
this.chart = null
},
methods:{
initChart() {
this.chart = echarts.init(this.$refs.diseases_distribution)
const dataLine = []
const final = []
const name = []
let dataTotal = 0
for (let i = this.data.length-1; i >=0 ; i--) {
name.push(this.data[i].name)
dataTotal += parseInt(this.data[i].value)
}
for (let i = this.data.length-1; i >=0 ; i--) {
if ((dataTotal)==0){
dataLine.push(0)
}else {
dataLine.push(((parseInt(this.data[i].value)/dataTotal)*100).toFixed(2))
}
final.push(this.data[i].value)
}
const option = {
title: {
text: this.title,
x:20,
textStyle: {
fontSize: 15,
color: 'black',
fontWeight: 'bolder',
},
},
backgroundColor: '#fff',
yAxis: {
type: 'category',
axisLine: {
show: false
},
axisTick: {
show: false
},
axisLabel: {
color: '#999'
},
data: name
},
xAxis: {
show: true,
splitLine:{
show:false
},
axisLine: {
show: true,
lineStyle:{
color:'#ccc'
}
},
axisLabel: {
color: '#000'
},
axisTick: {
show: true
},
},
tooltip: {
show: true,
},
grid: {
top: '13%',
right: '12%',
left: '12%',
bottom: '13%'
},
animationDuration: 100,
series: [{
type: 'bar',
barWidth: '15px',
showBackground: true,
backgroundStyle: {
color: 'rgba(220, 220, 220, 0.4)',
},
label: {
normal: {
show: true,
position: "right",
distance: 8,
formatter: function(data) {
return dataLine[data.dataIndex]+ "%"
},
textStyle: {
color: "#000000",
fontSize: 16
}
}
},
itemStyle: {
normal: {
color: '#8167F5',
shadowBlur: 10,
shadowColor: 'rgba(0, 103, 255, 0.2)',
shadowOffsetX: -5,
shadowOffsetY: 5,
},
},
data: final
}]
}
this.chart.setOption(option)
}
},
}
</script>
<style scoped>
</style>