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

相关推荐
P.H. Infinity21 分钟前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天24 分钟前
java的threadlocal为何内存泄漏
java
caridle36 分钟前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
^velpro^41 分钟前
数据库连接池的创建
java·开发语言·数据库
苹果醋31 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
秋の花1 小时前
【JAVA基础】Java集合基础
java·开发语言·windows
小松学前端1 小时前
第六章 7.0 LinkList
java·开发语言·网络
Wx-bishekaifayuan1 小时前
django电商易购系统-计算机设计毕业源码61059
java·spring boot·spring·spring cloud·django·sqlite·guava
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
全栈开发圈1 小时前
新书速览|Java网络爬虫精解与实践
java·开发语言·爬虫