echarts 一条折线图上显示不同颜色

文档树懒学堂:ECharts visualMap 代码实例及对应注释 - 树懒学堂

封装的echarts 组件代码:

<template>

    <div :style="{ height: '100%' }">

        <div class="foldLine" ref="foldLine" :style="{ width: widths, height: heights }"></div>

    </div>

</template>

<script>

import * as echarts from 'echarts';

export default {

    props: {

        id: {

            type: String,

            default: 'lineEchartNew'

        },

        heights: {

            type: String,

            default: '100%'

        },

        widths: {

            type: String,

            default: '100%'

        },

        tagName: {

            type: String,

            default: '排名'

        },

        indexName: {

            type: String,

            default: ''

        },

        chartObj: {

            type: Array,

            default: () => {

                return [];

            }

        },

        objs: {

            type: Object,

            default: () => {

                return {

                    height: '100%'

                };

            }

        }

    },

    data() {

        return {

            chart: null

        };

    },

    watch: {

        chartObj: {

            handler() {

                this.initEchats();

            },

            deep: true

        }

    },

    mounted() {

        this.$nextTick(() => {

            this.init();

        });

    },

    beforeDestroy() {

        this.chart && this.chart.dispose();

        this.chart = null;

    },

    methods: {

        init() {

            this.chart = echarts.init(this.$refs.foldLine);

            this.initEchats();

        },

        // 整体折线图显示不通颜色

        getLineColor(healthList) {

            let arr = [];

            healthList.forEach((v, i) => {

                let obj = {

                    gt: i,

                    lt: i + 1,

                    color: healthList[i] < 90 ? 'rgba(229,27,88,0.68)' : 'rgba(24,232,169,1)' //大于0小于12为绿色

                };

                arr.push(obj);

            });

            return arr;

        },

        initEchats() {

            let xDataList = []; // x轴

            let healthList = []; // 整体

            let xzHealthList = []; // 修障

            let zyHealthList = []; // 装移

            let zycolorList = []; // 装移

            let xzcolorList = []; // 修障

            let isColor = ''; // 整体

            //isColor == 1 说明低于90分 红色: #F32B2B

            this.chartObj &&

                this.chartObj.forEach((v, index) => {

                    xDataList.push(v.region);

                    // 整体

                    if (v.isColor == 0) {

                        isColor = 'rgba(24,232,169,1)';

                    } else if (v.isColor == 1) {

                        isColor = '#F32B2B'; // 红色

                        // isColor = 'rgba(24,232,169,1)';

                    }

                    // 装移

                    if (v.zyIsColor == 0) {

                        zycolorList[index] = ['#1EE7E7', 'rgba(30, 231, 231, 0.35)'];

                    } else if (v.zyIsColor == 1) {

                        zycolorList[index] = ['rgba(243, 43, 43, 0)', '#F32B2B'];

                    }

                    // 修障

                    if (v.xzIsColor == 0) {

                        xzcolorList[index] = ['#1890FF', 'rgba(24, 144, 255, 0.35)'];

                    } else if (v.xzIsColor == 1) {

                        xzcolorList[index] = ['rgba(243, 43, 43, 0)', '#F32B2B'];

                    }

                    if (!this.tagName || this.tagName == '排名') {

                        healthList.push(v.score);

                        xzHealthList.push(v.xzscore);

                        zyHealthList.push(v.zyscore);

                        this.legendData = [

                            {

                                show: true,

                                top: -3,

                                right: 110,

                                itemWidth: 10,

                                itemHeight: 2,

                                data: [

                                    {

                                        name: '整体',

                                        icon: 'rect'

                                    }

                                ],

                                itemStyle: {

                                    color: '#18E8A9'

                                },

                                textStyle: {

                                    fontSize: 12,

                                    lineHeight: 12,

                                    color: '#fff',

                                    rich: {

                                        a: {

                                            verticalAlign: 'middle'

                                        }

                                    },

                                    padding: [0, 0, -2, 0] //[上、右、下、左]

                                }

                            },

                            {

                                show: true,

                                top: -3,

                                right: 60,

                                itemWidth: 10,

                                itemHeight: 10,

                                data: [{ name: '装移', icon: 'rect' }],

                                textStyle: {

                                    fontSize: 12,

                                    lineHeight: 12,

                                    color: '#fff',

                                    rich: {

                                        a: {

                                            verticalAlign: 'middle'

                                        }

                                    },

                                    padding: [0, 0, -2, 0] //[上、右、下、左]

                                }

                            },

                            {

                                show: true,

                                top: -3,

                                right: 10,

                                itemWidth: 10,

                                itemHeight: 10,

                                data: [{ name: '修障', icon: 'rect' }],

                                textStyle: {

                                    fontSize: 12,

                                    lineHeight: 12,

                                    color: '#fff',

                                    rich: {

                                        a: {

                                            verticalAlign: 'middle'

                                        }

                                    },

                                    padding: [0, 0, -2, 0] //[上、右、下、左]

                                }

                            }

                        ];

                        if (this.indexName == '交付转化') {

                            this.legendData = [

                                {

                                    show: true,

                                    top: -3,

                                    right: 10,

                                    itemWidth: 10,

                                    itemHeight: 10,

                                    data: [{ name: '交付', icon: 'rect' }],

                                    textStyle: {

                                        fontSize: 12,

                                        lineHeight: 12,

                                        color: '#fff',

                                        rich: {

                                            a: {

                                                verticalAlign: 'middle'

                                            }

                                        },

                                        padding: [0, 0, -2, 0] //[上、右、下、左]

                                    }

                                }

                            ];

                        }

                        if (this.indexName == '修障重复申告率') {

                            this.legendData = [

                                {

                                    show: true,

                                    top: -3,

                                    right: 10,

                                    itemWidth: 10,

                                    itemHeight: 10,

                                    data: [{ name: '修障', icon: 'rect' }],

                                    textStyle: {

                                        fontSize: 12,

                                        lineHeight: 12,

                                        color: '#fff',

                                        rich: {

                                            a: {

                                                verticalAlign: 'middle'

                                            }

                                        },

                                        padding: [0, 0, -2, 0] //[上、右、下、左]

                                    }

                                }

                            ];

                        }

                    } else if (this.tagName == '整体') {

                        healthList.push(v.overall);

                        xzHealthList.push(v.xz);

                        zyHealthList.push(v.zy);

                        this.legendData = [

                            {

                                show: true,

                                top: -3,

                                right: 110,

                                itemWidth: 10,

                                itemHeight: 2,

                                data: [

                                    {

                                        name: '整体',

                                        icon: 'rect'

                                    }

                                ],

                                itemStyle: {

                                    color: '#18E8A9'

                                },

                                textStyle: {

                                    fontSize: 12,

                                    lineHeight: 12,

                                    color: '#fff',

                                    rich: {

                                        a: {

                                            verticalAlign: 'middle'

                                        }

                                    },

                                    padding: [0, 0, -2, 0] //[上、右、下、左]

                                }

                            },

                            {

                                show: true,

                                top: -3,

                                right: 60,

                                itemWidth: 10,

                                itemHeight: 10,

                                data: [{ name: '装移', icon: 'rect' }],

                                textStyle: {

                                    fontSize: 12,

                                    lineHeight: 12,

                                    color: '#fff',

                                    rich: {

                                        a: {

                                            verticalAlign: 'middle'

                                        }

                                    },

                                    padding: [0, 0, -2, 0] //[上、右、下、左]

                                }

                            },

                            {

                                show: true,

                                top: -3,

                                right: 10,

                                itemWidth: 10,

                                itemHeight: 10,

                                data: [{ name: '修障', icon: 'rect' }],

                                textStyle: {

                                    fontSize: 12,

                                    lineHeight: 12,

                                    color: '#fff',

                                    rich: {

                                        a: {

                                            verticalAlign: 'middle'

                                        }

                                    },

                                    padding: [0, 0, -2, 0] //[上、右、下、左]

                                }

                            }

                        ];

                        if (this.indexName == '交付转化') {

                            this.legendData = [

                                {

                                    show: true,

                                    top: -3,

                                    right: 10,

                                    itemWidth: 10,

                                    itemHeight: 10,

                                    data: [{ name: '交付', icon: 'rect' }],

                                    textStyle: {

                                        fontSize: 12,

                                        lineHeight: 12,

                                        color: '#fff',

                                        rich: {

                                            a: {

                                                verticalAlign: 'middle'

                                            }

                                        },

                                        padding: [0, 0, -2, 0] //[上、右、下、左]

                                    }

                                }

                            ];

                        }

                        if (this.indexName == '修障重复申告率') {

                            this.legendData = [

                                {

                                    show: true,

                                    top: -3,

                                    right: 10,

                                    itemWidth: 10,

                                    itemHeight: 10,

                                    data: [{ name: '修障', icon: 'rect' }],

                                    textStyle: {

                                        fontSize: 12,

                                        lineHeight: 12,

                                        color: '#fff',

                                        rich: {

                                            a: {

                                                verticalAlign: 'middle'

                                            }

                                        },

                                        padding: [0, 0, -2, 0] //[上、右、下、左]

                                    }

                                }

                            ];

                        }

                    } else if (this.tagName == '交付') {

                        healthList = [];

                        xzHealthList = [];

                        zyHealthList.push(v.zy);

                        this.legendData = [

                            {

                                show: true,

                                top: -3,

                                right: 10,

                                itemWidth: 10,

                                itemHeight: 10,

                                data: [{ name: this.indexName == '交付转化' ? '交付' : '装移', icon: 'rect' }],

                                textStyle: {

                                    fontSize: 12,

                                    lineHeight: 12,

                                    color: '#fff',

                                    rich: {

                                        a: {

                                            verticalAlign: 'middle'

                                        }

                                    },

                                    padding: [0, 0, -2, 0] //[上、右、下、左]

                                }

                            }

                        ];

                    } else if (this.tagName == '申告') {

                        healthList = [];

                        zyHealthList = [];

                        xzHealthList.push(v.xz);

                        this.legendData = [

                            {

                                show: true,

                                top: -3,

                                right: 10,

                                itemWidth: 10,

                                itemHeight: 10,

                                data: [{ name: this.indexName == '交付转化' ? '交付' : '修障', icon: 'rect' }],

                                textStyle: {

                                    fontSize: 12,

                                    lineHeight: 12,

                                    color: '#fff',

                                    rich: {

                                        a: {

                                            verticalAlign: 'middle'

                                        }

                                    },

                                    padding: [0, 0, -2, 0] //[上、右、下、左]

                                }

                            }

                        ];

                    }

                });

            let seriesAll = [

                {

                    name: '整体',

                    type: 'line',

                    // barWidth: 24,

                    barGap: '1%',

                    // symbol: 'rect',

                    symbolSize: [0, 0],

                    data: healthList,

                    // label: {

                    //     normal: {

                    //         show: true,

                    //         fontSize: 14,

                    //         color: '#fff',

                    //         formatter: '{c}',

                    //         position: 'top'

                    //     }

                    // },

                    itemStyle: {

                        color: 'rgba(24,232,169,1)',

                        // color: isColor,

                        borderColor: '#18E8A9',

                        borderWidth: 1,

                        borderType: 'solid'

                    }

                },

                {

                    name: '装移',

                    type: 'bar',

                    barWidth: 5,

                    barGap: '1%',

                    data: zyHealthList,

                    itemStyle: {

                        normal: {

                            //这里设置每个柱子颜色不一样

                            color: function (params) {

                                let colorItem = zycolorList[params.dataIndex];

                                if (colorItem !== undefined) {

                                    return new echarts.graphic.LinearGradient(

                                        0,

                                        0,

                                        0,

                                        1,

                                        [

                                            {

                                                offset: 0,

                                                color: colorItem[0]

                                            },

                                            {

                                                offset: 1,

                                                color: colorItem[1]

                                            }

                                        ],

                                        false

                                    );

                                }

                            }

                        }

                    }

                    // color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [

                    //     {

                    //         offset: 0,

                    //         color: '#1EE7E7 '

                    //     },

                    //     {

                    //         offset: 1,

                    //         color: 'rgba(30, 231, 231, 0.35)'

                    //     }

                    // ])

                },

                {

                    name: '修障',

                    type: 'bar',

                    barWidth: 5,

                    barGap: '1%',

                    data: xzHealthList,

                    itemStyle: {

                        normal: {

                            //这里设置每个柱子颜色不一样

                            color: function (params) {

                                let colorItem = xzcolorList[params.dataIndex];

                                if (colorItem !== undefined) {

                                    return new echarts.graphic.LinearGradient(

                                        0,

                                        0,

                                        0,

                                        1,

                                        [

                                            {

                                                offset: 0,

                                                color: colorItem[0]

                                            },

                                            {

                                                offset: 1,

                                                color: colorItem[1]

                                            }

                                        ],

                                        false

                                    );

                                }

                            }

                        }

                    }

                    // color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [

                    //     {

                    //         offset: 0,

                    //         color: '#1890FF '

                    //     },

                    //     {

                    //         offset: 1,

                    //         color: 'rgba(24, 144, 255, 0.35)'

                    //     }

                    // ])

                }

            ];

            let seriesJiaoFu = [

                {

                    name: '交付',

                    type: 'bar',

                    barWidth: 5,

                    barGap: '1%',

                    data: zyHealthList,

                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [

                        {

                            offset: 0,

                            color: '#1EE7E7 '

                        },

                        {

                            offset: 1,

                            color: 'rgba(30, 231, 231, 0.35)'

                        }

                    ])

                }

            ];

            let axisLabelAll = {

                color: 'rgba(230,247,255,0.85)',

                interval: 0, //0: x轴名字没有间隔隐藏,全部显示

                fontSize: 10,

                // rotate: 40, // 调整数值改变倾斜的幅度(范围-90到90)

                formatter: (text) => {

                    return text.replaceAll('分公司', '');

                }

            };

            let axisLabelMore = {

                color: 'rgba(230,247,255,0.85)',

                // interval: 0, //0: x轴名字没有间隔隐藏,全部显示

                fontSize: 10

            };

            let option = {

                tooltip: {

                    trigger: 'axis',

                    axisPointer: {

                        // 坐标轴指示器,坐标轴触发有效 设置鼠标放上去是线还是阴影

                        type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'

                    }

                    // formatter:function(param){

                    //     if(param.length>2){

                    //          return `

                    //          <p>时间:${param[0].name}</p>

                    //          <p>${param[0].seriesName}:能耗${param[0].data},PRB利用率:${param[1].data}</p>

                    //             <p>${param[2].seriesName}:能耗:${param[2].data},PRB利用率:${param[3].data}</p>

                    //         `

                    //     }else{

                    //          return `

                    //           <p>时间:${param[0].name}</p>

                    //          <p>${param[0].seriesName}:能耗${param[0].data},PRB利用率:${param[1].data}</p>

                    //         `

                    //     }

                    // }

                },

                legend: this.legendData,

                grid: {

                    top: '16%',

                    left: '3%',

                    right: '5%',

                    bottom: '7%',

                    containLabel: true

                },

                yAxis: {

                    name: '',

                    nameTextStyle: {

                        color: '#fff',

                        align: 'center'

                    },

                    nameGap: 20,

                    type: 'value',

                    splitLine: {

                        show: true,

                        lineStyle: {

                            //轴线样式

                            type: 'dashed', // dotted

                            color: 'rgba(230, 247, 255, 0.2)'

                        }

                    },

                    axisLine: {

                        //坐标轴轴线

                        show: false, //是否显示

                        lineStyle: {

                            //轴线样式

                            color: '#656A94'

                        }

                    },

                    axisTick: {

                        show: false //是否显示刻度

                    },

                    axisLabel: {

                        inside: false, //数值内侧外侧

                        // padding: [0, 4, 15, 0],

                        showMinLabel: true,

                        showMaxLabel: true,

                        color: 'rgba(230,247,255,0.6)',

                        fontSize: 12

                        // formatter:function(text,index){

                        //     return text+"%";

                        // }

                    }

                },

                xAxis: [

                    {

                        type: 'category',

                        data: xDataList,

                        axisLine: {

                            lineStyle: {

                                color: '#E6F7FF'

                            }

                        },

                        axisTick: {

                            show: false

                        },

                        // axisLabel: {

                        //     color: 'rgba(230,247,255,0.85)',

                        //     interval: 0, //0: x轴名字没有间隔隐藏,全部显示

                        //     fontSize: 10,

                        //     // rotate: 40, // 调整数值改变倾斜的幅度(范围-90到90)

                        //     formatter: (text) => {

                        //         return text.replaceAll('分公司', '');

                        //     }

                        // }

                        axisLabel: xDataList.length > 12 ? axisLabelMore : axisLabelAll

                    },

                    {

                        type: 'category',

                        axisLine: {

                            show: false

                        },

                        axisTick: {

                            show: false

                        },

                        axisLabel: {

                            show: false

                        }

                    }

                ],

                series: this.indexName == '交付转化' ? seriesJiaoFu : seriesAll,

                // 一条折线图显示不通颜色

                visualMap:

                    this.indexName == '交付转化'

                        ? null

                        : {

                              type: 'piecewise',

                              show: false,

                              dimension: 0,

                              seriesIndex: 0,

                              pieces: this.getLineColor(healthList)

                              //   pieces: [

                              //       {

                              //           gt: 0,

                              //           lt: 1,

                              //           color: healthList[0] < 90 ? 'rgba(229,27,88,0.68)' : 'rgba(24,232,169,1)' //大于0小于12为绿色

                              //       },

                              //       {

                              //           gt: 1,

                              //           lt: 2,

                              //           color: healthList[1] < 90 ? 'rgba(229,27,88,0.68)' : 'rgba(24,232,169,1)' //大于0小于12为绿色

                              //       },

                              //   ]

                          }

            };

            if (option && typeof option === 'object') {

                this.chart.clear(); //画布清空

                this.chart.resize(); //自适应div的大小

                this.chart.setOption(option, true);

                // this.chart.setOption(

                //     {

                //         series: seriesArr

                //     },

                //     { notMerge: false, lazyUpdate: false, silent: false }

                // );

            }

        }

    }

};

