yudao-init/src/views/copilot/components/LineChartBase.vue

522 lines
13 KiB
Vue
Raw Normal View History

2024-05-11 16:40:29 +08:00
<!--
2024-04-30 09:35:44 +08:00
filename: LineChartBase.vue
author: liubin
date: 2024-04-30 08:59:28
2024-05-11 16:40:29 +08:00
description:
2024-04-30 09:35:44 +08:00
-->
<template>
<chart-container class="line-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-30 09:35:44 +08:00
</div>
2024-05-17 10:31:47 +08:00
<div ref="elsChart" :style="{ height: '95%',width: width}"></div>
2024-04-30 09:35:44 +08:00
</chart-container>
</template>
<script>
import screenfull from "screenfull";
2024-05-13 16:20:37 +08:00
import { debounce } from "@/utils/debounce";
2024-04-30 09:35:44 +08:00
import ChartContainer from "./ChartContainer.vue";
2024-05-13 16:20:37 +08:00
// import chartMixin from "@/mixins/chart.js";
2024-05-11 16:40:29 +08:00
import * as echarts from "echarts";
2024-04-30 09:35:44 +08:00
export default {
name: "LineChartBase",
components: {
ChartContainer,
},
2024-05-13 16:20:37 +08:00
// mixins: [chartMixin],
2024-04-30 09:35:44 +08:00
props: {
vHeight: {
type: Number,
2024-05-11 16:40:29 +08:00
default: 38,
2024-04-30 09:35:44 +08:00
},
legend: {
type: Array,
required: true,
},
2024-05-17 10:31:47 +08:00
// xAxis: {
// type: Array,
// required: true,
// },
2024-04-30 09:35:44 +08:00
series: {
2024-05-17 10:31:47 +08:00
type: Object,
2024-04-30 09:35:44 +08:00
required: true,
},
in: {
type: String,
default: "",
},
},
data() {
return {
2024-05-14 08:46:58 +08:00
width: '100%',
2024-04-30 09:35:44 +08:00
isFullscreen: false,
actualOptions: null,
options: {
grid: {
left: "5%",
right: "4%",
bottom: "3%",
top: "15%",
containLabel: true,
},
2024-05-11 16:40:29 +08:00
// title: {
// text: 'Stacked Area Chart'
// },
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
label: {
backgroundColor: '#6a7985'
}
}
},
// legend: {
// data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
// },
2024-05-13 16:20:37 +08:00
// toolbox: {
// feature: {
// saveAsImage: {}
// }
// },
2024-05-11 16:40:29 +08:00
grid: {
left: '3%',
2024-05-17 10:31:47 +08:00
right: '2%',
2024-05-11 16:40:29 +08:00
bottom: '3%',
containLabel: true
},
2024-05-17 10:31:47 +08:00
xAxis:{
2024-05-11 16:40:29 +08:00
type: 'category',
boundaryGap: false,
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
2024-05-17 10:31:47 +08:00
},
yAxis: {
2024-05-11 16:40:29 +08:00
min: function () { // 取最小值向下取整为最小刻度
return 0
},
max: function (value) { // 取最大值向上取整为最大刻度
},
scale: true,
type: 'value',
name: 'kw/h',
// max: 100,//最大值
// min: 0,//最小值
// interval: 20,//间隔
nameTextStyle: {// y轴上方单位的颜色
color: 'rgba(255,255,255,0.5)', // 坐标值得具体的颜色
align: "right",
padding: [0, 0, 0, 2]
},
// position: 'left',
alignTicks: true,
axisLine: {
show: true,
lineStyle: {
// type: 'solid',
color: '#25528f',
// width: '1' // 坐标线的宽度
}
},
axisLabel: {
color: 'rgba(255,255,255,0.5)', // 坐标值得具体的颜色
fontSize: 12,
// formatter: '{value}'
formatter: '{value}'
// }
},
splitLine: {
lineStyle: {
color: '#25528f'
}
}
// type: 'value'
// axisLine: {
// show: true,
// lineStyle: {
// color: colors[0]
// }
// },
2024-04-30 09:35:44 +08:00
},
2024-05-11 16:40:29 +08:00
series: [
{
name: '瑞昌',
type: 'line',
stack: 'Total',
areaStyle: {
opacity: 0.8,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: '#8167F6'
},
{
offset: 1,
color: 'rgba(255, 234, 153, 0)'
}
])
},
itemStyle: {
normal: {
color: '#8167F6', //改变折线点的颜色
lineStyle: {
color: '#8167F6' //改变折线颜色
}
}
},
// emphasis: {
// focus: 'series'
// },
data: [120, 132, 101, 134, 90, 230, 210]
2024-04-30 09:35:44 +08:00
},
2024-05-11 16:40:29 +08:00
{
name: '邯郸',
type: 'line',
stack: 'Total',
areaStyle: {
opacity: 0.8,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: '#2760FF'
},
{
offset: 1,
color: 'rgba(255, 234, 153, 0)'
}
])
},
itemStyle: {
normal: {
color: '#2760FF', //改变折线点的颜色
lineStyle: {
color: '#2760FF' //改变折线颜色
}
}
},
// emphasis: {
// focus: 'series'
// },
data: [120, 132, 101, 134, 90, 230, 210]
2024-04-30 09:35:44 +08:00
},
2024-05-11 16:40:29 +08:00
{
name: '株洲',
type: 'line',
stack: 'Total',
areaStyle: {
opacity: 0.8,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: '#5996F7'
},
{
offset: 1,
color: 'rgba(255, 234, 153, 0)'
}
])
},
itemStyle: {
normal: {
color: '#5996F7', //改变折线点的颜色
lineStyle: {
color: '#5996F7' //改变折线颜色
}
}
2024-04-30 09:35:44 +08:00
},
2024-05-11 16:40:29 +08:00
// emphasis: {
// focus: 'series'
// },
data: [120, 132, 101, 134, 90, 230, 210]
2024-04-30 09:35:44 +08:00
},
2024-05-11 16:40:29 +08:00
{
name: '佳木斯',
type: 'line',
stack: 'Total',
areaStyle: {
opacity: 0.8,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: '#8BC566'
},
{
offset: 1,
color: 'rgba(255, 234, 153, 0)'
}
])
2024-04-30 09:35:44 +08:00
},
2024-05-11 16:40:29 +08:00
itemStyle: {
normal: {
color: '#8BC566', //改变折线点的颜色
lineStyle: {
color: '#8BC566' //改变折线颜色
}
}
},
// emphasis: {
// focus: 'series'
// },
data: [120, 132, 101, 134, 90, 230, 210]
2024-04-30 09:35:44 +08:00
},
{
2024-05-11 16:40:29 +08:00
name: '成都',
type: 'line',
stack: 'Total',
areaStyle: {
opacity: 0.8,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: '#11FAF0'
},
{
offset: 1,
color: 'rgba(255, 234, 153, 0)'
}
])
},
2024-04-30 09:35:44 +08:00
itemStyle: {
2024-05-11 16:40:29 +08:00
normal: {
color: '#11FAF0', //改变折线点的颜色
lineStyle: {
color: '#11FAF0' //改变折线颜色
}
}
2024-04-30 09:35:44 +08:00
},
2024-05-11 16:40:29 +08:00
// emphasis: {
// focus: 'series'
// },
data: [120, 132, 101, 134, 90, 230, 210]
2024-04-30 09:35:44 +08:00
},
{
2024-05-11 16:40:29 +08:00
name: '凯盛',
type: 'line',
stack: 'Total',
areaStyle: {
opacity: 0.8,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: '#F3C000'
},
{
offset: 1,
color: 'rgba(255, 234, 153, 0)'
}
])
},
itemStyle: {
normal: {
color: '#F3C000', //改变折线点的颜色
lineStyle: {
color: '#F3C000' //改变折线颜色
}
}
},
// emphasis: {
// focus: 'series'
2024-04-30 09:35:44 +08:00
// },
2024-05-11 16:40:29 +08:00
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: '蚌埠',
type: 'line',
stack: 'Total',
areaStyle: {
opacity: 0.8,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: '#F38600'
},
{
offset: 1,
color: 'rgba(255, 234, 153, 0)'
}
])
},
2024-04-30 09:35:44 +08:00
itemStyle: {
2024-05-11 16:40:29 +08:00
normal: {
color: '#F38600', //改变折线点的颜色
lineStyle: {
color: '#F38600' //改变折线颜色
}
}
2024-04-30 09:35:44 +08:00
},
2024-05-11 16:40:29 +08:00
// emphasis: {
// focus: 'series'
// },
data: [120, 132, 101, 134, 90, 230, 210]
2024-04-30 09:35:44 +08:00
},
2024-05-11 16:40:29 +08:00
]
2024-04-30 09:35:44 +08:00
},
};
},
2024-05-13 16:20:37 +08:00
computed: {
isOpen() {
return this.$store.getters.sidebar.opened
},
},
2024-04-30 09:35:44 +08:00
watch: {
2024-05-13 16:20:37 +08:00
isOpen(val) {
if (val === true) {
2024-05-17 10:31:47 +08:00
this.width = 97 + '%'
2024-05-13 16:20:37 +08:00
this.canvasReset()
2024-05-14 08:46:58 +08:00
console.log(this.width)
2024-05-13 16:20:37 +08:00
} else {
2024-05-29 17:05:55 +08:00
this.width = 100 + '%'
2024-05-13 16:20:37 +08:00
this.canvasReset()
2024-05-17 10:31:47 +08:00
2024-05-13 16:20:37 +08:00
}
},
2024-04-30 09:35:44 +08:00
/** 全屏状态切换时,对柱子粗细和字体大小进行相应调整 */
isFullscreen(val) {
2024-05-17 10:31:47 +08:00
if (val === false && this.isOpen === true) {
this.width = 97 + '%'
this.canvasReset()
} else if (val === false && this.isOpen === false) {
2024-05-29 17:05:55 +08:00
this.width = 100 + '%'
2024-05-17 10:31:47 +08:00
this.canvasReset()
}
// 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-30 09:35:44 +08:00
},
series(val) {
if (!val) {
2024-05-13 16:20:37 +08:00
this.initChart(this.options);
2024-04-30 09:35:44 +08:00
return;
}
const actualOptions = JSON.parse(JSON.stringify(this.options));
2024-05-17 10:31:47 +08:00
actualOptions.xAxis.data = val.times
actualOptions.series[0].data = val[0]
actualOptions.series[1].data = val[1];
actualOptions.series[2].data = val[2];
actualOptions.series[3].data = val[3];
actualOptions.series[4].data = val[4];
actualOptions.series[5].data = val[5];
actualOptions.series[6].data = val[6];
2024-04-30 09:35:44 +08:00
this.actualOptions = actualOptions;
2024-05-13 16:20:37 +08:00
this.initChart(actualOptions);
2024-04-30 09:35:44 +08:00
},
},
2024-05-13 16:20:37 +08:00
mounted() {
2024-05-17 10:31:47 +08:00
if (screenfull.isEnabled) {
screenfull.on("change", () => {
this.isFullscreen = screenfull.isFullscreen;
});
}
2024-05-13 16:20:37 +08:00
this.actualOptions = this.options
this.canvasReset();
window.addEventListener("resize", this.canvasReset);
},
beforeDestroy() {
if (this.chart) {
this.chart.dispose();
2024-04-30 09:35:44 +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();
}
2024-05-14 08:46:58 +08:00
this.chart = echarts.init(this.$refs.elsChart);
2024-05-13 16:20:37 +08:00
this.chart.setOption(this.actualOptions);
},
},
2024-04-30 09:35:44 +08:00
};
</script>
<style scoped lang="scss">
.line-chart-base {
// position: relative;
.legend {
position: absolute;
top: 5.2vh;
right: 1vw;
}
.legend-item {
position: relative;
// font-size: 12px;
2024-05-11 16:40:29 +08:00
margin-right: 2vw;
2024-04-30 09:35:44 +08:00
&::before {
content: "";
display: inline-block;
width: 0.432vw;
height: 0.432vw;
border-radius: 100%;
margin-right: 0.4vw;
}
&::after {
content: "";
display: inline-block;
width: 1vw;
height: 0.125vw;
position: absolute;
top: 54%;
left: -15%;
transform: translateY(-50%);
border-radius: 100;
margin-right: 0.22vw;
}
}
.legend-item:nth-child(1):after,
.legend-item:nth-child(1):before {
2024-05-11 16:40:29 +08:00
background-color: #8167F6;
2024-04-30 09:35:44 +08:00
}
.legend-item:nth-child(2):after,
.legend-item:nth-child(2):before {
2024-05-11 16:40:29 +08:00
background-color: #2760FF;
2024-04-30 09:35:44 +08:00
}
.legend-item:nth-child(3):after,
.legend-item:nth-child(3):before {
2024-05-11 16:40:29 +08:00
background-color: #5996F7;
2024-04-30 09:35:44 +08:00
}
.legend-item:nth-child(4):after,
.legend-item:nth-child(4):before {
2024-05-11 16:40:29 +08:00
background-color: #8BC566;
2024-04-30 09:35:44 +08:00
}
.legend-item:nth-child(5):after,
.legend-item:nth-child(5):before {
2024-05-11 16:40:29 +08:00
background-color: #11FAF0;
2024-04-30 09:35:44 +08:00
}
.legend-item:nth-child(6):after,
.legend-item:nth-child(6):before {
2024-05-11 16:40:29 +08:00
background-color: #F3C000;
2024-04-30 09:35:44 +08:00
}
.legend-item:nth-child(7):after,
.legend-item:nth-child(7):before {
2024-05-11 16:40:29 +08:00
background-color: #F38600;
2024-04-30 09:35:44 +08:00
}
}
</style>