1. 程式人生 > >【Java】echarts,highcharts中多個y軸對應的一個x軸的數量的Java對應排序程式碼(一個key下的多個value值對應key的位置)

【Java】echarts,highcharts中多個y軸對應的一個x軸的數量的Java對應排序程式碼(一個key下的多個value值對應key的位置)

1.首先,可以很輕鬆的從後臺資料庫獲取多個list。list如下:

2.根據其中一個的list的排序,獲取出x軸的陣列。(echarts,highcharts的X,Y軸一般為陣列) 

 

Java程式碼:

String x[] = new String[list.size()];
int y[] = new int[list.size()];

for (int i = 0; i < list.size(); i++) {
            Map map = (Map) list.get(i);
            x[i] = "" + map.get("KFYQ");
            y[i] = Integer.parseInt("" + map.get("SUM"));
        }

 3.關鍵是第二個,第三個的排序要對應第一個已經排好序的陣列。這裡利用了map的一個containsKey方法。

    3.1把原來的List<Map>轉變成Map.

Map mapList1 = new LinkedHashMap();
        for (int i = 0; i < list1.size(); i++) {
            Map map = (Map) list1.get(i);
            mapList1.put(map.get("KFYQ"), map.get("SUM"));
        }

    3.2 用了map的containsKey使之與排好序的第一個X軸的值對應。

int y1[] = new int[list.size()];

for (int i = 0; i < x.length; i++) {
            if(mapList1.containsKey(x[i])){
                y1[i] = Integer.parseInt(""+mapList1.get(x[i]));
            }else {
                y1[i] = 0;
            }
        }

4.轉成json,前臺解析

 5.效果圖

-------------------------------------------------------------------------------------------------------------------------------------------------------

完整Java程式碼:

public String getSQFB() throws Exception {
        Map mapAll = new HashMap();

        //獲取三區數量的List<Map>
        List list = testMapper.getSQFB();
        List list1 = testMapper.getSQFB1();
        List list2 = testMapper.getSQFB2();

        //hightcharts需要的資料格式
        String x[] = new String[list.size()];
        int y[] = new int[list.size()];
        int y1[] = new int[list.size()];
        int y2[] = new int[list.size()];

        //確定x軸的順序
        for (int i = 0; i < list.size(); i++) {
            Map map = (Map) list.get(i);
            x[i] = "" + map.get("KFYQ");
            y[i] = Integer.parseInt("" + map.get("SUM"));
        }

        //把原來的List<Map>轉變成Map。key為X軸的值,value為Y軸值。
        Map mapList1 = new LinkedHashMap();
        for (int i = 0; i < list1.size(); i++) {
            Map map = (Map) list1.get(i);
            mapList1.put(map.get("KFYQ"), map.get("SUM"));
        }
        //用containsKey使之與排好序的第一個X軸的值對應。
        for (int i = 0; i < x.length; i++) {
            if (mapList1.containsKey(x[i])) {
                y1[i] = Integer.parseInt("" + mapList1.get(x[i]));
            } else {
                y1[i] = 0;
            }
        }

        //同上
        Map mapList2 = new LinkedHashMap();
        for (int i = 0; i < list2.size(); i++) {
            Map map = (Map) list2.get(i);
            mapList2.put(map.get("KFYQ"), map.get("SUM"));
        }
        for (int i = 0; i < x.length; i++) {
            if (mapList2.containsKey(x[i])) {
                y2[i] = Integer.parseInt("" + mapList2.get(x[i]));
            } else {
                y2[i] = 0;
            }
        }
        
        mapAll.put("x", x);
        mapAll.put("y", y);
        mapAll.put("y1", y1);
        mapAll.put("y2", y2);
        Gson gson = new Gson();
        return gson.toJson(mapAll);
    }