</script>

<style lang="scss" scoped></style>

  • 25
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 使用 echarts 的 itemStyle 属性,可以设置折线段的颜色。可以参考官网文档:https://echarts.apache.org/zh/option.html#series-line.itemStyle ### 回答2: 在Echarts折线图中,要实现折线分段显示不同颜色,可以通过使用"series"中的"itemStyle"属性来实现。 首先,在数据项中,我们可以设置每个数据点的特定颜色,例如: ```javascript data: [ {value: 120, itemStyle: {color: 'red'}}, {value: 200, itemStyle: {color: 'blue'}}, {value: 150, itemStyle: {color: 'green'}} ] ``` 然后,在图表的配置项中,找到对应的series配置项,设置"itemStyle"属性为一个函数,这个函数将根据数据项中的颜色进行渲染: ```javascript series: [{ type: 'line', data: [ {value: 120, itemStyle: {color: 'red'}}, {value: 200, itemStyle: {color: 'blue'}}, {value: 150, itemStyle: {color: 'green'}} ], itemStyle: function (params) { return params.data.itemStyle; } }] ``` 这样配置后,折线图的每个数据点将根据其特定颜色进行渲染,从而实现了折线分段显示不同颜色。 需要注意的是,以上示例是基于Echarts4的配置方式,如果是其他版本可能会有些许差异。另外,为了使折线图更加美观,也可以在"itemStyle"中配置其他样式,如设置线条粗细、线条样式等。希望以上回答对你有所帮助。 ### 回答3: 要实现echarts折线图折线分段显示不同颜色,可以使用echarts的区域标记功能。 首先,需要设置折线图的x轴数据和y轴数据。然后,使用series配置项中的markArea属性来标记不同的区域。 例如,我们有以下数据: ```javascript // x轴数据 var xData = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']; // y轴数据 var yData = [120, 200, 150, 80, 70, 110, 130]; ``` 然后,在series配置项中设置markArea属性,来标记不同的区域: ```javascript series: [{ type: 'line', data: yData, markArea: { data: [{ yAxis: 0, // 区域起始位置 itemStyle: { color: 'rgba(255, 0, 0, 0.3)' // 区域颜色 } },{ yAxis: 100, // 区域终止位置 itemStyle: { color: 'rgba(0, 255, 0, 0.3)' // 区域颜色 } }] } }] ``` 以上代码中,我们设置了两个区域,一个从y轴0开始,另一个从y轴100开始。通过设置itemStyle中的color属性来指定不同区域的颜色。 最后,将x轴数据和series配置项应用到echarts实例中,即可实现折线图折线分段显示不同颜色。 ```javascript var myChart = echarts.init(document.getElementById('chart')); myChart.setOption({ xAxis: { data: xData }, series: [{ type: 'line', data: yData, markArea: { data: [{ yAxis: 0, itemStyle: { color: 'rgba(255, 0, 0, 0.3)' } },{ yAxis: 100, itemStyle: { color: 'rgba(0, 255, 0, 0.3)' } }] } }] }); ``` 这样,echarts折线图的折线就会按照设定的区域进行分段显示,每个区域的颜色也会根据设置来呈现。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值