rax-medical/src/views/home/number-pie-chart.vue

190 lines
4.4 KiB
Vue
Raw Normal View History

2023-12-21 17:31:28 +08:00
<template>
2024-05-17 12:06:29 +08:00
<div class="chart-box">
<div class="date-btn text-color">
<div class="btn-box">
<el-icon @click="getData(upMonth)">
<ArrowLeft/>
</el-icon>
<el-date-picker v-model="currentMonth" format="YYYY年MM月" type="month" :editable="false" :clearable="false"
@change="getData"/>
<el-icon @click="getData(downMonth)">
<ArrowRight/>
</el-icon>
</div>
2023-12-21 17:31:28 +08:00
</div>
2024-05-17 12:06:29 +08:00
<div class="total-box">
<div class="total-item" v-for="item in pieData" :key="item.name">{{ item.name }}:{{ item.value }}</div>
</div>
<div ref="chartDom" class="chart-dom"></div>
</div>
2023-12-21 17:31:28 +08:00
</template>
<script lang='ts' setup>
2024-05-17 12:06:29 +08:00
import {onMounted, ref} from 'vue'
2023-12-21 17:31:28 +08:00
import * as echarts from 'echarts';
2024-05-17 12:06:29 +08:00
import * as medicineApi from '@/api/medicine'
import {dateFormater, getWeekDates} from "@/utils/date-util";
import {generateRandomColor} from "@/utils/other";
2023-12-21 17:31:28 +08:00
const chartDom = ref()
const currentMonth = ref(new Date())
const upMonth = ref()
const downMonth = ref()
const pieData = ref([] as any)
onMounted(() => {
2024-05-17 12:06:29 +08:00
getData(new Date())
2023-12-21 17:31:28 +08:00
})
function initChart(chartData: any) {
2024-05-17 12:06:29 +08:00
const chart = echarts.init(chartDom.value as HTMLElement);
chart.clear();
const option = {
color: chartData.color,
tooltip: {
trigger: 'item',
formatter: (params: any) => {
return params.marker + params.name + '<br>数量:' + params.value + '<br>占比:' + params.percent + '%'
}
},
grid: {
left: 0,
right: 0,
bottom: 0,
top: 0,
containLabel: true,
},
series: [{
type: 'pie',
center: ['50%', '50%'],
radius: ['0%', '60%'],
clockwise: true,
label: {
show: true,
position: 'outter',
color: 'inherit',
overflow: 'break',
formatter: function (params: any) {
return params.name + params.percent + '%'
}
},
labelLine: {
show: false,
length: 3,
length2: 0,
smooth: true,
},
data: chartData.data
}],
};
chart.setOption(option);
chart.resize();
window.addEventListener('resize', () => {
2023-12-21 17:31:28 +08:00
chart.resize();
2024-05-17 12:06:29 +08:00
});
2023-12-21 17:31:28 +08:00
}
const getData = (date: any) => {
2024-05-17 12:06:29 +08:00
currentMonth.value = new Date(date)
upMonth.value = new Date(currentMonth.value.getFullYear(), currentMonth.value.getMonth() - 1, 1)
downMonth.value = new Date(currentMonth.value.getFullYear(), currentMonth.value.getMonth() + 1, 1)
const color: any = []
pieData.value = []
const start = dateFormater("yyyy-MM-dd", new Date(currentMonth.value.getFullYear(), currentMonth.value.getMonth(), 1))
const end = dateFormater("yyyy-MM-dd", new Date(currentMonth.value.getFullYear(), currentMonth.value.getMonth() + 1, 0))
medicineApi.getSurgeryTypeProportion(start, end).then(res => {
if (res.code == 0) {
res.data.forEach((item: any, i: number) => {
const c = generateRandomColor()
color.push(c)
pieData.value.push({
value: item.count,
name: item._id,
itemStyle: {
color: c
}
})
})
}
initChart({data: pieData.value, color})
});
2023-12-21 17:31:28 +08:00
}
</script>
<style lang='scss' scoped>
.chart-box {
2024-05-17 12:06:29 +08:00
position: relative;
width: 100%;
height: 100%;
display: flex;
justify-content: space-between;
align-items: center;
.date-btn {
position: absolute;
height: 50px;
top: 0;
left: 20px;
right: 20px;
2023-12-21 17:31:28 +08:00
display: flex;
2024-05-17 12:06:29 +08:00
z-index: 1;
.btn-box {
height: 30px;
display: flex;
align-items: center;
color: $text2-color;
& > :deep(.el-date-editor) {
font-size: 14px;
2023-12-21 17:31:28 +08:00
2024-05-17 12:06:29 +08:00
.el-input__wrapper {
box-shadow: none;
padding: 0;
.el-input__prefix,
.el-input__suffix {
display: none;
}
.el-input__inner {
cursor: pointer;
width: 100px;
color: $text2-color;
text-align: center;
}
2023-12-21 17:31:28 +08:00
}
2024-05-17 12:06:29 +08:00
}
2023-12-21 17:31:28 +08:00
2024-05-17 12:06:29 +08:00
.el-icon {
cursor: pointer;
}
2023-12-21 17:31:28 +08:00
}
2024-05-17 12:06:29 +08:00
}
2023-12-21 17:31:28 +08:00
2024-05-17 12:06:29 +08:00
.chart-dom {
width: 70%;
height: 100%;
margin-left: 30%;
}
.total-box {
position: absolute;
width: 30%;
top: 30px;
left: 20px;
bottom: 0;
color: $text2-color;
font-size: 14px;
display: flex;
flex-direction: column;
justify-content: center;
.total-item {
padding: 5px 0;
2023-12-21 17:31:28 +08:00
}
2024-05-17 12:06:29 +08:00
}
2023-12-21 17:31:28 +08:00
}</style>