线性回归图

    @PostMapping("/univariateLinRegression/importData")
    public AjaxResult univariateLinRegression(@RequestBody ChartDateTto chartDateTto) throws Exception {

        String chartValue = chartDateTto.getChartValue();
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);
        JSONObject jsonObject = JSON.parseObject(chartValue);
        System.out.println("传入的数据: " + jsonObject + " 传入的数据");

        List<List<String>> getdata = extractDataFromList(chartDateTto.getDataList());

        if (CollectionUtils.isEmpty(getdata)) {
            return AjaxResult.error("请填充数据");
        }

        List<String> groupName = new ArrayList<>();
        List<List<List<String>>> newList = new ArrayList<>();
        List<List<String>> list = new ArrayList<>();

        for (int i = 1; i < getdata.size(); i++) {
            if (i == getdata.size() - 1) {
                List<String> sdata = getdata.get(i);
                list.add(sdata);
                newList.add(list);
            } else if (i == 1) {
                List<String> gName = getdata.get(i);
                groupName.add(gName.get(0));
            } else {
                if (getdata.get(i).size() == 1) {
                    List<String> gName = getdata.get(i);
                    groupName.add(gName.get(0));
                    newList.add(list);
                    list = new ArrayList<>();
                } else {
                    List<String> sdata = getdata.get(i);
                    ArrayList<String> newList1 = new ArrayList<>();
                    for (int i1 = 0; i1 < sdata.size(); i1++) {
                        String s = sdata.get(i1);
                        if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                            newList1.add(s);
                        }
                    }
                    list.add(newList1);
                }
            }
        }

        ArrayList<Map> maps = new ArrayList<>();

        List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
        JSONObject json = series.get(0);

        ArrayList<JSONObject> jsonObjects = new ArrayList<>();

        for (int i = 0; i < newList.size(); i++) {
            List<List<String>> lists = newList.get(i);
            List<List<Object>> nonStringLists = new ArrayList<>();

            for (List<String> sublist : lists) {
                List<Object> nonStringSublist = new ArrayList<>();
                for (String item : sublist) {
                    try {
                        // 尝试将字符串转换为数字
                        nonStringSublist.add(Double.parseDouble(item));
                    } catch (NumberFormatException e) {
                        // 如果转换失败,保留原字符串
                        nonStringSublist.add(item);
                    }
                }
                nonStringLists.add(nonStringSublist);
            }

            HashMap<String, Object> map = new HashMap<>();
            map.put("source", nonStringLists);
            maps.add(map);

            double[] x = new double[lists.size()];
            double[] y = new double[lists.size()];

            for (int i1 = 0; i1 < lists.size(); i1++) {
                List<String> sources = lists.get(i1);
                for (int i2 = 0; i2 < sources.size(); i2++) {
                    x[i1] = Double.parseDouble(sources.get(0));
                    y[i1] = Double.parseDouble(sources.get(1));
                }
            }
            Map service = ExcelDataUtils.getService1(x, y);
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.putAll(json);
            jsonObject1.put("datasetIndex", i);
            jsonObject1.put("name", groupName.get(i));
            JSONObject markLine1 = (JSONObject) jsonObject1.get("markLine");
            JSONObject markLine = new JSONObject();
            markLine.putAll(markLine1);
            JSONObject label1 = (JSONObject) markLine.get("label");
            JSONObject label = new JSONObject();
            label.putAll(label1);
            label.put("formatter", service.get("formatter"));
            markLine.put("label", label);

            JSONObject tooltip1 = (JSONObject) markLine.get("tooltip");
            JSONObject tooltip = new JSONObject();
            tooltip.putAll(tooltip1);
            tooltip.put("formatter", service.get("formatter"));
            markLine.put("tooltip", tooltip);

            List<JSONObject> data = (List<JSONObject>) markLine.get("data");
            List<JSONObject> data1 = (List<JSONObject>) data.get(0);
            List<JSONObject> jsonObjects1 = new ArrayList<>();
            List<List<JSONObject>> jsonObjects2 = new ArrayList<>();
            for (int i1 = 0; i1 < data1.size(); i1++) {
                JSONObject jsonObject2 = data1.get(i1);
                JSONObject jsonObject3 = new JSONObject();
                jsonObject3.putAll(jsonObject2);
                if (i1 == 0) {
                    jsonObject3.put("coord", service.get("min"));
                } else {
                    jsonObject3.put("coord", service.get("max"));
                }
                jsonObjects1.add(jsonObject3);
            }
            jsonObjects2.add(jsonObjects1);
            markLine.put("data", jsonObjects2);
            jsonObject1.put("markLine", markLine);
            jsonObjects.add(jsonObject1);
        }

        // 获取 legend 数组并处理
        JSONArray legendArray = jsonObject.getJSONArray("legend");
        if (legendArray != null && !legendArray.isEmpty()) {
            JSONObject legend = legendArray.getJSONObject(0); // 获取第一个 legend 对象
            ArrayList<Map> maps1 = new ArrayList<>();
            for (int i = 0; i < groupName.size(); i++) {
                Map map = new HashMap<>();
                map.put("name", groupName.get(i));
                maps1.add(map);
            }
            legend.put("data", maps1);
            jsonObject.put("legend", legendArray);
        }

        jsonObject.put("series", jsonObjects);
        jsonObject.put("dataset", maps);
        System.out.println(JSON.toJSONString(jsonObject));

        ChartSmartVo chartSmartVo = new ChartSmartVo();

        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));

        List<ChartSmartVo> newLista = new ArrayList<>();
        newLista.add(chartSmartVo);

        return AjaxResult.success(newLista);


    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值