個人工作中知識點記錄,以便加深影響
1.需求:需要實現一個echarts疊堆報表,如圖:
所需格式:
// 指定圖表的配置項和數據 var option = { toolbox: { show : true, feature : { magicType : {show: true, type: ['line', 'bar']}, dataView : {show: false, readOnly: false}, restore : {show: false}, saveAsImage : {show: true}, myTool : { show : true, title : 'excel下載', icon : 'image://../../resource/images/op/base/exl.png', onclick : function (){ location.href=encodeURI(getRootPath_web()+"/opMain/exportExcel.xhtml?title=人臉識別業務量&colnames=公司名稱,50分以下,50(含)-55分,55(含)-60分,60(含)-65分,65(含)-70分,70(含)-75分,75(含)-80分,80(含)-85分,85(含)以上,其它&datalist="+JSON.stringify(exlArrs.sort())); } } } }, tooltip : { trigger: 'axis' }, grid: { bottom: 200, }, xAxis : [ { type : 'category', axisLabel: {rotate: 30,interval:-1}, flag: dataObj.flag, data : dataObj.gsName, fid: dataObj.gsId } ], yAxis : [ { type : 'value' } ], dataZoom: [ { show: true, showDetail: false, bottom:'50', start: 1, end: 50 }, ], series : [ { name: '50分以下', type: 'bar', stack: '總量', /*label: { normal: { show: true, position: 'insideRight' } },*/ data: dataObj.one }, { name: '50(含)-55分', type: 'bar', stack: '總量', /*label: { normal: { show: true, position: 'insideRight' } },*/ data: dataObj.two, }, { name: '55(含)-60分', type: 'bar', stack: '總量', /* label: { normal: { show: true, position: 'insideRight' } },*/ data: dataObj.three }, { name: '60(含)-65分', type: 'bar', stack: '總量', /* label: { normal: { show: true, position: 'insideRight' } },*/ data: dataObj.four }, { name: '65(含)-70分', type: 'bar', stack: '總量', /* label: { normal: { show: true, position: 'insideRight' } },*/ data: dataObj.five }, { name: '70(含)-75分', type: 'bar', stack: '總量', /*label: { normal: { show: true, position: 'insideRight' } },*/ data: dataObj.six }, { name: '75(含)-80分', type: 'bar', stack: '總量', /* label: { normal: { show: true, position: 'insideRight' } },*/ data: dataObj.seven }, { name: '80(含)-85分', type: 'bar', stack: '總量', /*label: { normal: { show: true, position: 'insideRight' } },*/ data: dataObj.eight }, { name: '85(含)以上', type: 'bar', stack: '總量', /* label: { normal: { show: true, position: 'insideRight' } },*/ data: dataObj.nine }, { name: '其它', type: 'bar', stack: '總量', /* label: { normal: { show: true, position: 'insideRight' } },*/ data: dataObj.other } ] };
后台拿到的數據,如下:
[{gsId=8022, gsName=西北分公司, type=0, yybcount=4}, {gsId=8016, gsName=揚州分公司, type=0, yybcount=4}, {gsId=8008, gsName=南京分公司, type=0, yybcount=14409}, {gsId=8008, gsName=南京分公司, type=9, yybcount=107}, {gsId=8008, gsName=南京分公司, type=1, yybcount=45}, {gsId=8008, gsName=南京分公司, type=8, yybcount=8}, {gsId=8008, gsName=南京分公司, type=7, yybcount=5}, {gsId=8008, gsName=南京分公司, type=6, yybcount=4}, {gsId=8008, gsName=南京分公司, type=3, yybcount=3}, {gsId=8008, gsName=南京分公司, type=5, yybcount=1}, {gsId=8008, gsName=南京分公司, type=2, yybcount=1}, {gsId=8008, gsName=南京分公司, type=4, yybcount=1}, {gsId=8002, gsName=蘇州分公司, type=0, yybcount=5}, {gsId=8001, gsName=上海分公司, type=0, yybcount=4}, {gsId=8000, gsName=總部特殊業務管理, type=0, yybcount=38616}, {gsId=8000, gsName=總部特殊業務管理, type=9, yybcount=133}, {gsId=8000, gsName=總部特殊業務管理, type=1, yybcount=72}, {gsId=8000, gsName=總部特殊業務管理, type=8, yybcount=46}, {gsId=8000, gsName=總部特殊業務管理, type=3, yybcount=42}, {gsId=8000, gsName=總部特殊業務管理, type=7, yybcount=40}, {gsId=8000, gsName=總部特殊業務管理, type=6, yybcount=32}, {gsId=8000, gsName=總部特殊業務管理, type=5, yybcount=28}, {gsId=8000, gsName=總部特殊業務管理, type=2, yybcount=15}, {gsId=8000, gsName=總部特殊業務管理, type=4, yybcount=2}, {gsId=1, gsName=華泰證券, type=0, yybcount=36}]
需要轉換組裝的格式,如下:
{ gsId=[8000, 8008, 1, 8002, 8022, 8016, 8001], gsName=[總部特殊業務管理, 南京分公司, 華泰證券, 蘇州分公司, 西北分公司, 揚州分公司, 上海分公司], flag=[1, 1, 1, 1, 1, 1, 1], other=[38616, 14409, 36, 5, 4, 4, 4] one=[72, 45, 0, 0, 0, 0, 0], two=[15, 1, 0, 0, 0, 0, 0], three=[42, 3, 0, 0, 0, 0, 0], four=[2, 1, 0, 0, 0, 0, 0], five=[28, 1, 0, 0, 0, 0, 0], six=[32, 4, 0, 0, 0, 0, 0], seven=[40, 5, 0, 0, 0, 0, 0], eight=[46, 8, 0, 0, 0, 0, 0], nine=[133, 107, 0, 0, 0, 0, 0] }
思路分析:
List<type1> type1 List<type2> ... for(1-10){ object obj = list.get(gsId.get(i),1);//循環9個固定閥值,當gsId,類型相等則返回該對象,否則返回null
if(null !=obj){
type1.add(obj.getYybcount());
}else{
type1.add("0");
}
}
公司名稱 50分以下 50(含)-55分 55(含)-60分 60(含)-65分 65(含)-70分 70(含)-75分 75(含)-80分 80(含)-85分 85(含)以上 其它 華泰證券 0 0 0 0 0 0 0 0 0 36 總部特殊業務管理 72 15 42 2 28 32 40 46 133 38616 上海分公司 0 0 0 0 0 0 0 0 0 4 蘇州分公司 0 0 0 0 0 0 0 0 0 5 南京分公司 45 1 3 1 1 4 5 8 107 14409 揚州分公司 0 0 0 0 0 0 0 0 0 4 西北分公司 0 0 0 0 0 0 0 0 0 4
實現代碼,如下:
@SuppressWarnings("unchecked") public static Map<String, Object> assemReturnMap(Map<String, Object> returnMap, List<Map<String, String>> list,String flag) { List<Map<String, String>> lists = mergeList(list); ComparatorMap comparator=new ComparatorMap(); Collections.sort(lists, comparator); //組裝name集合 List<String> gsNameList = new ArrayList<String>(); List<String> gsIdList = new ArrayList<String>(); List<String> flags = new ArrayList<String>(); for(Map<String,String> mp:lists){ String gsName = mp.get("gsName"); if(!gsNameList.contains(gsName)){ gsNameList.add(gsName); } String gsId = String.valueOf(mp.get("gsId")); if(!gsIdList.contains(gsId)){ gsIdList.add(gsId); if("0".equals(flag)){ flags.add("0"); } if("1".equals(flag)){ flags.add("1"); } } } List<String> yybcountListOther = new ArrayList<String>(); List<String> yybcountListOne = new ArrayList<String>(); List<String> yybcountListTwo = new ArrayList<String>(); List<String> yybcountListThree = new ArrayList<String>(); List<String> yybcountListFour = new ArrayList<String>(); List<String> yybcountListFive = new ArrayList<String>(); List<String> yybcountListSix = new ArrayList<String>(); List<String> yybcountListSeven = new ArrayList<String>(); List<String> yybcountListEight = new ArrayList<String>(); List<String> yybcountListNine = new ArrayList<String>(); for (int i = 0; i < 10; i++) { for (String gsId : gsIdList) { Map<String, String> obj = getGsInfoByGsIdAndType(list, gsId, i); if (i == 0) { if (null != obj) { yybcountListOther.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0"); } else { yybcountListOther.add("0"); } } if (i == 1) { if (null != obj) { yybcountListOne.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0"); } else { yybcountListOne.add("0"); } } if (i == 2) { if (null != obj) { yybcountListTwo.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0"); } else { yybcountListTwo.add("0"); } } if (i == 3) { if (null != obj) { yybcountListThree.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0"); } else { yybcountListThree.add("0"); } } if (i == 4) { if (null != obj) { yybcountListFour.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0"); } else { yybcountListFour.add("0"); } } if (i == 5) { if (null != obj) { yybcountListFive.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0"); } else { yybcountListFive.add("0"); } } if (i == 6) { if (null != obj) { yybcountListSix.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0"); } else { yybcountListSix.add("0"); } } if (i == 7) { if (null != obj) { yybcountListSeven.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0"); } else { yybcountListSeven.add("0"); } } if (i == 8) { if (null != obj) { yybcountListEight.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0"); } else { yybcountListEight.add("0"); } } if (i == 9) { if (null != obj) { yybcountListNine.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0"); } else { yybcountListNine.add("0"); } } } } returnMap.put("gsId", gsIdList); returnMap.put("gsName", gsNameList); returnMap.put("flag", flags); returnMap.put("one", yybcountListOne); returnMap.put("two", yybcountListTwo); returnMap.put("three", yybcountListThree); returnMap.put("four", yybcountListFour); returnMap.put("five", yybcountListFive); returnMap.put("six", yybcountListSix); returnMap.put("seven", yybcountListSeven); returnMap.put("eight", yybcountListEight); returnMap.put("nine", yybcountListNine); returnMap.put("other", yybcountListOther); returnMap.put("list", list); return returnMap; }
/** * 通過公司ID和類型獲取公司信息 * @param list * @param gsId * @param type * @return */ private static Map<String, String> getGsInfoByGsIdAndType(List<Map<String, String>> list, String gsId, int type) { for (Map<String, String> obj : list) { if (gsId.equals(String.valueOf(obj.get("gsId"))) && String.valueOf(type).equals(String.valueOf(obj.get("type")))) { return obj; } } return null; }
/** * 合並重復數據 * @param oneList * @return */ public static List<Map<String, String>> mergeList(List<Map<String, String>> oneList) { List<Map<String,String>> list = new ArrayList<Map<String,String>>(); for (int i = 0; i < oneList.size(); i++) { String gsId = String.valueOf(oneList.get(i).get("gsId")); int flag = 0; for (int j = 0; j < list.size(); j++) { String gsId_ = String.valueOf(list.get(j).get("gsId")); if (null!=gsId&&null!=gsId_&&!"".equals(gsId)&&!"".equals(gsId_)&&gsId.equals(gsId_)) { int sum = Integer.parseInt(String.valueOf(oneList.get(i).get("yybcount"))) + Integer.parseInt(String.valueOf(list.get(j).get("yybcount"))); list.get(j).put("yybcount", sum + ""); flag = 1; continue; } } if (flag == 0) { list.add(oneList.get(i)); } } return list; }