yudao-init/src/views/report/components/bmLineBar.vue

214 lines
5.1 KiB
Vue
Raw Normal View History

2024-06-12 13:27:09 +08:00
<template>
<div>
2024-06-13 17:12:50 +08:00
<!-- 暂无数据 -->
<div
class="no-data-bg"
style="position: relative; left: 50%; transform: translateX(-50%)"
v-show="this.chartMsg.series.length === 0"
></div>
2024-06-12 13:27:09 +08:00
<!-- 图例 -->
2024-06-13 17:12:50 +08:00
<div v-show="this.chartMsg.series.length > 0">
<div class="legend">
<span class="item" v-for="item in legendList" :key="item.id">
2024-06-12 13:27:09 +08:00
<span
2024-06-13 17:12:50 +08:00
v-if="item.type === 1"
class="block"
2024-06-12 13:27:09 +08:00
:style="{ backgroundColor: item.color }"
></span>
2024-06-13 17:12:50 +08:00
<span
v-if="item.type === 2"
class="line"
:style="{ backgroundColor: item.color }"
>
<span
class="line-block"
:style="{ backgroundColor: item.color }"
></span>
</span>
{{ item.name }}</span
>
</div>
<div
:id="chartId"
:style="{ width: '100%', height: chartHeight + 'px' }"
></div>
2024-06-12 13:27:09 +08:00
</div>
</div>
</template>
<script>
import * as echarts from "echarts";
import { debounce } from "@/utils/debounce";
export default {
name: "bmLineBar",
data() {
return {
myChart: "",
option: {
color: [],
// color: ["#8EF0AB", "#63BDFF", "#288AFF"],
grid: {
left: 100,
right: 10,
bottom: 30,
top: 30,
},
tooltip: {
trigger: "axis",
axisPointer: {
2024-06-13 15:36:02 +08:00
// type: "cross",
2024-06-12 13:27:09 +08:00
crossStyle: {
color: "rgba(237,237,237,0.5)",
},
},
formatter: function (params) {
2024-06-13 17:12:50 +08:00
var res = `<span style='color:rgba(0,0,0,0.8)'>${params[0].axisValueLabel}</span>`;
2024-06-12 13:27:09 +08:00
for (var i = 0, l = params.length; i < l; i++) {
res +=
"<br/>" +
`<span style='display:inline-block;margin-right:4px;border-radius:10px;width:10px;height:10px;background-color:${params[i].color}'></span>` +
`<span style='display:inline-block;width:150px;color:rgba(0,0,0,0.8);font-size:14px;'>${params[i].seriesName}</span>` +
2024-06-13 17:12:50 +08:00
`<span style='color:rgba(0,0,0,0.48);font-size:14px;'>${
params[i].name === "%"
? (params[i].value ? params[i].value.toFixed(2) : 0.0) +
params[i].name
: params[i].value
}</span>`;
2024-06-12 13:27:09 +08:00
}
return res;
},
},
xAxis: {
type: "category",
data: [],
axisTick: {
show: false,
},
axisPointer: {
type: "shadow",
},
},
yAxis: {
type: "value",
2024-06-13 15:36:02 +08:00
name: "",
nameTextStyle: {
fontSize: 12,
align: "right",
},
axisLabel: {},
2024-06-12 13:27:09 +08:00
},
series: [],
},
};
},
props: {
chartHeight: {
type: Number,
default: 300,
},
legendList: {
type: Array,
default: () => [],
},
chartMsg: {
type: Object,
default: () => {},
},
2024-06-13 15:36:02 +08:00
chartId: {
type: String,
default: "bmChart",
},
chartNum: {
type: Number,
default: 1,
},
2024-06-12 13:27:09 +08:00
},
watch: {
chartHeight: {
handler(newVal) {
this.chartHeight = newVal;
},
},
2024-06-13 15:36:02 +08:00
chartNum(val) {
2024-06-12 13:27:09 +08:00
this.canvasReset();
},
chartMsg: {
handler(newVal) {
2024-06-13 17:12:50 +08:00
this.canvasReset();
2024-06-12 13:27:09 +08:00
},
deep: true,
},
},
mounted() {
this.canvasReset();
},
methods: {
canvasReset() {
debounce(() => {
this.getMes();
}, 500)();
},
getMes() {
if (this.myChart) {
this.myChart.dispose();
}
2024-06-13 15:36:02 +08:00
var chartDom = document.getElementById(this.chartId);
2024-06-12 13:27:09 +08:00
this.myChart = echarts.init(chartDom);
this.option.color = this.chartMsg.color;
this.option.xAxis.data = this.chartMsg.xData;
2024-06-13 15:36:02 +08:00
this.option.yAxis.name = this.chartMsg.yName;
2024-06-13 17:12:50 +08:00
if (
this.chartMsg.series.length > 0 &&
this.chartMsg.series[0].data[0].name === "%"
) {
this.option.yAxis.axisLabel = {
formatter: function (value) {
return value + ".00%";
},
};
} else {
this.option.yAxis.axisLabel = {
formatter: function (value) {
return value;
},
};
}
// this.option.yAxis.axisLabel = this.chartMsg.yAxisLabel;
2024-06-12 13:27:09 +08:00
this.option.series = this.chartMsg.series;
this.myChart.setOption(this.option);
},
},
};
</script>
<style lang="scss" scoped>
.legend {
text-align: right;
.item {
display: inline-block;
margin-right: 10px;
font-size: 14px;
color: #8c8c8c;
.block {
width: 10px;
height: 10px;
display: inline-block;
margin-right: 4px;
}
.line {
width: 10px;
height: 10px;
border-radius: 5px;
display: inline-block;
margin-right: 4px;
position: relative;
.line-block {
position: absolute;
width: 20px;
height: 2px;
left: -5px;
top: 4px;
}
}
}
}
</style>