YZProjectCloud/yanzhu-bigscreen/src/components/project-overview-chart.vue

237 lines
8.0 KiB
Vue
Raw Normal View History

2024-11-27 23:42:39 +08:00
<template>
2025-05-11 00:09:30 +08:00
<div class="project-overview-chart" style="position: relative" @click="doClick">
<div :style="'height:'+height+'px;'+(width?('width:'+width+'px;'):'')" ref="warningPieChart"></div>
<div class="chart-gif chart-overview-gif" :style="'top:'+gifTop"></div>
<div v-if="htmlShow" class="chart-text" :style="'top:'+gifTop">
<div class="chart-text-title">{{title1 }}</div>
<div class="chart-text-sub-title">{{subTitle }}</div>
</div>
</div>
2024-11-27 23:42:39 +08:00
</template>
<script>
export default {
name: 'JhbigscreenProjectOverviewChart',
props: {
2025-05-11 00:09:30 +08:00
htmlShow: {
type: Boolean,
default: false,
2024-11-27 23:42:39 +08:00
},
2025-05-11 00:09:30 +08:00
txtTop: {
type: String,
default: '0',
2024-11-27 23:42:39 +08:00
},
2025-05-11 00:09:30 +08:00
gifTop: {
type: String,
default: '63px',
2024-11-27 23:42:39 +08:00
},
2025-05-11 00:09:30 +08:00
fn: {
type: Function,
},
typedata: {
2024-11-27 23:42:39 +08:00
type: Array,
},
2025-05-11 00:09:30 +08:00
width: {
type: Number,
2024-11-27 23:42:39 +08:00
},
2025-05-11 00:09:30 +08:00
height: {
type: Number,
2024-11-27 23:42:39 +08:00
},
2025-05-11 00:09:30 +08:00
text: {
type: String,
2024-11-27 23:42:39 +08:00
},
2025-05-11 00:09:30 +08:00
legendOpt: {
type: Object,
default: () => {},
2024-11-27 23:42:39 +08:00
},
2025-05-11 00:09:30 +08:00
maintitle: {
type: [String, Number],
default: '',
},
sp: {
type: String,
default: '\n',
2024-11-27 23:42:39 +08:00
},
},
data() {
return {
2025-05-11 00:09:30 +08:00
active: 0,
option: {},
title1: '',
subTitle: '',
2024-11-27 23:42:39 +08:00
}
},
2025-05-11 00:09:30 +08:00
mounted() {
2024-11-27 23:42:39 +08:00
this.init()
},
methods: {
2025-05-11 00:09:30 +08:00
doClick() {
this.$emit('clickme')
},
init() {
2024-11-27 23:42:39 +08:00
this.getChartData()
},
2025-05-11 00:09:30 +08:00
getChartData() {
2024-11-27 23:42:39 +08:00
//品类金额占比 饼图
2025-05-11 00:09:30 +08:00
var chChartPie = echarts.init(this.$refs.warningPieChart)
this.echartPie(chChartPie, this.typedata)
2024-11-27 23:42:39 +08:00
},
2025-05-11 00:09:30 +08:00
echartPie(chChart, chartData) {
2024-11-27 23:42:39 +08:00
let newPromise = new Promise((resolve) => {
resolve()
})
//然后异步执行echarts的初始化函数
newPromise.then(() => {
2025-05-11 00:09:30 +08:00
var total_datas = 0
var data = []
var legendData = []
var color = ['#4974ff', '#52aef7', '#6863d7', '#1d5d89', '#20e6ff', '#67feef']
for (let i = 0; i < chartData.length; i++) {
total_datas += Number(chartData[i].value)
2024-11-27 23:42:39 +08:00
legendData.push(chartData[i].name)
2025-05-11 00:09:30 +08:00
data.push({
value: chartData[i].value,
name: chartData[i].name,
itemStyle: {
//颜色渐变
color: color[i],
2024-11-27 23:42:39 +08:00
},
2025-05-11 00:09:30 +08:00
})
}
2024-11-27 23:42:39 +08:00
/* let total = chartData.reduce((a, b) => {
return a + b.value;
}, 0);*/
2025-05-11 00:09:30 +08:00
let is1K = this.$dpi() == '1K'
let is2K = this.$dpi() == '2K'
2024-11-27 23:42:39 +08:00
let legendOption = {
2025-05-11 00:09:30 +08:00
top: 'center',
2024-11-27 23:42:39 +08:00
2025-05-11 00:09:30 +08:00
orient: 'vertical',
icon: 'circle',
2024-11-27 23:42:39 +08:00
itemWidth: 12,
itemGap: 8,
textStyle: {
2025-05-11 00:09:30 +08:00
color: '#c3dbfd',
fontSize: is1K ? 14 : is2K ? 18 : 24,
rich: {
2024-11-27 23:42:39 +08:00
name: {
2025-05-11 00:09:30 +08:00
color: '#c3dbfd',
fontSize: is1K ? 14 : is2K ? 18 : 24,
padding: [10, 5, 20, 5],
2024-11-27 23:42:39 +08:00
},
percent: {
2025-05-11 00:09:30 +08:00
color: '#18DB9F',
fontSize: is1K ? 16 : is2K ? 24 : 32,
padding: [0, 5, 0, 5],
2024-11-27 23:42:39 +08:00
},
},
},
2025-05-11 00:09:30 +08:00
formatter: (name) => {
let res = chartData.filter((v) => v.name === name)
let percent = ((res[0].value * 100) / total_datas).toFixed(1)
if (total_datas == 0) {
percent = 0
2024-11-27 23:42:39 +08:00
}
2025-05-11 00:09:30 +08:00
return '{name| ' + name + '}' + this.sp + '{val|' + res[0].value + '} {percent|' + percent + '%}'
2024-11-27 23:42:39 +08:00
},
2025-05-11 00:09:30 +08:00
}
let opt = { ...legendOption, ...(this.legendOpt || {}) }
this.title1 = this.maintitle || total_datas
this.subTitle = this.text
this.option = {
2024-11-27 23:42:39 +08:00
title: {
2025-05-11 00:09:30 +08:00
show: !this.htmlShow,
text: this.maintitle || total_datas,
2024-11-27 23:42:39 +08:00
subtext: this.text,
2025-05-11 00:09:30 +08:00
textAlign: 'center',
top: this.txtTop,
itemGap: 10,
2024-11-27 23:42:39 +08:00
textStyle: {
2025-05-11 00:09:30 +08:00
color: '#0dd2fd',
2024-11-27 23:42:39 +08:00
fontSize: 24,
2025-05-11 00:09:30 +08:00
fontWeight: 'bold',
align: 'center',
2024-11-27 23:42:39 +08:00
},
subtextStyle: {
2025-05-11 00:09:30 +08:00
color: '#a5b5f0',
2024-11-27 23:42:39 +08:00
fontSize: 12,
2025-05-11 00:09:30 +08:00
align: 'center',
2024-11-27 23:42:39 +08:00
},
2025-05-11 00:09:30 +08:00
padding: [95, 0, 0, 110],
left: 'left',
2024-11-27 23:42:39 +08:00
},
tooltip: {
trigger: 'item',
2025-05-11 00:09:30 +08:00
formatter: '{b} <br/>{c} ({d}%)',
textStyle: {
fontSize: is1K ? 14 : is2K ? 18 : 24,
},
2024-11-27 23:42:39 +08:00
},
2025-05-11 00:09:30 +08:00
2024-11-27 23:42:39 +08:00
legend: [
{
right: 10,
data: legendData,
2025-05-11 00:09:30 +08:00
align: 'left',
2024-11-27 23:42:39 +08:00
...opt,
},
],
series: [
{
2025-05-11 00:09:30 +08:00
name: '品类金额占比',
type: 'pie',
center: ['25%', '50%'],
radius: ['46%', '63%'],
2024-11-27 23:42:39 +08:00
data: data,
label: {
show: false,
},
itemStyle: {
normal: {
borderWidth: 5,
2025-05-11 00:09:30 +08:00
borderColor: '#051a36',
},
2024-11-27 23:42:39 +08:00
},
},
{
2025-05-11 00:09:30 +08:00
name: '外边框',
type: 'pie',
2024-11-27 23:42:39 +08:00
clockWise: false, //顺时加载
hoverAnimation: false, //鼠标移入变大
2025-05-11 00:09:30 +08:00
center: ['25%', '50%'],
radius: ['70%', '70%'],
2024-11-27 23:42:39 +08:00
label: {
normal: {
show: false,
},
},
data: [
{
value: 9,
2025-05-11 00:09:30 +08:00
name: '',
2024-11-27 23:42:39 +08:00
itemStyle: {
normal: {
borderWidth: 3,
2025-05-11 00:09:30 +08:00
borderColor: '#152c65',
2024-11-27 23:42:39 +08:00
},
},
},
],
},
],
}
2025-05-11 00:09:30 +08:00
if (this.fn) {
this.option = this.fn(this.option)
2024-11-27 23:42:39 +08:00
}
2025-05-11 00:09:30 +08:00
chChart.setOption(this.option)
window.onresize = chChart.resize
2024-11-27 23:42:39 +08:00
})
},
2025-05-11 00:09:30 +08:00
},
}
2024-11-27 23:42:39 +08:00
</script>