vue3之echarts渐变柱状图

vue3之echarts渐变柱状图

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

<template>
    <div class="abnormal">
        <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: [350, 320, 310, 450, 150, 300, 250, 250, 310, 280, 200, 180],
    },
    op: {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow',
                lineStyle: {
                    color: 'rgba(135, 140, 147, 0.6)',
                    width: 10,
                },
            }, // 鼠标移上的阴影,默认是线
        },
        grid: {
            top: '5%',
            right: '1%',
            left: '0%',
            bottom: '0%',
            containLabel: true,
        },
        xAxis: [],
        yAxis: [
            {
                type: 'value',
                name: '装载量(万吨)',
                position: 'left',
                axisLabel: {
                    formatter: '{value}',
                    color: '#FFFFFF',
                    textStyle: {
                        fontSize: 13,
                        fontFamily: 'DINPro-Regular',
                    },
                    interval: 0,
                },
                axisLine: {
                    show: false,
                    lineStyle: {
                        color: 'rgba(255,255,255,1)',
                    },
                },
                axisTick: {
                    // x轴刻度相关设置
                    alignWithLabel: true,
                },
                splitLine: {
                    lineStyle: {
                        type: 'dashed',
                        color: 'rgba(255,255,255,0.12)',
                    },
                },
            },
            {
                type: 'value',
                name: '装载车次(千辆)',
                nameLocation: 'end',
                position: 'right',
                axisTick: {
                    show: false,
                },
                axisLine: {
                    show: false,
                },
                axisLabel: {
                    formatter: '{value}',
                    textStyle: {
                        fontSize: 13,
                        color: '#ffffff',
                        fontFamily: 'DINPro-Regular',
                    },
                },
                axisTick: {
                    // x轴刻度相关设置
                    alignWithLabel: true,
                },
                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.xAxis.push(
        {
            type: 'category',
            data: obj.data.xdata,
            axisLine: {
                lineStyle: {
                    color: 'rgba(255,255,255,0.12)',
                },
            },
            axisLabel: {
                margin: 11,
                color: '#FFFFFF',
                textStyle: {
                    fontSize: 13,
                    fontFamily: 'DINPro-Regular',
                },
            },
        }
    );

    obj.op.series.push(
        {
            type: 'bar',
            data: obj.data.ydata,
            name: '装载量',
            stack: 'Ad',
            barWidth: '5px',
            itemStyle: {
                normal: {
                    color: new echarts.graphic.LinearGradient(
                        0,
                        0,
                        0,
                        1,
                        [
                            {
                                offset: 0,
                                color: '#77FB9F', // 0% 处的颜色
                            },
                            {
                                offset: 1,
                                color: 'rgba(255,255,255,0)', // 100% 处的颜色
                            },
                        ],
                        false,
                    ),
                },
            },
        },
    );
    myCharts.setOption(obj.op);
}

const initChart = () => {
    myCharts = echarts.init(chartsRef.value);
}


onMounted(() => {
    initChart();
    flushChart();
})

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;

    .chart {
        display: inline-block;
        width: 100%;
        height: 185px;
        zoom: 1;
    }
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值