updatedJson = ColorUtils.processChartColors(JSON.toJSONString(updatedJson), getdata.size());
int size = seriesName.size();
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
JsonNode colorNode = jsonNode.path("color");
List<String> baseColors = new ArrayList<>();
if (colorNode.isArray()) {
for (JsonNode color : colorNode) {
String colorValue = color.asText();
baseColors.add(colorValue);
}
}
System.out.println("我是color字段: " + baseColors);
List<String> gradientColors = ColorUtils.generateGradientColors(baseColors, size);
ArrayNode gradientColorsNode = objectMapper.valueToTree(gradientColors);
jsonNode.set("color", gradientColorsNode);
package com.chart.common.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
public class ColorUtils
{
public static Set<String> getColors(int num, Set<String> colorSet) {
Random random = new Random();
num=colorSet.size()+num;
while (colorSet.size() < num) {
String red = Integer.toHexString(random.nextInt(256)).toUpperCase();
String green = Integer.toHexString(random.nextInt(256)).toUpperCase();
String blue = Integer.toHexString(random.nextInt(256)).toUpperCase();
red = padZero(red);
green = padZero(green);
blue = padZero(blue);
String colorCode = "#" + red + green + blue;
colorSet.add(colorCode);
}
return colorSet;
}
public static List<String> generateGradientColors(String color1, String color2, int numGradients) {
List<String> colorList = new ArrayList<>();
int[] rgb1 = hexToRGB(color1);
int[] rgb2 = hexToRGB(color2);
colorList.add(color1);
for (int i = 1; i <= numGradients; i++) {
float ratio = (float) i / (numGradients + 1);
int[] newRGB = new int[3];
for (int j = 0; j < 3; j++) {
newRGB[j] = (int) (rgb1[j] + ratio * (rgb2[j] - rgb1[j]));
}
String newColor = rgbToHex(newRGB);
colorList.add(newColor);
}
colorList.add(color2);
return colorList;
}
public static List<String> generateGradientColors(List<String> baseColors, int totalColors) {
List<String> colorList = new ArrayList<>();
int numBaseColors = baseColors.size();
int numSegments = numBaseColors - 1;
int colorsPerSegment = (totalColors - numBaseColors) / numSegments;
int remainder = (totalColors - numBaseColors) % numSegments;
for (int i = 0; i < numBaseColors - 1; i++) {
String startColor = baseColors.get(i);
String endColor = baseColors.get(i + 1);
colorList.add(startColor);
int extraColor = (i < remainder) ? 1 : 0;
int numColorsToGenerate = colorsPerSegment + extraColor;
for (int j = 1; j <= numColorsToGenerate; j++) {
float ratio = (float) j / (numColorsToGenerate + 1);
String newColor = interpolateColor(startColor, endColor, ratio);
colorList.add(newColor);
}
}
colorList.add(baseColors.get(numBaseColors - 1));
return colorList;
}
private static String interpolateColor(String color1, String color2, float ratio) {
int[] rgb1 = hexToRGB(color1);
int[] rgb2 = hexToRGB(color2);
int[] rgb = new int[3];
for (int i = 0; i < 3; i++) {
rgb[i] = (int) (rgb1[i] + ratio * (rgb2[i] - rgb1[i]));
}
return rgbToHex(rgb);
}
private static int[] hexToRGB(String hexColor) {
return new int[] {
Integer.parseInt(hexColor.substring(1, 3), 16),
Integer.parseInt(hexColor.substring(3, 5), 16),
Integer.parseInt(hexColor.substring(5, 7), 16)
};
}
private static String rgbToHex(int[] rgb) {
return String.format("#%02X%02X%02X", rgb[0], rgb[1], rgb[2]);
}
private static String padZero(String hex) {
return hex.length() == 1 ? "0" + hex : hex;
}
}