• echarts点击按钮排序


            const rootData = '你的参数'
    
            var sort = [false, false];
    
            setTimeout(() => {
                initChart(rootData)
            });
    
            function initChart(res) {
                // 根据排序条件处理数据
                if (sort[0]) {
                    res = ([].concat(rootData)).sort((a, b) => {
                        return a.indicatorTree - b.indicatorTree
                    })
                } else if (sort[1]) {
                    res = ([].concat(rootData)).sort((a, b) => {
                        return a.indicatorOne - b.indicatorOne
                    })
                }else if(sort[2]){
                    res = ([].concat(rootData)).sort((a, b) => {
                        return a.indicatorTwo - b.indicatorTwo
                    })
                }
    
    
                let data = [
                    {
                        name: '总和',
                        data: res.map(v => v.indicatorTree),
    
                    },
                    {
                    name: '网费',
                    data: res.map(v => v.indicatorOne),
    
                    },
                    {
                        name: '商品',
                        data: res.map(v => v.indicatorTwo),
                    }
                ]
                let axis = res.map(v => v.name)
                let colors = ['#ff3824','#3c54ff', '#23af41']
                let richStyle = {
                    a: {
                        color: "#fff",
                        fontSize: 9,
                        fontWeight:600,
                        verticalAlign: 'middle'
                    }
                }
                let legendSelected = {}
                let legendData = []
                data.forEach((v, i) => {
                    legendSelected[v.name] = true // 默认选中所有图例
                    legendData.push({
                        name: data[i].name,
                        selected: true,
                        textStyle: {
                            rich: {
                                a: {
                                    color: sort[i] ? "#00a0f8" : '#3d3d3d',
                                    fontSize: 12,
                                    fontWeight:600,
                                    verticalAlign: 'middle'
                                }
                            }
                        }
                    })
                })
                let option = {
                    backgroundColor: '#F1F1F4',
                    color: colors,
                    legend: {
                        textStyle: {
                            color: '#3d3d3d',
                            fontSize: 9,
                            fontWeight:600
                        },
                        itemGap: 50,
                        itemWidth: 15,
                        itemHeight: 18,
                        icon: 'rect',
                        top: '5%',
                        left: 'center',
                        selected: legendSelected,
                        data: legendData,
                        formatter: name => `${name}  {a|⇅}`
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'shadow'
                        }
                    },
                    grid: {
                        top: '15%',
                        left: '20%',
                        right: '0%',
                        bottom: '10%',
                        width: '80%',
                        height: '80%'
                    },
                    xAxis: {
                        axisLine: {
                            lineStyle: {
                                color: '#d2d2d2'
                            }
                        },
                        splitLine: {
                            show: true,
                            lineStyle: {
                                color: '#cdcdcd'
                            }
                        },
                        axisLabel: {
                            textStyle: {
                                color: '#3d3d3d',
                                fontSize: 9,
                                fontWeight:600
                            },
                            color: "#e2e2e2"
                        },
                    },
                    yAxis: {
                        axisTick: {
                            alignWithLabel: true,
                        },
    
                        axisLine: {
                            lineStyle: {
                                color: '#d2d2d2'
                            }
                        },
    
                        axisLine: {
                            lineStyle: {
                                color: '#3d3d3d'
                            }
                        },
                        axisLabel: {
                            textStyle: {
                                color: '#00a0f8',
                                fontSize: 9,
                                fontWeight:600
                            },
                        },
                        data: axis
                    },
                    series: [
                        {
                            name: data[0].name,
                            type: 'bar',
                            barGap: '-100%',
                            barWidth: 0,
                            data: data[0].data,
                            label: {
                                normal: {
                                    show: true,
                                    position: 'right',
                                    textStyle: { color: '#ff3824' ,fontSize: 9,fontWeight:600}
                                }
                            },
                        },
    
                        {
                        name: data[1].name,
                        type: 'bar',
                        stack: 'one',
                        barWidth: 0,
                        data: data[1].data,
                        label: {
                            show: true,
                            textStyle: { //数值样式
                                color: '#fff',
                                fontSize: 9,
                                fontWeight:600
    
                            }
                        },
                        },
    
                        {
                            name: data[2].name,
                            type: 'bar',
                            stack: 'one',
                            barWidth: 0,
                            data: data[2].data,
                            label: {
                                show: true,
                                textStyle: { //数值样式
                                    color: '#fff',
                                    fontSize: 9,
                                    fontWeight:600
                                }
                            },
                        },
    
                    ]
                };
                let myChart = echarts.init(document.getElementById('myChart'));
                myChart.setOption(option);
    
                myChart.off('legendselectchanged') // 取消事件,避免事件绑定重复导致多次触发
                myChart.on('legendselectchanged', e => {
                    let index = data.findIndex(v => v.name === e.name)
                    let flag = sort[index]
                    sort = sort.map(v => false) // 全部重置为false,保证同时只能根据一个字段排序
                    sort[index] = !flag
                    initChart(rootData)
                })
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
  • 相关阅读:
    方法引用知识点
    基本的爬虫工作原理
    NC200369 四舍五入(枚举)
    「Spring Boot 系列」04. Spring Boot配置------获取数据
    vscode配置typescript及简单使用
    2022-08-25-反射
    springboot毕设项目摄影跟拍预定管理系统808t0(java+VUE+Mybatis+Maven+Mysql)
    浅谈C++|类的成员
    【Linux】shell命令行简单解释器
    SpringMVC
  • 原文地址:https://blog.csdn.net/qq_34716929/article/details/126281095