YZProjectCloud/yanzhu-bigscreen/src/components/staffSurveyChart.vue

234 lines
8.9 KiB
Vue
Raw Normal View History

2024-12-02 00:05:10 +08:00
<template>
2025-03-22 19:04:33 +08:00
<div :style="{ 'height': height, 'width': width }" ref="chart"></div>
2024-12-02 00:05:10 +08:00
</template>
<script>
export default {
props: {
2025-03-22 19:04:33 +08:00
height: {
type: [Number, String]
2024-12-02 00:05:10 +08:00
},
2025-03-22 19:04:33 +08:00
data: {
type: Array
2024-12-02 00:05:10 +08:00
},
2025-03-22 19:04:33 +08:00
width: {
type: [Number, String]
2024-12-02 00:05:10 +08:00
},
2025-03-22 19:04:33 +08:00
max: {
type: Number
2024-12-02 00:05:10 +08:00
},
unit: {
2025-03-22 19:04:33 +08:00
type: String
2024-12-02 00:05:10 +08:00
}
},
data() {
return {
2025-03-22 19:04:33 +08:00
chart: null,
2024-12-02 00:05:10 +08:00
}
},
2025-03-22 19:04:33 +08:00
mounted() {
2024-12-02 00:05:10 +08:00
this.init()
2025-03-22 19:04:33 +08:00
},
2024-12-02 00:05:10 +08:00
methods: {
2025-03-22 19:04:33 +08:00
init() {
2024-12-02 00:05:10 +08:00
this.getChartData()
},
2025-03-22 19:04:33 +08:00
chartClick(p) {
this.$emit("chart-click", p);
2024-12-02 00:05:10 +08:00
},
2025-03-22 19:04:33 +08:00
getChartData() {
2024-12-02 00:05:10 +08:00
//品类金额占比 饼图
var chChart = echarts.init(this.$refs.chart);
2025-03-22 19:04:33 +08:00
this.chart = chChart;
chChart.on("click", this.chartClick)
this.echart(chChart, this.data)
2024-12-02 00:05:10 +08:00
},
2025-03-22 19:04:33 +08:00
echart(chChart, chartData) {
let fontSize = this.$dpi() == '1K' ? '12' : this.$dpi() == '2K' ? '14' : '24';
2024-12-02 00:05:10 +08:00
let newPromise = new Promise((resolve) => {
resolve()
})
//然后异步执行echarts的初始化函数
newPromise.then(() => {
var value = []
var text = []
var bgData = []
var total = 0
for (let i = 0; i < chartData.length; i++) {
value.push(chartData[i].value)
text.push(chartData[i].text)
bgData.push(100)
2025-03-22 19:04:33 +08:00
if (this.max) {
2024-12-02 00:05:10 +08:00
total = this.max
2025-03-22 19:04:33 +08:00
} else {
2024-12-02 00:05:10 +08:00
total += chartData[i].value
}
}
var prop = []
for (let j = 0; j < value.length; j++) {
2025-03-22 19:04:33 +08:00
if (total == 0) {
2024-12-02 00:05:10 +08:00
prop.push(0)
2025-03-22 19:04:33 +08:00
} else {
prop.push((value[j] / total * 100).toFixed(1))
2024-12-02 00:05:10 +08:00
}
}
2025-03-22 19:04:33 +08:00
2024-12-02 00:05:10 +08:00
var unit = '人'
2025-03-22 19:04:33 +08:00
if (this.unit) {
2024-12-02 00:05:10 +08:00
unit = this.unit
}
this.option = {
grid: {
left: "5%",
right: "5%",
bottom: "0%",
top: "0%",
containLabel: true,
},
xAxis: [{
show: false,
},
2025-03-22 19:04:33 +08:00
//由于下边X轴已经是百分比刻度了,所以需要在顶部加一个X轴,刻度是金额,也隐藏掉
{
show: false,
}
2024-12-02 00:05:10 +08:00
],
2025-03-22 19:04:33 +08:00
yAxis: [
2024-12-02 00:05:10 +08:00
{
2025-03-22 19:04:33 +08:00
triggerEvent: true,
2024-12-02 00:05:10 +08:00
type: 'category',
axisLabel: {
show: false, //让Y轴数据不显示
},
itemStyle: {
},
axisTick: {
show: false, //隐藏Y轴刻度
},
axisLine: {
show: false, //隐藏Y轴线段
},
data: [],
2025-03-22 19:04:33 +08:00
}, {
triggerEvent: true,
2024-12-02 00:05:10 +08:00
show: false,
data: [],
axisLine: {
show: false
}
}],
series: [
//数据条--------------------我是分割线君------------------------------//
{
show: true,
type: 'bar',
xAxisIndex: 1, //代表使用第二个X轴刻度!!!!!!!!!!!!!!!!!!!!!!!!
barGap: '-100%',
2025-03-22 19:04:33 +08:00
barWidth: this.$dpi() == '1K' ? '6' : this.$dpi() == '2K' ? '8' : '16', //统计条宽度
2024-12-02 00:05:10 +08:00
itemStyle: {
normal: {
color: 'rgba(22,203,115,0.05)'
},
},
label: {
normal: {
show: true,
//label 的position位置可以是top bottom left,right,也可以是固定值
//在这里需要上下统一对齐,所以用固定值
2025-03-22 19:04:33 +08:00
position: [0, this.$dpi() == '1K' ? '-25' : this.$dpi() == '2K' ? '-40' : '-50'],
2024-12-02 00:05:10 +08:00
rich: { //富文本
prop: { //自定义颜色
color: '#c6d9fa',
2025-03-22 19:04:33 +08:00
fontSize: fontSize,
2024-12-02 00:05:10 +08:00
},
2025-03-22 19:04:33 +08:00
unit: {
2024-12-02 00:05:10 +08:00
color: '#6c829a',
2025-03-22 19:04:33 +08:00
fontSize: fontSize,
2024-12-02 00:05:10 +08:00
},
2025-03-22 19:04:33 +08:00
yes: {
2024-12-02 00:05:10 +08:00
color: '#55adf7',
2025-03-22 19:04:33 +08:00
fontSize: fontSize,
2024-12-02 00:05:10 +08:00
},
2025-03-22 19:04:33 +08:00
not: {
2024-12-02 00:05:10 +08:00
color: '#4677fa',
2025-03-22 19:04:33 +08:00
fontSize: fontSize,
2024-12-02 00:05:10 +08:00
},
2025-03-22 19:04:33 +08:00
index: {
2024-12-02 00:05:10 +08:00
color: '#fcbc02',
fontStyle: 'italic',
2025-03-22 19:04:33 +08:00
padding: [0, 0, 0, 5],
fontSize: fontSize,
2024-12-02 00:05:10 +08:00
},
name: {
width: this.width,
color: '#c6d9fa',
2025-03-22 19:04:33 +08:00
padding: [0, 0, 0, 10],
fontSize: fontSize,
2024-12-02 00:05:10 +08:00
},
2025-03-22 19:04:33 +08:00
color: {
2024-12-02 00:05:10 +08:00
color: '#8ca2be',
2025-03-22 19:04:33 +08:00
fontSize: fontSize,
2024-12-02 00:05:10 +08:00
},
2025-03-22 19:04:33 +08:00
arrow: {
width: this.$dpi() == '1K' ? 12 : this.$dpi() == '2K' ? 16 : 24,
height: this.$dpi() == '1K' ? 8 : this.$dpi() == '2K' ? 12 : 20,
2024-12-02 00:05:10 +08:00
backgroundColor: {
image: "/cdn/images/WEB_2B7C06210CD44D55BFEE6205A35DE4A7.png",
},
},
},
2025-03-22 19:04:33 +08:00
formatter: (data) => {
2024-12-02 00:05:10 +08:00
//富文本固定格式{colorName|这里填你想要写的内容}
//return '{arrow|}'
2025-03-22 19:04:33 +08:00
return '{arrow|}{name|' + text[data.dataIndex] + '}{prop|' + value[data.dataIndex] + '}{color| ' + unit + '} {prop|' + prop[data.dataIndex] + '}{color| %} ';
2024-12-02 00:05:10 +08:00
},
}
},
data: bgData,
},
{
type: "bar",
zlevel: 1,
itemStyle: {
normal: {
color: "#6ab9fe",
},
},
barWidth: 8,
data: prop,
},
{
type: "bar",
barWidth: 8,
barGap: "-100%",
data: bgData,
itemStyle: {
normal: {
color: "rgba(24,31,68,1)",
},
},
},
],
};
chChart.setOption(this.option);
window.onresize = chChart.resize;
})
},
},
2025-03-22 19:04:33 +08:00
watch: {
data: function (n, o) {
2024-12-02 00:05:10 +08:00
this.getChartData()
}
2025-03-22 19:04:33 +08:00
}
2024-12-02 00:05:10 +08:00
}
</script>
2025-03-22 19:04:33 +08:00
<style></style>