echarts 折线图 每段不同颜色

在这里插入图片描述

let aa = [
            {
                time: '2024-01-01 01:00:00',
                val: 10,
                color:'green'
            },
            {
                time: '2024-01-01 02:00:00',
                val: 20,
                color:'yellow'
            },
            {
                time: '2024-01-01 03:00:00',
                val: 50,
                color:'yellow'
            },
            {
                time: '2024-01-01 04:00:00',
                val: 10,
                color:'red'
            },
            {
                time: '2024-01-01 05:00:00',
                val: 20,
                color:'red'
            },
            {
                time: '2024-01-01 06:00:00',
                val: 50,
                color:'green'
            },
            {
                time: '2024-01-02 01:00:00',
                val: 10,
                color:'green'
            },
            {
                time: '2024-01-02 02:00:00',
                val: 20,
                color:'green'
            },
            {
                time: '2024-01-02 03:00:00',
                val: 50,
                color:'red'
            },
            {
                time: '2024-01-02 04:00:00',
                val: 10,
                color:'red'
            },
            {
                time: '2024-01-02 05:00:00',
                val: 20,
                color:'red'
            },
            {
                time: '2024-01-02 06:00:00',
                val: 50,
                color:'yellow'
            },
        ]
        let arr = []
        let xdata = []
        let ydata = []

        for(let i=0;i<aa.length;i++){
            xdata.push(aa[i].time)
            ydata.push(aa[i].val)
            let obj = {
                gt:i,
                lte:i+1,
                color:aa[i].color
            }
            arr.push(obj)
        }


        option =  {
            title: {
                text: "一天用电量分布",
                subtext: "纯属虚构",
            },
            tooltip: {
                trigger: "axis",
                axisPointer: {
                    type: "cross",
                },
            },
            toolbox: {
                show: true,
                feature: {
                    saveAsImage: {},
                },
            },
            xAxis: {
                type: "category",
                boundaryGap: false,
                data: xdata,
            },
            yAxis: {
                type: "value",
                axisLabel: {
                    formatter: "{value} W",
                },
                axisPointer: {
                    snap: true,
                },
            },
            visualMap: [
                {
                    show: false,
                    dimension: 0,
                    seriesIndex: 0, 
                    pieces: arr,
                },

            ],
            series: [
                {
                    name: "用电量",
                    type: "line",
                    smooth: true,
                    data: ydata,
                },

            ],
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值