@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);
}
线性回归图
于 2024-07-25 13:53:08 首次发布