YZProjectCloud/yanzhu-bigscreen/src/components/certificate-bar-chart.vue

150 lines
5.0 KiB
Vue
Raw Normal View History

2025-04-29 22:36:18 +08:00
<template>
2025-05-11 00:09:30 +08:00
<div :style="{ 'height': height + 'px' }" ref="chart"></div>
2025-04-29 22:36:18 +08:00
</template>
<script>
export default {
props: {
2025-05-11 00:09:30 +08:00
data: {
type: Array,
},
height: {
type: Number,
2025-04-29 22:36:18 +08:00
},
},
data() {
return {
2025-05-11 00:09:30 +08:00
option: {},
2025-04-29 22:36:18 +08:00
}
},
2025-05-11 00:09:30 +08:00
mounted() {
2025-04-29 22:36:18 +08:00
this.init()
},
methods: {
2025-05-11 00:09:30 +08:00
init() {
2025-04-29 22:36:18 +08:00
this.getChartData()
},
2025-05-11 00:09:30 +08:00
getChartData() {
2025-04-29 22:36:18 +08:00
//品类金额占比 饼图
2025-05-11 00:09:30 +08:00
var chChart = echarts.init(this.$refs.chart)
this.echart(chChart, this.data)
2025-04-29 22:36:18 +08:00
},
2025-05-11 00:09:30 +08:00
echart(chChart, chartData) {
2025-04-29 22:36:18 +08:00
let newPromise = new Promise((resolve) => {
resolve()
})
//然后异步执行echarts的初始化函数
newPromise.then(() => {
var value = []
var text = []
var total = 0
var bgd = []
for (let i = 0; i < chartData.length; i++) {
value.push(chartData[i].value)
text.push(chartData[i].text)
bgd.push(100)
total += chartData[i].value
}
var prop = []
for (let j = 0; j < value.length; j++) {
2025-05-11 00:09:30 +08:00
prop.push(((value[j] / total) * 100).toFixed(1))
2025-04-29 22:36:18 +08:00
}
2025-05-11 00:09:30 +08:00
let is1K = this.$dpi() == '1K'
let is2K = this.$dpi() == '2K'
2025-04-29 22:36:18 +08:00
this.option = {
grid: {
2025-05-11 00:09:30 +08:00
left: '5%',
right: '5%',
bottom: '-10%',
top: '2%',
2025-04-29 22:36:18 +08:00
containLabel: true,
},
xAxis: {
show: false,
2025-05-11 00:09:30 +08:00
type: 'value',
2025-04-29 22:36:18 +08:00
},
yAxis: [
{
2025-05-11 00:09:30 +08:00
type: 'category',
2025-04-29 22:36:18 +08:00
inverse: true,
axisLabel: {
show: true,
textStyle: {
2025-05-11 00:09:30 +08:00
color: '#cbdaff',
fontSize: is1K ? 14 : is2K ? 18 : 20,
2025-04-29 22:36:18 +08:00
},
},
splitLine: {
show: false,
},
axisTick: {
show: false,
},
axisLine: {
show: false,
},
data: text,
},
{
2025-05-11 00:09:30 +08:00
type: 'category',
2025-04-29 22:36:18 +08:00
inverse: true,
2025-05-11 00:09:30 +08:00
axisTick: 'none',
axisLine: 'none',
2025-04-29 22:36:18 +08:00
show: true,
axisLabel: {
textStyle: {
2025-05-11 00:09:30 +08:00
color: '#cbdaff',
fontSize: is1K ? 16 : is2K ? 20 : 30,
2025-04-29 22:36:18 +08:00
},
2025-05-11 00:09:30 +08:00
formatter: function (params, i) {
var text = '{a|' + value[i] + '}{a| ' + prop[i] + '%}'
return text
2025-04-29 22:36:18 +08:00
},
rich: {
a: {
2025-05-11 00:09:30 +08:00
fontSize: is1K ? 16 : is2K ? 20 : 30,
color: '#cbdaff',
2025-04-29 22:36:18 +08:00
},
},
},
data: prop,
},
],
series: [
{
2025-05-11 00:09:30 +08:00
type: 'bar',
2025-04-29 22:36:18 +08:00
zlevel: 1,
itemStyle: {
normal: {
2025-05-11 00:09:30 +08:00
color: '#6ab9fe',
2025-04-29 22:36:18 +08:00
},
},
2025-05-11 00:09:30 +08:00
barWidth: is1K ? 8 : is2K ? 12 : 16,
2025-04-29 22:36:18 +08:00
data: prop,
},
{
2025-05-11 00:09:30 +08:00
type: 'bar',
barWidth: is1K ? 8 : is2K ? 12 : 16,
barGap: '-100%',
2025-04-29 22:36:18 +08:00
data: bgd,
itemStyle: {
normal: {
2025-05-11 00:09:30 +08:00
color: 'rgba(24,31,68,1)',
2025-04-29 22:36:18 +08:00
},
},
},
],
2025-05-11 00:09:30 +08:00
}
2025-04-29 22:36:18 +08:00
2025-05-11 00:09:30 +08:00
chChart.setOption(this.option)
window.onresize = chChart.resize
2025-04-29 22:36:18 +08:00
})
},
},
2025-05-11 00:09:30 +08:00
watch: {
data: function (n, o) {
2025-04-29 22:36:18 +08:00
this.getChartData(this.data)
2025-05-11 00:09:30 +08:00
},
},
2025-04-29 22:36:18 +08:00
}
</script>