• vue3之echarts区域折线图


    vue3之echarts区域折线图

    效果:
    在这里插入图片描述
    核心代码:

    <template>
        <div class="abnormal">
            <div class="per">单位:{{ obj.data?.unit }}</div>
            <div class="chart" ref="chartsRef"></div>
        </div>
    </template>
    
    <script setup>
    import * as echarts from 'echarts';
    import { reactive, onMounted } from 'vue';
    
    const obj = reactive({
        data: {
            xdata: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
            ydata: [ 0, 0, 0, 1354, 0, 254, 0, 468, 0, 16, 498, 0],
            unit: '次'
        },
        op: {
            tooltip: {},
            grid: {
                top: '5%',
                left: '0%',
                right: '2%',
                bottom: '0%',
                containLabel: true,
            },
            xAxis: null,
            yAxis: [
                {
                    type: 'value',
                    nameLocation: 'end',
                    axisTick: {
                        show: false,
                    },
                    axisLine: {
                        show: false,
                    },
                    axisLabel: {
                        textStyle: {
                            fontSize: 13,
                            color: '#ffffff',
                            fontFamily: 'DINPro-Regular',
                        },
                        padding: [0, 0, 0, -10],
                    },
                    splitLine: {
                        lineStyle: {
                            type: 'dashed',
                            color: 'rgba(160, 169, 192, 0.8)',
                        },
                    },
                },
            ],
            series: [],
        },
    })
    let myCharts = null;
    let chartsRef = ref(null)
    
    const flushChart = () => {
        obj.op.xAxis = {};
        obj.op.series = [];
        obj.op.tooltip = {};
    
        obj.op.tooltip = {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow',
                lineStyle: {
                    color: 'rgba(135, 140, 147, 0.6)',
                },
            },
            formatter(params) {
                return `${params[0].name}月 : ${params[1].value}`;
            },
            backgroundColor: 'rgba(45, 105, 133, 0.8)',
            borderWidth: 0,
            textStyle: {
                color: '#DAE3E7',
                fontFamily: 'DINPro-Regular',
            },
        };
        obj.op.xAxis = {
            type: 'category',
            boundaryGap: false, // 坐标轴两边留白
            data: obj.data.xdata,
            axisLabel: {
                margin: 10,
                textStyle: {
                    color: '#ffffff',
                    fontSize: 13,
                    fontFamily: 'DINPro-Regular',
                },
    
            },
            axisLine: {
                lineStyle: {
                    color: 'rgba(160, 169, 192, 0.6)',
                },
            },
            axisTick: {
                show: true,
            },
        };
        obj.op.series.push(
            {
                name: 'bg',
                type: 'bar',
                data: [...Array(obj.data.ydata.length).fill(Math.max(...obj.data.ydata) === 0 ? 10 : Math.max(...obj.data.ydata))],
                barWidth: '50%',
                itemStyle: {
                    color: 'rgba(255, 255, 255, 0.02)',
                },
            },
        );
        obj.op.series.push(
            {
                type: 'line',
                symbol: 'none',
                itemStyle: {
                    normal: {
                        color: '#68A4FF',
                        lineStyle: {
                            color: '#68A4FF',
                            width: 3,
                        },
                        areaStyle: {
                            // 区域填充样式
                            color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                                {
                                    offset: 0,
                                    color: 'rgba(104,164,255,0)',
                                }, {
                                    offset: 0.5,
                                    color: 'rgba(104,164,255,0.6)',
                                },
                                {
                                    offset: 1,
                                    color: 'rgba(104,164,255,1)',
                                },
                            ]),
                        },
                    },
                },
                toolbox: {
                    show: false,
                },
                data: obj.data.ydata,
            },
        );
        myCharts.setOption(obj.op);
    }
    
    const initChart = () => {
        myCharts = echarts.init(chartsRef.value);
        flushChart();
    } 
    
    onMounted(() => {
        initChart();
    })
    
    const destroyChart = () => {
        if (!myCharts) {
            return;
        }
        myCharts.dispose();
        myCharts = null;
    }
    
    onBeforeMount(() => {
        destroyChart();
    })
    </script>
    
    <style lang="scss" scoped>
    .abnormal {
        position: relative;
        width: 100%;
        height: 276px;
        margin-top: 19px;
        display: inline-block;
    
        .per {
            font-size: 12px;
            color: rgba(255, 255, 255, 0.5);
            margin-bottom: 19px;
        }
    
        .chart {
            display: inline-block;
            width: 100%;
            height: 185px;
            zoom: 1;
        }
    }
    </style>
    
    
    • 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
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
  • 相关阅读:
    要想增加流量需要做的几点,淘宝运营新手必看的免费流量小知识
    0918 框架理论知识
    单链表基础知识讲解
    Worthington酒精脱氢酶的特异性和五个应用
    flink中使用外部定时器实现定时刷新
    【linux命令讲解大全】107.mkdir命令:创建目录的指令
    Java Integer.toOctalString()具有什么功能呢?
    每日一题·对原型和原型链的理解(12/1)
    linux基础(2)
    Linux命令学习之原来最简单的ls命令这么复杂
  • 原文地址:https://blog.csdn.net/qq_40864647/article/details/134511002