需求:前端根据后端的返回数据:画统计图;
说明:
1.X轴为地域,Y轴为地域出现的次数;
- 动态展示(有地域展示,没有不展示,且高低排序)
Demo案例:
java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class day22 {
public static void main(String[] args) {
List<Map<String,Object>> list = new ArrayList<>();
Map<String,Object> map1 = new HashMap<>();
map1.put("region","西安");
map1.put("num",1.0);
Map<String,Object> map2 = new HashMap<>();
map2.put("region","上海");
map2.put("num",2.0);
Map<String,Object> map3 = new HashMap<>();
map3.put("region","北京");
map3.put("num",3.0);
Map<String,Object> map4 = new HashMap<>();
map4.put("region","西安");
map4.put("num",1.5);
Map<String,Object> map5 = new HashMap<>();
map5.put("region","西安");
map5.put("num",2.5);
Map<String,Object> map6 = new HashMap<>();
map6.put("region","北京");
map6.put("num",4.0);
Map<String,Object> map7 = new HashMap<>();
map7.put("region","成都");
map7.put("num",1.0);
Map<String,Object> map8 = new HashMap<>();
map8.put("region","上海");
map8.put("num",2.0);
Map<String,Object> map9 = new HashMap<>();
map9.put("region","上海");
map9.put("num",2.5);
Map<String,Object> map10 = new HashMap<>();
map10.put("region","上海");
map10.put("num",2.0);
Map<String,Object> map11 = new HashMap<>();
map11.put("region","长沙");
map11.put("num",2.0);
Map<String,Object> map12 = new HashMap<>();
map12.put("region","苏州");
map12.put("num",2.0);
Map<String,Object> map13 = new HashMap<>();
map13.put("region","江西");
map13.put("num",2.0);
Map<String,Object> map14 = new HashMap<>();
map14.put("region","长沙");
map14.put("num",2.0);
Map<String,Object> map15 = new HashMap<>();
map15.put("region","苏州");
map15.put("num",2.0);
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
list.add(map6);
list.add(map7);
list.add(map8);
list.add(map9);
list.add(map10);
list.add(map11);
list.add(map12);
list.add(map13);
list.add(map14);
list.add(map15);
// Set:获取数组中的所有数据:去重
Set<String> hashSet = new HashSet<>();
for (int i = 0; i < list.size(); i++) {
String region = String.valueOf(list.get(i).get("region"));
hashSet.add(region);
}
String[] regionArr = new String[hashSet.size()];
int[] valueSort = new int[hashSet.size()];
// Set转数组
if(list.size() > 0){
regionArr = hashSet.toArray(new String[hashSet.size()]);
}
// 根据数组计算每个元素的数量;
for (int i = 0; i < regionArr.length; i++) {
for (int j = 0; j < list.size(); j++) {
String s = String.valueOf(list.get(j).get("region"));
if(regionArr[i].equals(s)){
valueSort[i] = valueSort[i] + 1;
}
}
}
// 排序;
BubblSortUtils.bubbleSortDescMultiple(valueSort,regionArr);
List<Map<String,Object>> listFinal= new ArrayList<>();
for (int i = 0; i < regionArr.length; i++) {
Map<String,Object> map = new HashMap<>();
map.put("title",regionArr[i]);
map.put("value",valueSort[i]);
listFinal.add(map);
}
System.out.println("listFinal:"+listFinal);
}
}
测试输出:
java
listFinal:[{title=上海, value=4}, {title=西安, value=3}, {title=苏州, value=2}, {title=北京, value=2}, {title=长沙, value=2}, {title=江西, value=1}, {title=成都, value=1}]
案例二:
java
public List<Map<String,Object>> getRevealData(HttpServletRequest request, Map<String, Object> paramMap) {
List<Map<String,Object>> list = getCountData(request,paramMap);
if(list.size() == 0){
return null;
}
// 获取数据的类型;去重
Set<String> hashSet = new HashSet<>();
for (int i = 0; i < list.size(); i++) {
String waysOfProblemReveal = String.valueOf(list.get(i).get("waysOfProblemReveal"));
hashSet.add(waysOfProblemReveal);
}
String[] waysOfProblemArr = new String[hashSet.size()];
int[] valueSort = new int[hashSet.size()];
if(list.size() > 0){
waysOfProblemArr = hashSet.toArray(new String[hashSet.size()]);
}
// 根据数组,计算每个元素的个数;
for (int i = 0; i < waysOfProblemArr.length; i++) {
for (int j = 0; j < list.size(); j++) {
String s = String.valueOf(list.get(j).get("waysOfProblemReveal"));
if(waysOfProblemArr[i].equals(s)){
valueSort[i] = valueSort[i] + 1;
}
}
}
// 数组排序;
BubblSortUtils.bubbleSortDescMultiple(valueSort,waysOfProblemArr);
List<Map<String,Object>> listFinal= new ArrayList<>();
for (int i = 0; i < waysOfProblemArr.length; i++) {
Map<String,Object> map = new HashMap<>();
map.put("title",waysOfProblemArr[i]);
map.put("value",valueSort[i]);
map.put("sortValue",waysOfProblemArr.length-i);
listFinal.add(map);
}
return listFinal;
}
postman接口测试 :