public static List< List< Object> > splitList ( List< Object> originalList, int groupSize) {
List< List< Object> > groupedLists = new ArrayList < > ( ) ;
int size = originalList. size ( ) ;
for ( int i = 0 ; i < size; i + = groupSize) {
int end = Math. min ( size, i + groupSize) ;
List< Object> sublist = originalList. subList ( i, end) ;
groupedLists. add ( new ArrayList < > ( sublist) ) ;
}
return groupedLists;
}
@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) ;
}
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" ) ) {
if ( previousNonZeroIndex != - 1 ) {
int columnsBetween = i - previousNonZeroIndex;
arrayList. add ( columnsBetween) ;
}
previousNonZeroIndex = i;
}
}
getdata. remove ( 0 ) ;
getdata. remove ( 0 ) ;
Object arr0 = arrayList. get ( 0 ) ;
Integer integerValue = ( Integer) arr0;
int line = integerValue;
System. out. println ( "我是中间隔的列数" + line) ;
int number = sizeLine / line;
List< List< String> > 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) ;
columnData. get ( colIndex) . add ( 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) {
sourceArray. addPOJO ( item) ;
}
groupIndex++ ;
}
}
System. out. println ( "更新后的 dataset: " + dataset. toString ( ) ) ;
String updatedJson = rootNode. toString ( ) ;
System. out. println ( "完整的 JSON 数据: " + updatedJson) ;
return AjaxResult. success ( ) ;
}