echarst加载动态数据,格式化legend

10 篇文章 0 订阅

赋值写在接口数据加载里面,避免数据加载不同步

获取接口,传参 

页面数据数组:showPage:[]

legend格式化: 

 // 回调函数 实现标志图形字体后边加上百分比
                    formatter: function(name) {
                        let total = 0
                        let tarValue = 0
                        for (let i = 0; i < dt.length; i++) {
                            total += dt[i].value
                            if (dt[i].name === name) {
                                tarValue = dt[i].value
                            }
                        }
                        const p = (tarValue / total) * 100
                        return name + ' ' + tarValue + '人'
                        // return name + ' ' + tarValue + '人' + ' ' + p.toFixed(2) + '%'
                    }

  获取数据,初始化图表

// 获取数据
        getData() {
            chartCount(this.queryParams).then(res => {
                this.seriesData = res.data.organization
                this.seriesData && this.seriesData.forEach((item, index) => {
                    if (item.typeName === '协作队伍') {
                        this.seriesData.splice(index, 1)
                    }
                })
                this.seriesData && this.seriesData.forEach((item, index) => {
                    this.seriesData[index] = JSON.parse(
                        JSON.stringify(item)
                            .replace(/statisCount/gi, 'value')
                            .replace(/typeName/gi, 'name')
                    )
                    
                })
                if (this.seriesData.length > this.pageSize) {
                    this.pageCount = Math.ceil(this.seriesData.length / this.pageSize)
                    const list = (this.currentPage - 1) * this.pageSize
                    this.showPage = this.seriesData.slice(list, list + this.pageSize)
                }else {
                    this.pageCount = 1
                    this.showPage = this.seriesData
                }
           //初始图表,传入参数
                this.getOptions(this.showPage)
            })
        },
        

 图标方法,传参,格式化legend

getOptions(dt) {
           
            this.labourOption = {
                // color:['#EB9AFF','#25A54F','#2CC6EA','#0263FF','#FB6E52','#8256E8','#FF7E00','#FFC655'],
                tooltip: {
                    trigger: 'item',
                    formatter: '{b}: <br> {c}人 ({d}%)'// 模板变量有 {a}、{b}、{c}、{d},分别表示系列名,数据名,数据值,百分比。
                },
                legend: {
                    orient: 'vertical',
                    top: 5,
                    left: 15,
                    icon: 'rect',
                    itemWidth: 8,
                    itemHeight: 8,
                    // padding: [5, 0, 0, 0],
                    itemGap: 20,
                    textStyle: {
                        fontSize: 10,
                        color: '#ffffff',
                        rich: {
                            //这里定义a的样式
                            a: {
                                width: 100
                            }
                        }
                    },
                    // 回调函数 实现标志图形字体后边加上百分比
                    formatter: function(name) {
                        let total = 0
                        let tarValue = 0
                        for (let i = 0; i < dt.length; i++) {
                            total += dt[i].value
                            if (dt[i].name === name) {
                                tarValue = dt[i].value
                            }
                        }
                        const p = (tarValue / total) * 100
                        return name + ' ' + tarValue + '人'
                        // return name + ' ' + tarValue + '人' + ' ' + p.toFixed(2) + '%'
                    }
                    // data: []
                },
                series: [
                    {
                        /* top: -50,
                                right:-200,*/
                        type: 'pie',
                        radius: ['45%', '70%'],
                        // center: ['', ''],第一个值调整左右,第二个值调整上下
                        center: ['75%', '45%'],
                        avoidLabelOverlap: false,
                        itemStyle: { // 图形样式
                            normal: { // normal 是图形在默认状态下的样式;emphasis 是图形在高亮状态下的样式,比如在鼠标悬浮或者图例联动高亮时。
                                label: {
                                    show: true,
                                    position: 'inner',
                                    formatter: '{d}%',
                                    distance: 0.7, // 这项是标识距离中心点的距离
                                    textStyle: {
                                        align: 'center',
                                        baseline: 'middle',
                                        fontFamily: '微软雅黑',
                                        fontSize: '14px',
                                        fontWeight: 'bold'
                                    }
                                },
                                /* label: {
                                            color: '#ffffff',
                                            show : true,
                                            formatter: '{b}: {c}%\n',
                                            padding: [0, -57]
                                        },*/
                                labelLine: {
                                    show: false,
                                    length: 10,
                                    length2: 80,
                                    lineStyle: {
                                        color: '#ffffff'
                                    }
                                }
                                /* label : {  //饼图图形上的文本标签
                                            show : true , //平常不显示
                                            fontSize: '14px',
                                            fontWeight: 'bold'
                                        },
                                        labelLine : {     //标签的视觉引导线样式
                                            show : false  //平常不显示
                                        }*/
                            }
                            /* emphasis : {   //normal 是图形在默认状态下的样式;emphasis 是图形在高亮状态下的样式,比如在鼠标悬浮或者图例联动高亮时。
                                         label : {  //饼图图形上的文本标签
                                             show : true,
                                             position : 'center',
                                             textStyle : {
                                                 fontSize : '10',
                                                 fontWeight : 'bold'
                                             }
                                         }
                                     }*/
                        },

                        data: dt
                    }
                ],
                color: this.colorData
            }
        },

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值