当前位置: 首页 > article >正文

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>


http://www.kler.cn/news/233036.html

相关文章:

  • 【Android】GridLayout实现等比布局
  • DC-9靶机渗透详细流程
  • 每日五道java面试题之java基础篇(一)
  • 飞桨自然语言处理框架 paddlenlp的 trainer
  • openssl3.2 - exp - buffer to BIO
  • HarmonyOS SDK 助力新浪新闻打造精致易用的新闻应用
  • 【ETOJ P1046】斐波那契数列 题解(数学+动态规划)
  • Electron+Vue实现仿网易云音乐实战
  • python 基础知识点(蓝桥杯python科目个人复习计划35)
  • 【开源】JAVA+Vue+SpringBoot实现实验室耗材管理系统
  • 前端图片转base64 方法
  • MinGW/MSYS/GCC/GNU/MSVC/Clang/LLVM都是什么
  • 【01】判断素数/质数(C语言)
  • 使用深度学习对网络摄像头图像进行分类
  • node网站 宝塔 面板配置 防止刷新404
  • DNS 域名系统——应用层
  • 数字图像处理与Python语言实现-常见图像特效(三)
  • 记:STM32F4参考手册-存储器和总线架构
  • Android:Android Studio安装及环境配置
  • (52)只出现一次的数字III
  • Python学习之路-Tornado基础:安全应用
  • 探索未来:集成存储器计算(IMC)与深度神经网络(DNN)的机遇与挑战
  • 「递归算法」:子集(两种解法)
  • 泛娱乐社交出海洞察,Flat Ads解锁海外增长新思路
  • 创建一个VUE项目(vue2和vue3)
  • cleanmymacX和腾讯柠檬哪个好用
  • (delphi11最新学习资料) Object Pascal 学习笔记---第4章第2.6节(默认参数)
  • Java图形化界面编程—— 基本组件和对话框 笔记
  • Spring IoC容器详解
  • 在centos7中利用pybind11构建C++的动态库供python调用