java初步实现最大余额法,自己使用过程中没发现问题(有没有其它bug,还有待验证)
阅读之前请先查看前一篇 《百度echarts饼图百分比的计算规则---最大余额法》,保证理解了什么是最大余额方法。
代码实现如下:
package com.psmp_mobile.model.echarts;
import java.util.*;
/**
* @Title: java模拟实现最大余额方法
* @Description: ${todo}
* @Version 1.0
*/
public class PercentageSortUtil {
/**
* 对数据进行最大余额方法处理
* (最终值保留两位小数)
* @param list
* @return
*/
public static ListgetPercentageSort(List list){
List> percentRsList =new ArrayList<>();
Double sum =0D;
for (int i =0; i < list.size(); i++) {
EchartsDatagrid ed = list.get(i);
String percent = ed.getThisPercentage().toString();
// 小数点的位置
int index =getIndex(percent, '.');
if(percent.length()>5){
Map map =new HashMap<>();
String percentRs = percent.substring(0,index)+percent.substring(index, index+3);// 截取小数点后两位(不采用四舍五入)
String percentRsTmp = percent.substring(index+3, percent.length());// 小数点两位以后的所有
map.put("index", i);
map.put("percentRs", percentRs);
map.put("percentRsTmp", percentRsTmp);
percentRsList.add(map);
sum+=Double.valueOf(percentRs);
}
}
// 筛选出需要+0.01的数据条数
Double rs = (100-sum)*100;
int rsInt = (int)Math.round(rs);
// 对list进行排序 筛选出
mapSort(percentRsList);
int count =0;
for (int i = percentRsList.size()-1; i >=0; i--) {
// 不做处理将值对应到原来list的位置
if( (count +1) > rsInt ){
int index = Integer.valueOf(percentRsList.get(i).get("index").toString());
Double percentRs = Double.valueOf(percentRsList.get(i).get("percentRs").toString());
list.get(index).setThisPercentage(percentRs);
}else{
// 将原来的值 +0.01 后对应到原来list的位置
int index = Integer.valueOf(percentRsList.get(i).get("index").toString());
Double percentRs = Double.valueOf(percentRsList.get(i).get("percentRs").toString());
Double val = percentRs+0.01D;
Double str = (double)Math.round(val *100)/100;
list.get(index).setThisPercentage(str);
}
count +=1;
}
return list;
}
/**
* 计算小数点的位置
* @param str
* @param ch
* @return
*/
public static int getIndex(String str, char ch) {
for (int i =0; i < str.length(); i++) {
if (str.charAt(i) == ch) {
return i;
}
}
return -1;
}
/**
* list<Map<String, Object>> 进行排序
* @param list
*/
public static void mapSort(List> list){
Collections.sort(list, new Comparator>() {
public int compare(Map o1, Map o2) {
String v1 = o1.get("percentRsTmp").toString();// v1是从你list里面拿出来的一个指定的值
String v2 = o2.get("percentRsTmp").toString(); // v2是从你list里面拿出来的第二个指定的值
// 从name1与name2取字段值然后比较,大于返回1,等于返回0,小于返回-1
if(!v1.equals(v2)){
return v1.compareTo(v2);
}else{
// 如果两个值相等的话还按原来的顺序,不做排序,返回-1处理。
return -1;
}
}
});
}
public static void main(String[] args) {
// 根据实际业务构造较为真实的数据格式
List list =new ArrayList<>();
EchartsDatagrid ed1 =new EchartsDatagrid();
ed1.setThisPercentage((19*100.0/117));
EchartsDatagrid ed2 =new EchartsDatagrid();
ed2.setThisPercentage((16*100.0/117));
EchartsDatagrid ed3 =new EchartsDatagrid();
ed3.setThisPercentage((16*100.0/117));
EchartsDatagrid ed4 =new EchartsDatagrid();
ed4.setThisPercentage((12*100.0/117));
EchartsDatagrid ed5 =new EchartsDatagrid();
ed5.setThisPercentage((11*100.0/117));
EchartsDatagrid ed6 =new EchartsDatagrid();
ed6.setThisPercentage((11*100.0/117));
EchartsDatagrid ed7 =new EchartsDatagrid();
ed7.setThisPercentage((10*100.0/117));
EchartsDatagrid ed8 =new EchartsDatagrid();
ed8.setThisPercentage((8*100.0/117));
EchartsDatagrid ed9 =new EchartsDatagrid();
ed9.setThisPercentage((7*100.0/117));
EchartsDatagrid ed10 =new EchartsDatagrid();
ed10.setThisPercentage((7*100.0/117));
list.add(ed1);
list.add(ed2);
list.add(ed3);
list.add(ed4);
list.add(ed5);
list.add(ed6);
list.add(ed7);
list.add(ed8);
list.add(ed9);
list.add(ed10);
getPercentageSort(list);
}
}
完结撒花,如有不明白的地方,可加QQ: 592998695