yudao-dev/src/views/OperationalOverview/components/pileBarChart.vue

272 lines
6.4 KiB
Vue
Raw Normal View History

2023-12-29 09:00:00 +08:00
<!--
* @Author: zhp
* @Date: 2023-12-27 13:54:52
2024-04-03 16:01:11 +08:00
* @LastEditTime: 2024-04-03 15:48:43
2023-12-29 09:00:00 +08:00
* @LastEditors: zhp
* @Description:
-->
<template>
<div>
2024-04-01 09:25:54 +08:00
<div :id="id" :style="{ height:'75px', width: width }" />
2023-12-29 09:00:00 +08:00
</div>
</template>
<script>
import * as echarts from 'echarts';
import 'echarts/theme/macarons' // echarts theme
import resize from './mixins/resize'
2024-03-28 16:48:42 +08:00
import { raw } from 'body-parser';
2023-12-29 09:00:00 +08:00
export default {
name: 'OverviewBar',
mixins: [resize],
props: {
id: {
type: String,
default: 'linearBarChart'
},
className: {
type: String,
default: 'chart'
},
width: {
type: String,
default: '100%'
},
borderRadius: {
type: Array,
default: () => [9, 9, 0, 0]
},
beilv: {
type: Number,
default: 1
},
height: {
type: Number,
2024-03-27 17:02:44 +08:00
default: 100
2023-12-29 09:00:00 +08:00
},
showLegend: {
type: Boolean,
default: false
},
// nameList: {
// type: Array,
// default: () => []
// },
// dataList: {
// type: Array,
// default: () => []
// }
},
data() {
return {
chart: null,
nameList: [],
series: [{
type: 'bar',
data: [],
barWidth: 6
}]
}
},
mounted() {
2024-04-01 09:25:54 +08:00
// console.log('mounted')
window.addEventListener('resize', () => {
this.resize()
})
2023-12-29 09:00:00 +08:00
// console.log('borderRadius: ', this.borderRadius)
// console.log('33333', this.dataList)
// let arr = []
// this.dataList.forEach(ele => {
// console.log(ele);
// this.series = []
2024-03-28 16:48:42 +08:00
// this.initChart()
2023-12-29 09:00:00 +08:00
// this.$nextTick(() => {
// // this.initChart()
// })
},
beforeDestroy() {
if (!this.chart) {
return
}
this.chart.dispose()
this.chart = null
},
methods: {
2024-04-01 09:25:54 +08:00
resize() {
this.chart.resize({
width: 'auto',
height: 90
});;
},
2023-12-29 09:00:00 +08:00
initChart(nameList, topNameList, nameWasteList, passRateList, wasteList) {
2024-02-21 15:27:09 +08:00
let rawData = []
2024-03-25 16:57:41 +08:00
let colors = ['#0fdedb', '#2359ec']
rawData.push(passRateList, wasteList)
2024-03-28 16:48:42 +08:00
const totalData = [];
// if (rawData.length != 0 && raw,Data,length != 0) {
2024-03-25 16:57:41 +08:00
for (let i = 0; i < rawData[0].length; ++i) {
let sum = 0;
for (let j = 0; j < rawData.length; ++j) {
sum += rawData[j][i];
}
totalData.push(sum);
2024-02-21 15:27:09 +08:00
}
2024-03-28 16:48:42 +08:00
// }
2024-04-03 16:01:11 +08:00
// rawData[1].map((d, did) =>
// console.log((d / totalData[did]).toFixed(3))
// // totalData[did] <= 0 ? 0 : d / totalData[did]
// )
2024-03-28 15:36:39 +08:00
console.log('total', totalData)
2024-02-21 15:27:09 +08:00
const series = [
'良品',
'废品',
// 'Affiliate Ad',
// 'Video Ad',
// 'Search Engine'
].map((name, sid) => {
// console.log(sid)
return {
name,
2023-12-29 16:29:15 +08:00
type: 'bar',
stack: 'total',
2024-03-28 15:36:39 +08:00
barWidth: 12,
// label: {
// show: true,
// formatter: (params) => Math.round(params.value * 1000) / 10 + '%'
// },
2024-02-21 15:27:09 +08:00
color:colors[sid],
2024-03-25 16:57:41 +08:00
data: rawData.length != 0 ? rawData[sid].map((d, did) =>
2024-02-21 15:27:09 +08:00
totalData[did] <= 0 ? 0 : d / totalData[did]
2024-03-25 16:57:41 +08:00
) : []
2024-02-21 15:27:09 +08:00
};
});
2024-03-27 17:02:44 +08:00
// this.charts.resize({
// //width: width,
// //height: height,
// // es6解构
// width,
// height
// })
2024-03-29 16:56:21 +08:00
this.chart = echarts.init(document.getElementById(this.id))
let isFinished = false //标记 isFinished
this.chart.on('finished', _ => {
if (!isFinished) {
console.log('我只执行一次')
isFinished = true
// this.isLoading = false //关闭loading
this.chart.resize() //重新渲染charts大小
}
console.log(113, 'finished')
})
2023-12-29 09:00:00 +08:00
this.chart.setOption({
2023-12-29 16:29:15 +08:00
legend: {
2024-03-28 15:36:39 +08:00
formatter: function (name) {
//通过name获取到数组对象中的单个对象
let singleData = series.filter(function (item) {
return item.name == name
})
2024-04-03 16:01:11 +08:00
return name + parseFloat((singleData[0].data * 100).toFixed(2)) + '%'
2024-03-28 15:36:39 +08:00
},
2024-03-13 14:54:35 +08:00
itemWidth: 12,
itemHeight: 12,
2024-03-27 17:02:44 +08:00
bottom: '20',
left: '20',
2024-04-03 16:01:11 +08:00
icon: 'roundRect',
2023-12-29 16:29:15 +08:00
textStyle: {
2024-04-03 16:01:11 +08:00
color: 'rgba(255,255,255,.9)',
2024-03-29 16:56:21 +08:00
fontSize:12,
2023-12-29 09:00:00 +08:00
}
},
grid: {
2024-03-27 17:02:44 +08:00
top:'0',
2023-12-29 09:00:00 +08:00
left: '3%',
right: '4%',
2024-01-17 15:30:47 +08:00
// bottom: '3%',
2023-12-29 09:00:00 +08:00
width: 'auto',
2024-03-27 17:02:44 +08:00
height: '95',
2023-12-29 09:00:00 +08:00
containLabel: true
},
yAxis: [
{
2023-12-29 16:29:15 +08:00
type: 'category',
2023-12-29 09:00:00 +08:00
inverse: true,
2023-12-29 16:29:15 +08:00
splitLine: {
show: false
},
axisTick: {
show: false
},
axisLine: {
show: false
},
2023-12-29 09:00:00 +08:00
axisLabel: {
show: true,
inside: true,
2023-12-29 16:29:15 +08:00
interval: 0, //横轴信息全部显
splitNumber: 50,
// boundaryGap: [20, 20],
2023-12-29 09:00:00 +08:00
textStyle: {
2024-04-03 16:01:11 +08:00
color: 'rgba(255,255,255,.9)',
2023-12-29 16:29:15 +08:00
verticalAlign: 'bottom',
2024-03-29 16:56:21 +08:00
fontSize: 16,
2023-12-29 09:00:00 +08:00
align: 'left',
2024-03-27 17:02:44 +08:00
padding: [0, 0, 10, -5]
2023-12-29 09:00:00 +08:00
}
},
2023-12-29 16:29:15 +08:00
data: topNameList
},
{
type: 'category',
inverse: true,
2023-12-29 09:00:00 +08:00
splitLine: {
show: false
},
axisTick: {
show: false
},
axisLine: {
show: false
2023-12-29 16:29:15 +08:00
},
2023-12-29 09:00:00 +08:00
axisLabel: {
2023-12-29 16:29:15 +08:00
show: true,
2023-12-29 09:00:00 +08:00
inside: true,
2023-12-29 16:29:15 +08:00
interval: 0, //横轴信息全部显
splitNumber: 50,
// boundaryGap: [20, 20],
2023-12-29 09:00:00 +08:00
textStyle: {
2024-04-03 16:01:11 +08:00
color: 'rgba(255,255,255,.9)',
2023-12-29 16:29:15 +08:00
verticalAlign: 'bottom',
2024-03-29 16:56:21 +08:00
fontSize: 16,
2023-12-29 09:00:00 +08:00
align: 'right',
2024-03-27 17:02:44 +08:00
padding: [0, 0, 10, -5]
2023-12-29 09:00:00 +08:00
}
},
2023-12-29 16:29:15 +08:00
data: nameWasteList
2023-12-29 09:00:00 +08:00
}
],
2023-12-29 16:29:15 +08:00
xAxis: {
// max: 120,
show: false,
},
series:series
2023-12-29 09:00:00 +08:00
})
2024-04-01 09:25:54 +08:00
this.$nextTick(() => {
setTimeout(() => {
this.resize()
},1000);
})
// this.chart.resize({
// width: 'auto',
// height: 90
// });;
2023-12-29 09:00:00 +08:00
}
}
}
</script>
2024-01-17 15:30:47 +08:00
<style scoped lang="scss">
</style>