【Java 动态数据统计图】动态数据统计思路案例(动态,排序,动态数组(重点推荐))七(129)

需求:前端根据后端的返回数据:画统计图;

说明:

1.X轴为地域,Y轴为地域出现的次数;

  1. 动态展示(有地域展示,没有不展示,且高低排序)

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接口测试

相关推荐
m0_548514771 分钟前
前端Pako.js 压缩解压库 与 Java 的 zlib 压缩与解压 的互通实现
java·前端·javascript
坊钰29 分钟前
【Java 数据结构】移除链表元素
java·开发语言·数据结构·学习·链表
chenziang134 分钟前
leetcode hot100 LRU缓存
java·开发语言
会说法语的猪40 分钟前
springboot实现图片上传、下载功能
java·spring boot·后端
码农老起40 分钟前
IntelliJ IDEA 基本使用教程及Spring Boot项目搭建实战
java·ide·intellij-idea
m0_748239831 小时前
基于web的音乐网站(Java+SpringBoot+Mysql)
java·前端·spring boot
时雨h1 小时前
RuoYi-ue前端分离版部署流程
java·开发语言·前端
麒麟而非淇淋1 小时前
Day13 苍穹外卖项目 工作台功能实现、Apache POI、导出数据到Excel表格
java
小爬虫程序猿1 小时前
利用Java爬虫获取速卖通(AliExpress)商品详情的详细指南
java·开发语言·爬虫