2024-05-07 13:51:11 +08:00
|
|
|
<!--
|
|
|
|
* @Author: zhp
|
|
|
|
* @Date: 2024-05-07 13:22:43
|
2024-05-14 08:46:58 +08:00
|
|
|
* @LastEditTime: 2024-05-14 08:32:44
|
2024-05-07 13:51:11 +08:00
|
|
|
* @LastEditors: zhp
|
|
|
|
* @Description:
|
2024-04-24 16:31:27 +08:00
|
|
|
-->
|
|
|
|
<template>
|
|
|
|
<chart-container class="bar-chart-base">
|
|
|
|
<div class="legend">
|
2024-05-13 16:20:37 +08:00
|
|
|
<span v-for="item in legend" :key="item.label" class="legend-item"
|
|
|
|
:style="{ fontSize: isFullscreen ? '0.58vw' : '0.54vw' }">{{ item.label }}</span>
|
2024-04-24 16:31:27 +08:00
|
|
|
</div>
|
2024-05-13 16:20:37 +08:00
|
|
|
<div ref="chart" :style="{ height: '100%',width: width}"></div>
|
2024-04-24 16:31:27 +08:00
|
|
|
</chart-container>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import screenfull from "screenfull";
|
2024-04-28 14:54:54 +08:00
|
|
|
import ChartContainer from "../../../../components/ChartContainer.vue";
|
2024-05-13 16:20:37 +08:00
|
|
|
// import chartMixin from "@/mixins/chart.js";
|
|
|
|
import { debounce } from "@/utils/debounce";
|
|
|
|
import * as echarts from "echarts";
|
2024-04-24 16:31:27 +08:00
|
|
|
export default {
|
|
|
|
name: "BarChartBase",
|
|
|
|
components: {
|
2024-04-28 14:54:54 +08:00
|
|
|
ChartContainer,
|
2024-04-24 16:31:27 +08:00
|
|
|
},
|
2024-05-13 16:20:37 +08:00
|
|
|
// mixins: [chartMixin],
|
2024-04-24 16:31:27 +08:00
|
|
|
props: {
|
|
|
|
vHeight: {
|
|
|
|
type: Number,
|
2024-05-10 11:10:42 +08:00
|
|
|
default: 36,
|
2024-04-24 16:31:27 +08:00
|
|
|
},
|
|
|
|
legend: {
|
|
|
|
type: Array,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
xAxis: {
|
|
|
|
type: Array,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
series: {
|
|
|
|
type: Array,
|
|
|
|
required: true,
|
|
|
|
},
|
2024-04-26 15:44:04 +08:00
|
|
|
in: {
|
|
|
|
type: String,
|
2024-04-26 17:05:26 +08:00
|
|
|
default: "",
|
|
|
|
},
|
2024-04-24 16:31:27 +08:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
2024-05-14 08:46:58 +08:00
|
|
|
width: '100%',
|
2024-04-24 16:31:27 +08:00
|
|
|
isFullscreen: false,
|
|
|
|
actualOptions: null,
|
|
|
|
options: {
|
|
|
|
grid: {
|
|
|
|
left: "5%",
|
2024-05-13 16:20:37 +08:00
|
|
|
right: "0%",
|
2024-04-24 16:31:27 +08:00
|
|
|
bottom: "3%",
|
|
|
|
top: "15%",
|
|
|
|
containLabel: true,
|
|
|
|
},
|
|
|
|
tooltip: {},
|
|
|
|
xAxis: {
|
|
|
|
axisTick: {
|
|
|
|
show: false,
|
|
|
|
},
|
|
|
|
axisLine: {
|
|
|
|
lineStyle: {
|
|
|
|
color: "#4561AE",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
axisLabel: {
|
|
|
|
color: "#fff",
|
|
|
|
fontSize: 12,
|
|
|
|
},
|
|
|
|
data: this.xAxis,
|
|
|
|
},
|
|
|
|
yAxis: {
|
2024-05-07 13:51:11 +08:00
|
|
|
name: "单位/%",
|
2024-04-24 16:31:27 +08:00
|
|
|
nameTextStyle: {
|
|
|
|
color: "#fff",
|
|
|
|
fontSize: 12,
|
|
|
|
},
|
|
|
|
axisTick: {
|
|
|
|
show: false,
|
|
|
|
},
|
|
|
|
axisLabel: {
|
|
|
|
color: "#fff",
|
|
|
|
fontSize: 12,
|
|
|
|
},
|
|
|
|
axisLine: {
|
|
|
|
show: true,
|
|
|
|
lineStyle: {
|
|
|
|
color: "#4561AE",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
splitLine: {
|
|
|
|
lineStyle: {
|
|
|
|
color: "#4561AE",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
series: [
|
|
|
|
{
|
|
|
|
name: "", // this.series[0].name,
|
|
|
|
type: "bar",
|
|
|
|
barWidth: 12,
|
|
|
|
itemStyle: {
|
|
|
|
borderRadius: [10, 10, 0, 0],
|
|
|
|
color: {
|
|
|
|
type: "linear",
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
x2: 0,
|
|
|
|
y2: 1,
|
|
|
|
colorStops: [
|
|
|
|
{
|
|
|
|
offset: 0,
|
|
|
|
color: "#12f7f1", // 0% 处的颜色
|
|
|
|
},
|
|
|
|
{
|
|
|
|
offset: 0.35,
|
|
|
|
color: "#12f7f177", // 100% 处的颜色
|
|
|
|
},
|
|
|
|
{
|
|
|
|
offset: 0.75,
|
|
|
|
color: "#12f7f133", // 100% 处的颜色
|
|
|
|
},
|
|
|
|
{
|
|
|
|
offset: 1,
|
|
|
|
color: "transparent", // 100% 处的颜色
|
|
|
|
},
|
|
|
|
],
|
|
|
|
global: false, // 缺省为 false
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data: [], // this.series[0].data,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "", // this.series[1].name,
|
|
|
|
type: "bar",
|
|
|
|
barWidth: 12,
|
|
|
|
// tooltip: {
|
|
|
|
// valueFormatter: function (value) {
|
|
|
|
// return value + " ml";
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
itemStyle: {
|
|
|
|
borderRadius: [10, 10, 0, 0],
|
|
|
|
color: {
|
|
|
|
type: "linear",
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
x2: 0,
|
|
|
|
y2: 1,
|
|
|
|
colorStops: [
|
|
|
|
{
|
|
|
|
offset: 0,
|
|
|
|
color: "#57abf8", // 0% 处的颜色
|
|
|
|
},
|
|
|
|
{
|
|
|
|
offset: 1,
|
|
|
|
color: "#364BFE66", // 100% 处的颜色
|
|
|
|
},
|
|
|
|
],
|
|
|
|
global: false, // 缺省为 false
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data: [], // this.series[1].data,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2024-05-13 16:20:37 +08:00
|
|
|
computed: {
|
|
|
|
isOpen() {
|
|
|
|
return this.$store.getters.sidebar.opened
|
|
|
|
},
|
|
|
|
},
|
2024-04-24 16:31:27 +08:00
|
|
|
watch: {
|
2024-05-13 16:20:37 +08:00
|
|
|
isOpen(val) {
|
|
|
|
if (val === true) {
|
|
|
|
this.width = '100%-128px'
|
|
|
|
this.canvasReset()
|
|
|
|
} else {
|
|
|
|
this.watch = 100 + '%'
|
|
|
|
this.canvasReset()
|
|
|
|
}
|
|
|
|
},
|
2024-04-24 16:31:27 +08:00
|
|
|
/** 全屏状态切换时,对柱子粗细和字体大小进行相应调整 */
|
|
|
|
isFullscreen(val) {
|
|
|
|
this.actualOptions.series.map((item) => {
|
|
|
|
item.barWidth = val ? 18 : 12;
|
|
|
|
});
|
|
|
|
this.actualOptions.xAxis.axisLabel.fontSize = val ? 18 : 12;
|
|
|
|
this.actualOptions.yAxis.axisLabel.fontSize = val ? 18 : 12;
|
|
|
|
this.actualOptions.yAxis.nameTextStyle.fontSize = val ? 18 : 12;
|
2024-05-13 16:20:37 +08:00
|
|
|
this.initChart(this.actualOptions);
|
2024-04-24 16:31:27 +08:00
|
|
|
},
|
|
|
|
series(val) {
|
|
|
|
if (!val) {
|
2024-05-13 16:20:37 +08:00
|
|
|
this.initChart(this.options);
|
2024-04-24 16:31:27 +08:00
|
|
|
return;
|
|
|
|
}
|
2024-05-08 16:38:05 +08:00
|
|
|
console.log(val)
|
|
|
|
const actualOptions = JSON.parse(JSON.stringify(this.options))
|
2024-04-24 16:31:27 +08:00
|
|
|
actualOptions.series[0].data = val[0].data;
|
|
|
|
actualOptions.series[0].name = val[0].name;
|
2024-04-26 11:23:40 +08:00
|
|
|
actualOptions.series[1].data = val?.[1]?.data || [];
|
2024-04-26 15:44:04 +08:00
|
|
|
actualOptions.series[1].name = val?.[1]?.name || "";
|
2024-04-24 16:31:27 +08:00
|
|
|
this.actualOptions = actualOptions;
|
2024-05-13 16:20:37 +08:00
|
|
|
this.initChart(actualOptions);
|
2024-04-24 16:31:27 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
mounted() {
|
2024-05-13 16:20:37 +08:00
|
|
|
// if (screenfull.isEnabled) {
|
|
|
|
// screenfull.on("change", () => {
|
|
|
|
// this.isFullscreen = screenfull.isFullscreen;
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
this.actualOptions = this.options
|
|
|
|
this.canvasReset();
|
|
|
|
window.addEventListener("resize", this.canvasReset);
|
|
|
|
},
|
|
|
|
beforeDestroy() {
|
|
|
|
if (this.chart) {
|
|
|
|
this.chart.dispose();
|
2024-04-24 16:31:27 +08:00
|
|
|
}
|
|
|
|
},
|
2024-05-13 16:20:37 +08:00
|
|
|
destroyed() {
|
|
|
|
window.removeEventListener("resize", this.canvasReset);
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
canvasReset() {
|
|
|
|
debounce(() => {
|
|
|
|
this.initChart();
|
|
|
|
}, 500)();
|
|
|
|
},
|
|
|
|
initChart() {
|
|
|
|
if (this.chart) {
|
|
|
|
this.chart.dispose();
|
|
|
|
}
|
|
|
|
this.chart = echarts.init(this.$refs.chart);
|
|
|
|
this.chart.setOption(this.actualOptions);
|
|
|
|
},
|
|
|
|
},
|
2024-04-24 16:31:27 +08:00
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style scoped lang="scss">
|
|
|
|
.bar-chart-base {
|
|
|
|
// position: relative;
|
|
|
|
|
|
|
|
.legend {
|
|
|
|
position: absolute;
|
|
|
|
top: 5.2vh;
|
|
|
|
right: 1vw;
|
|
|
|
}
|
|
|
|
|
|
|
|
.legend-item {
|
|
|
|
position: relative;
|
|
|
|
// font-size: 12px;
|
|
|
|
margin-right: 0.67vw;
|
|
|
|
|
|
|
|
&::before {
|
|
|
|
content: "";
|
|
|
|
display: inline-block;
|
|
|
|
width: 0.531vw;
|
|
|
|
height: 0.531vw;
|
|
|
|
background-color: #ccc;
|
|
|
|
border-radius: 2px;
|
|
|
|
margin-right: 0.22vw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.legend-item:nth-child(1):before {
|
|
|
|
background-color: #12f7f1;
|
|
|
|
}
|
|
|
|
|
|
|
|
.legend-item:nth-child(2):before {
|
|
|
|
background-color: #58adfa;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|