wujingjing
2024-08-29 19b778d2d04bed31ce2e1f167c6ff2fda9906421
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<template>
    <div class="model-binding-right-box">
        <div id="mainChart" style="height: calc(100% - 25px)"></div>
    </div>
</template>
<script setup lang="ts">
import * as echarts from 'echarts';
import { onMounted, watch, reactive, shallowRef, toRefs } from 'vue';
import { formatTime } from '/@/utils/istation/common.js';
const myChart = shallowRef(null);
// 定义变量内容
const state = reactive({
    themeTime: '' as any,
    queryDisabled: false,
    chartDataInfo: {
        SeriesData: [],
        XData: [],
    },
    getFreshData: [], //x轴y轴的数据
    selectSignalID: '',
    selectSignalName: '',
    echartTitle: '',
});
 
onMounted(() => {
    window.addEventListener('resize', selfAdaption);
});
//#region ====================== 图表传参 ======================
const props = defineProps({
    selectDataInfo: {
        type: Object,
    },
    echartBar: {
        type: Object,
    },
});
let { selectDataInfo, echartBar } = toRefs(props);
watch(selectDataInfo, async (newVal, oldVal) => {
    if (newVal == oldVal) {
        return;
    }
    state.selectSignalID = newVal.SignalID;
    // console.log(state.selectSignalID, 44);
    state.echartTitle = newVal.selectIDTitle;
});
watch(echartBar, async (newVal, oldVal) => {
    if (newVal == oldVal) {
        return;
    }
    state.chartDataInfo.SeriesData = newVal.SeriesData;
    state.chartDataInfo.XData = newVal.XData;
    setTimeout(() => {
        drawBar();
    }, 100);
});
//#endregion
 
//#region ====================== 绘图init ======================
//  初始化echarts折线图
const drawBar = () => {
    myChart.value && myChart.value.clear();
    //先获取Dom上的实例
    let chartDom = echarts.getInstanceByDom(document.getElementById('mainChart') as HTMLDivElement);
    //然后判断实例是否存在,如果不存在,就创建新实例
    if (chartDom == null) {
        chartDom = echarts.init(document.getElementById('mainChart') as HTMLDivElement);
        myChart.value = chartDom;
    }
 
    let series_arr: any = [];
    series_arr.push({
        type: 'bar',
        barWidth: '30px',
        showBackground: true,
        backgroundStyle: {
            color: 'rgba(180, 180, 180, 0.2)',
        },
 
        data: state.chartDataInfo.SeriesData,
    });
 
    let option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow',
            },
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true,
        },
        xAxis: [
            {
                name: '时间',
                type: 'category',
                data: state.chartDataInfo.XData,
                axisTick: {
                    alignWithLabel: true,
                },
                axisLabel: {
                    formatter: function (value) {
                        const date = new Date(value);
                        return formatTime('yyyy-MM-dd', date);
                    },
                },
            },
        ],
        yAxis: [
            {
                name: '值',
                type: 'value',
                // 整条y轴
                axisLine: {
                    show: true,
                },
            },
        ],
        series: series_arr,
    };
    myChart.value.setOption(option);
    myChart.value.resize();
};
//#endregion
//#region ====================== 图表自适应 ======================
 
//  自适应
const selfAdaption = () => {
    if (!myChart.value) return;
    myChart.value.resize();
    setTimeout(() => {
        drawBar();
    }, 100);
};
//#endregion
</script>
<style scoped lang="scss">
.model-binding-right-box {
    width: 100%;
    height: 100%;
    background: #fff;
}
</style>