@ApiOperation("导入盒须图数据4")
@PostMapping("/importData4")
@Transactional(rollbackFor = Exception.class)
public AjaxResult importData4(@RequestBody ChartDateTto chartDateTto) throws Exception {
List<List<String>> getdata = extractDataFromListNullString(chartDateTto.getDataList());
int sizeLine = getdata.size();
System.out.println("我是有多少列" + sizeLine);
for (List<String> getdatum : getdata) {
System.out.println(getdatum);
}
ArrayList<String> legendNameList = new ArrayList<>();
List<String> strings = getdata.get(0);
int previousNonZeroIndex = -1;
ArrayList<Object> arrayList = new ArrayList<>();
for (int i = 1; i < strings.size(); i++) {
String value = strings.get(i);
if (!value.equals("0")) {
legendNameList.add(value);
}
if (!value.equals("0")) {
if (previousNonZeroIndex != -1) {
int columnsBetween = i - previousNonZeroIndex;
arrayList.add(columnsBetween);
}
previousNonZeroIndex = i;
}
}
for (String aas : legendNameList) {
System.out.println("我X轴的名称" + aas);
}
ArrayList<String> xAxisNameList = new ArrayList<>();
List<String> dataRow = getdata.get(1);
int lastNonZeroIndex = -1;
ArrayList<Object> columnGaps = new ArrayList<>();
for (int i = 1; i < dataRow.size(); i++) {
String currentValue = dataRow.get(i);
if (!currentValue.equals("0")) {
xAxisNameList.add(currentValue);
}
if (!currentValue.equals("0")) {
if (lastNonZeroIndex != -1) {
int columnsBetween = i - lastNonZeroIndex;
columnGaps.add(columnsBetween);
}
lastNonZeroIndex = i;
}
}
getdata.remove(0);
getdata.remove(0);
Object arr0 = arrayList.get(0);
Integer integerValue = (Integer) arr0;
int line = integerValue;
System.out.println("我是中间隔的列数" + line);
ArrayList<String> selectedXAxisNames = new ArrayList<>();
for (int i = 0; i < Math.min(line, xAxisNameList.size()); i++) {
selectedXAxisNames.add(xAxisNameList.get(i));
}
for (String name : selectedXAxisNames) {
System.out.println("提取的图例名称: " + name);
}
int number = sizeLine / line;
List<List<Double>> columnData = new ArrayList<>();
if (!getdata.isEmpty()) {
int numColumns = getdata.get(0).size() - 1;
for (int i = 0; i < numColumns; i++) {
columnData.add(new ArrayList<>());
}
}
ArrayList<String> seriesName = new ArrayList<>();
for (int rowIndex = 0; rowIndex < getdata.size(); rowIndex++) {
List<String> row = getdata.get(rowIndex);
String firstColumnValue = row.remove(0);
seriesName.add(firstColumnValue);
for (int colIndex = 0; colIndex < row.size(); colIndex++) {
String cellValue = row.get(colIndex);
try {
double numericValue = Double.parseDouble(cellValue);
columnData.get(colIndex).add(numericValue);
} catch (NumberFormatException e) {
System.err.println("Invalid number format: " + cellValue);
}
}
}
ArrayList<Object> arrayLists = new ArrayList<>();
for (int colIndex = 0; colIndex < columnData.size(); colIndex++) {
arrayLists.add(columnData.get(colIndex));
}
List<List<Object>> groupedLists = splitList(arrayLists, line);
for (int i = 0; i < groupedLists.size(); i++) {
System.out.println("Group " + (i + 1) + ": " + groupedLists.get(i));
}
String chartValue = chartDateTto.getChartValue();
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(chartValue);
ArrayNode dataset = (ArrayNode) rootNode.path("dataset");
JsonNode firstSourceNode = null;
int firstSourceNodeIndex = -1;
for (int i = 0; i < dataset.size(); i++) {
JsonNode node = dataset.get(i);
if (node.has("source")) {
if (firstSourceNode == null) {
firstSourceNode = node;
firstSourceNodeIndex = i;
} else {
dataset.remove(i);
i--;
}
}
}
int requiredDatasets = groupedLists.size();
int insertIndex = firstSourceNodeIndex + 1;
for (int i = 1; i < requiredDatasets; i++) {
dataset.insert(insertIndex, firstSourceNode.deepCopy());
insertIndex++;
}
int groupIndex = 0;
for (JsonNode node : dataset) {
if (node.has("source")) {
ArrayNode sourceArray = (ArrayNode) node.get("source");
sourceArray.removeAll();
List<Object> groupData = groupedLists.get(groupIndex);
for (Object item : groupData) {
ArrayNode arrayNode = objectMapper.createArrayNode();
((List<Double>) item).forEach(arrayNode::add);
sourceArray.add(arrayNode);
}
groupIndex++;
}
}
ArrayNode legend = (ArrayNode) rootNode.path("legend");
ArrayNode legendDataArray = (ArrayNode) legend.get(0).get("data");
JsonNode firstLegendData = legendDataArray.get(0);
legendDataArray.removeAll();
legendDataArray.add(firstLegendData);
for (int i = 0; i < selectedXAxisNames.size(); i++) {
JsonNode newLegendData = firstLegendData.deepCopy();
((ObjectNode) newLegendData).put("name", selectedXAxisNames.get(i));
legendDataArray.add(newLegendData);
}
legendDataArray.remove(0);
ArrayNode series = (ArrayNode) rootNode.path("series");
JsonNode firstSeries = series.get(0);
series.removeAll();
for (int i = 0; i < selectedXAxisNames.size(); i++) {
JsonNode newSeries = firstSeries.deepCopy();
((ObjectNode) newSeries).put("name", selectedXAxisNames.get(i));
((ObjectNode) newSeries).put("datasetIndex", i);
series.add(newSeries);
}
ArrayNode datasetArray = (ArrayNode) rootNode.path("dataset");
JsonNode firstFromDatasetIndexNode = null;
int firstFromDatasetIndexNodeIndex = -1;
for (int i = 0; i < datasetArray.size(); i++) {
JsonNode node = datasetArray.get(i);
if (node.has("fromDatasetIndex")) {
if (firstFromDatasetIndexNode == null) {
firstFromDatasetIndexNode = node;
firstFromDatasetIndexNodeIndex = i;
} else {
datasetArray.remove(i);
i--;
}
}
}
int insertIndexa = firstFromDatasetIndexNodeIndex + 1;
for (int i = 1; i < line; i++) {
JsonNode newFromDatasetIndexNode = firstFromDatasetIndexNode.deepCopy();
((ObjectNode) newFromDatasetIndexNode).put("fromDatasetIndex", i);
datasetArray.insert(insertIndexa, newFromDatasetIndexNode);
insertIndexa++;
}
ArrayNode datasetArrays = (ArrayNode) rootNode.path("dataset");
int maxFromDatasetIndex = -1;
for (JsonNode node : dataset) {
if (node.has("fromDatasetIndex")) {
int fromDatasetIndex = node.get("fromDatasetIndex").asInt();
if (fromDatasetIndex > maxFromDatasetIndex) {
maxFromDatasetIndex = fromDatasetIndex;
}
}
}
if (maxFromDatasetIndex != -1) {
ArrayNode seriesa = (ArrayNode) rootNode.path("series");
int datasetIndex = maxFromDatasetIndex + 1;
for (JsonNode seriesNode : seriesa) {
((ObjectNode) seriesNode).put("datasetIndex", datasetIndex);
datasetIndex++;
}
}
System.out.println("更新后的 dataset: " + dataset.toString());
String updatedJson = rootNode.toString();
System.out.println("完整的 JSON 数据: " + updatedJson);
ArrayList<Object> newList = new ArrayList<>();
ChartSmartVo chartSmartVo = new ChartSmartVo();
chartSmartVo.setConfigValue(updatedJson);
chartSmartVo.setUniList(legendNameList);
newList.add(chartSmartVo);
return AjaxResult.success(newList);
}