@@ -11,29 +11,29 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SVG工具类,用于生成价格趋势的SVG图表
|
* SVG工具类,用于生成价格趋势的SVG图表
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class SvgUtils {
|
public class SvgUtils {
|
||||||
|
|
||||||
// SVG画布尺寸
|
// SVG画布尺寸
|
||||||
private static final int WIDTH = 800;
|
private static final int WIDTH = 800;
|
||||||
private static final int HEIGHT = 500;
|
private static final int HEIGHT = 500;
|
||||||
private static final int PADDING = 60;
|
private static final int PADDING = 60;
|
||||||
|
|
||||||
// 生成SVG折线图(支持多条线),新增decimalPlaces参数控制Y轴小数位
|
// 生成SVG折线图(支持多条线),新增decimalPlaces参数控制Y轴小数位
|
||||||
public static String generatePriceTrendSvg(List<List<PriceData>> dataSets, List<String> labels, String title, String subtitle, int decimalPlaces) {
|
public static String generatePriceTrendSvg(List<List<PriceData>> dataSets, List<String> labels, String title, String subtitle, int decimalPlaces) {
|
||||||
if (dataSets.isEmpty() || dataSets.get(0).isEmpty()) {
|
if (dataSets.isEmpty() || dataSets.get(0).isEmpty()) {
|
||||||
return "<svg xmlns='http://www.w3.org/2000/svg' width='" + WIDTH + "' height='" + HEIGHT + "'></svg>";
|
return "<svg xmlns='http://www.w3.org/2000/svg' width='" + WIDTH + "' height='" + HEIGHT + "'></svg>";
|
||||||
}
|
}
|
||||||
// 过滤为空的数据
|
// 过滤为空的数据
|
||||||
dataSets = dataSets.stream()
|
dataSets = dataSets.stream()
|
||||||
.map(innerList -> innerList.stream()
|
.map(innerList -> innerList.stream()
|
||||||
.filter(priceData -> priceData != null && priceData.getPrice() != null)
|
.filter(priceData -> priceData != null && priceData.getPrice() != null)
|
||||||
.collect(Collectors.toList()))
|
.collect(Collectors.toList()))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
// 计算价格和时间的范围
|
// 计算价格和时间的范围
|
||||||
double minPrice = dataSets.stream()
|
double minPrice = dataSets.stream()
|
||||||
.flatMap(List::stream)
|
.flatMap(List::stream)
|
||||||
.filter((p) -> p != null && p.getPrice() != null)
|
.filter((p) -> p != null && p.getPrice() != null)
|
||||||
@@ -50,14 +50,14 @@ public class SvgUtils {
|
|||||||
minPrice = minPrice * (1 - buffer);
|
minPrice = minPrice * (1 - buffer);
|
||||||
maxPrice = maxPrice * (1 + buffer);
|
maxPrice = maxPrice * (1 + buffer);
|
||||||
|
|
||||||
// 如果decimalPlaces为0,调整minPrice和maxPrice为整数等差
|
// 如果decimalPlaces为0,调整minPrice和maxPrice为整数等差
|
||||||
// if (decimalPlaces == 0) {
|
// if (decimalPlaces == 0) {
|
||||||
// long step = calculateStep((long) Math.ceil(maxPrice) - (long) Math.floor(minPrice));
|
// long step = calculateStep((long) Math.ceil(maxPrice) - (long) Math.floor(minPrice));
|
||||||
// minPrice = Math.floor(minPrice / step) * step;
|
// minPrice = Math.floor(minPrice / step) * step;
|
||||||
// maxPrice = Math.ceil(maxPrice / step) * step;
|
// maxPrice = Math.ceil(maxPrice / step) * step;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// 解析最小和最大日期
|
// 解析最小和最大日期
|
||||||
String minYearMonth = dataSets.stream()
|
String minYearMonth = dataSets.stream()
|
||||||
.flatMap(List::stream)
|
.flatMap(List::stream)
|
||||||
.map(PriceData::getYearmonth)
|
.map(PriceData::getYearmonth)
|
||||||
@@ -69,22 +69,22 @@ public class SvgUtils {
|
|||||||
.max(String::compareTo)
|
.max(String::compareTo)
|
||||||
.orElse("2025-01");
|
.orElse("2025-01");
|
||||||
|
|
||||||
// 构建SVG内容
|
// 构建SVG内容
|
||||||
StringBuilder svg = new StringBuilder();
|
StringBuilder svg = new StringBuilder();
|
||||||
svg.append(String.format("<svg xmlns='http://www.w3.org/2000/svg' width='%d' height='%d'>", WIDTH, HEIGHT));
|
svg.append(String.format("<svg xmlns='http://www.w3.org/2000/svg' width='%d' height='%d'>", WIDTH, HEIGHT));
|
||||||
svg.append(String.format("<rect width='%d' height='%d' fill='#ffffff'/>", WIDTH, HEIGHT));
|
svg.append(String.format("<rect width='%d' height='%d' fill='#ffffff'/>", WIDTH, HEIGHT));
|
||||||
|
|
||||||
// 添加标题和副标题
|
// 添加标题和副标题
|
||||||
svg.append(String.format("<text x='%d' y='%d' font-size='20' font-family='Arial' fill='#333333' font-weight='bold' text-anchor='middle'>%s</text>",
|
svg.append(String.format("<text x='%d' y='%d' font-size='20' font-family='Arial' fill='#333333' font-weight='bold' text-anchor='middle'>%s</text>",
|
||||||
WIDTH / 2, 30, title));
|
WIDTH / 2, 30, title));
|
||||||
svg.append(String.format("<text x='%d' y='%d' font-size='14' font-family='Arial' fill='#666666' text-anchor='middle'>%s</text>",
|
svg.append(String.format("<text x='%d' y='%d' font-size='14' font-family='Arial' fill='#666666' text-anchor='middle'>%s</text>",
|
||||||
WIDTH / 2, 50, subtitle));
|
WIDTH / 2, 50, subtitle));
|
||||||
|
|
||||||
// 画虚线网格线和坐标轴
|
// 画虚线网格线和坐标轴
|
||||||
svg.append(drawGrid(minYearMonth, maxYearMonth, minPrice, maxPrice, decimalPlaces));
|
svg.append(drawGrid(minYearMonth, maxYearMonth, minPrice, maxPrice, decimalPlaces));
|
||||||
svg.append(drawAxis(minYearMonth, maxYearMonth, minPrice, maxPrice, decimalPlaces));
|
svg.append(drawAxis(minYearMonth, maxYearMonth, minPrice, maxPrice, decimalPlaces));
|
||||||
|
|
||||||
// 画多条折线和图例
|
// 画多条折线和图例
|
||||||
String[] colors = {"#66B3FF", "#FF6666", "#FFA500", "#8A2BE2"};
|
String[] colors = {"#66B3FF", "#FF6666", "#FFA500", "#8A2BE2"};
|
||||||
for (int i = 0; i < dataSets.size(); i++) {
|
for (int i = 0; i < dataSets.size(); i++) {
|
||||||
svg.append(drawSmoothLine(dataSets.get(i), minYearMonth, maxYearMonth, minPrice, maxPrice, colors[i % colors.length], decimalPlaces));
|
svg.append(drawSmoothLine(dataSets.get(i), minYearMonth, maxYearMonth, minPrice, maxPrice, colors[i % colors.length], decimalPlaces));
|
||||||
@@ -96,14 +96,14 @@ public class SvgUtils {
|
|||||||
return svg.toString();
|
return svg.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 绘制平滑折线
|
// 绘制平滑折线
|
||||||
private static String drawSmoothLine(List<PriceData> data, String minYearMonth, String maxYearMonth, double minPrice, double maxPrice, String color, int decimalPlaces) {
|
private static String drawSmoothLine(List<PriceData> data, String minYearMonth, String maxYearMonth, double minPrice, double maxPrice, String color, int decimalPlaces) {
|
||||||
// if (data.size() < 2) return "";
|
// if (data.size() < 2) return "";
|
||||||
StringBuilder path = new StringBuilder("<path d='M");
|
StringBuilder path = new StringBuilder("<path d='M");
|
||||||
StringBuilder pointsAndLabels = new StringBuilder();
|
StringBuilder pointsAndLabels = new StringBuilder();
|
||||||
int totalMonths = getMonthIndex(maxYearMonth) - getMonthIndex(minYearMonth) + 1;
|
int totalMonths = getMonthIndex(maxYearMonth) - getMonthIndex(minYearMonth) + 1;
|
||||||
if (data.size() == 1) {
|
if (data.size() == 1) {
|
||||||
// 处理只有一个数据点的情况
|
// 处理只有一个数据点的情况
|
||||||
PriceData point = data.get(0);
|
PriceData point = data.get(0);
|
||||||
int monthIndex = Math.max(0, Math.min(totalMonths - 1, getMonthIndex(point.getYearmonth()) - getMonthIndex(minYearMonth)));
|
int monthIndex = Math.max(0, Math.min(totalMonths - 1, getMonthIndex(point.getYearmonth()) - getMonthIndex(minYearMonth)));
|
||||||
int x = WIDTH / 2;
|
int x = WIDTH / 2;
|
||||||
@@ -153,7 +153,7 @@ public class SvgUtils {
|
|||||||
return path.toString();
|
return path.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 绘制网格线
|
// 绘制网格线
|
||||||
private static String drawGrid(String minYearMonth, String maxYearMonth, double minPrice, double maxPrice, int decimalPlaces) {
|
private static String drawGrid(String minYearMonth, String maxYearMonth, double minPrice, double maxPrice, int decimalPlaces) {
|
||||||
StringBuilder grid = new StringBuilder();
|
StringBuilder grid = new StringBuilder();
|
||||||
double step = calculateYStep(minPrice, maxPrice, decimalPlaces);
|
double step = calculateYStep(minPrice, maxPrice, decimalPlaces);
|
||||||
@@ -169,22 +169,22 @@ public class SvgUtils {
|
|||||||
return grid.toString();
|
return grid.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 绘制坐标轴
|
// 绘制坐标轴
|
||||||
private static String drawAxis(String minYearMonth, String maxYearMonth, double minPrice, double maxPrice, int decimalPlaces) {
|
private static String drawAxis(String minYearMonth, String maxYearMonth, double minPrice, double maxPrice, int decimalPlaces) {
|
||||||
StringBuilder axis = new StringBuilder();
|
StringBuilder axis = new StringBuilder();
|
||||||
|
|
||||||
// X轴
|
// X轴
|
||||||
axis.append(String.format("<line x1='%d' y1='%d' x2='%d' y2='%d' stroke='#333333' stroke-width='2'/>",
|
axis.append(String.format("<line x1='%d' y1='%d' x2='%d' y2='%d' stroke='#333333' stroke-width='2'/>",
|
||||||
PADDING, HEIGHT - PADDING, WIDTH - PADDING, HEIGHT - PADDING));
|
PADDING, HEIGHT - PADDING, WIDTH - PADDING, HEIGHT - PADDING));
|
||||||
axis.append(String.format("<line x1='%d' y1='%d' x2='%d' y2='%d' stroke='#333333' stroke-width='2'/>",
|
axis.append(String.format("<line x1='%d' y1='%d' x2='%d' y2='%d' stroke='#333333' stroke-width='2'/>",
|
||||||
PADDING, PADDING, PADDING, HEIGHT - PADDING));
|
PADDING, PADDING, PADDING, HEIGHT - PADDING));
|
||||||
|
|
||||||
// X轴刻度
|
// X轴刻度
|
||||||
int totalMonths = getMonthIndex(maxYearMonth) - getMonthIndex(minYearMonth) + 1;
|
int totalMonths = getMonthIndex(maxYearMonth) - getMonthIndex(minYearMonth) + 1;
|
||||||
for (int i = 0; i < totalMonths; i++) {
|
for (int i = 0; i < totalMonths; i++) {
|
||||||
int x;
|
int x;
|
||||||
if (totalMonths == 1) {
|
if (totalMonths == 1) {
|
||||||
// 当只有一个月时,将刻度放在中间位置
|
// 当只有一个月时,将刻度放在中间位置
|
||||||
x = PADDING + (WIDTH - 2 * PADDING) / 2;
|
x = PADDING + (WIDTH - 2 * PADDING) / 2;
|
||||||
} else {
|
} else {
|
||||||
x = PADDING + i * (WIDTH - 2 * PADDING) / (totalMonths - 1);
|
x = PADDING + i * (WIDTH - 2 * PADDING) / (totalMonths - 1);
|
||||||
@@ -196,7 +196,7 @@ public class SvgUtils {
|
|||||||
x, HEIGHT - PADDING + 20, monthLabel));
|
x, HEIGHT - PADDING + 20, monthLabel));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Y轴刻度
|
// Y轴刻度
|
||||||
double step = calculateYStep(minPrice, maxPrice, decimalPlaces);
|
double step = calculateYStep(minPrice, maxPrice, decimalPlaces);
|
||||||
int numSteps = (int) ((maxPrice - minPrice) / step) + 1;
|
int numSteps = (int) ((maxPrice - minPrice) / step) + 1;
|
||||||
for (int i = 0; i < numSteps; i++) {
|
for (int i = 0; i < numSteps; i++) {
|
||||||
@@ -211,18 +211,18 @@ public class SvgUtils {
|
|||||||
return axis.toString();
|
return axis.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算Y轴步长
|
// 计算Y轴步长
|
||||||
private static double calculateYStep(double minPrice, double maxPrice, int decimalPlaces) {
|
private static double calculateYStep(double minPrice, double maxPrice, int decimalPlaces) {
|
||||||
double range = maxPrice - minPrice;
|
double range = maxPrice - minPrice;
|
||||||
if (decimalPlaces == 0) {
|
if (decimalPlaces == 0) {
|
||||||
return calculateStep((long) range);
|
return calculateStep((long) range);
|
||||||
}
|
}
|
||||||
double step = range / 10.0; // 默认10个间隔
|
double step = range / 10.0; // 默认10个间隔
|
||||||
double scale = Math.pow(10, decimalPlaces);
|
double scale = Math.pow(10, decimalPlaces);
|
||||||
return Math.round(step * scale) / scale;
|
return Math.round(step * scale) / scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算整数步长
|
// 计算整数步长
|
||||||
private static long calculateStep(long range) {
|
private static long calculateStep(long range) {
|
||||||
long step = (long) Math.pow(10, Math.floor(Math.log10(range)));
|
long step = (long) Math.pow(10, Math.floor(Math.log10(range)));
|
||||||
if (range / step < 4) step /= 2;
|
if (range / step < 4) step /= 2;
|
||||||
@@ -230,7 +230,7 @@ public class SvgUtils {
|
|||||||
return step;
|
return step;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取月份标签和索引(保持不变)
|
// 获取月份标签和索引(保持不变)
|
||||||
private static String getMonthLabel(String minYearMonth, int offset) {
|
private static String getMonthLabel(String minYearMonth, int offset) {
|
||||||
int totalMonths = getMonthIndex(minYearMonth);
|
int totalMonths = getMonthIndex(minYearMonth);
|
||||||
int targetMonthIndex = totalMonths + offset;
|
int targetMonthIndex = totalMonths + offset;
|
||||||
@@ -255,44 +255,44 @@ public class SvgUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对齐已有系列数据与新的时间轴
|
* 对齐已有系列数据与新的时间轴
|
||||||
*/
|
*/
|
||||||
public static void alignSeriesData(List<String> newCategories, List<PriceDto.DoubleSeries> series, PriceDto.PriceTrend rentTrend) {
|
public static void alignSeriesData(List<String> newCategories, List<PriceDto.DoubleSeries> series, PriceDto.PriceTrend rentTrend) {
|
||||||
for (PriceDto.DoubleSeries s : series) {
|
for (PriceDto.DoubleSeries s : series) {
|
||||||
List<Double> originalData = s.getData();
|
List<Double> originalData = s.getData();
|
||||||
List<String> originalCategories = new ArrayList<>(rentTrend.getCategories()); // 假设原始时间轴可从别处获取
|
List<String> originalCategories = new ArrayList<>(rentTrend.getCategories()); // 假设原始时间轴可从别处获取
|
||||||
List<Double> alignedData = new ArrayList<>();
|
List<Double> alignedData = new ArrayList<>();
|
||||||
|
|
||||||
// 根据新的时间轴对齐数据
|
// 根据新的时间轴对齐数据
|
||||||
for (String date : newCategories) {
|
for (String date : newCategories) {
|
||||||
int index = originalCategories.indexOf(date);
|
int index = originalCategories.indexOf(date);
|
||||||
if (index >= 0 && index < originalData.size()) {
|
if (index >= 0 && index < originalData.size()) {
|
||||||
alignedData.add(originalData.get(index));
|
alignedData.add(originalData.get(index));
|
||||||
} else {
|
} else {
|
||||||
alignedData.add(null); // 日期不存在时补 null
|
alignedData.add(null); // 日期不存在时补 null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.setData(alignedData);
|
s.setData(alignedData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试方法
|
// 测试方法
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
List<PriceData> data1 = PriceData.getSampleData(5200);
|
List<PriceData> data1 = PriceData.getSampleData(5200);
|
||||||
List<List<PriceData>> dataSets = new ArrayList<>();
|
List<List<PriceData>> dataSets = new ArrayList<>();
|
||||||
dataSets.add(data1);
|
dataSets.add(data1);
|
||||||
String name = "新世纪大厦";
|
String name = "新世纪大厦";
|
||||||
List<String> labels = Arrays.asList(name);
|
List<String> labels = Arrays.asList(name);
|
||||||
|
|
||||||
String title = "价格走势图";
|
String title = "价格走势图";
|
||||||
String subtitle = "2024-12 至 2025-11";
|
String subtitle = "2024-12 至 2025-11";
|
||||||
|
|
||||||
// // 测试小数位为2
|
// // 测试小数位为2
|
||||||
// String svgWithoutDecimals = generatePriceTrendSvg(dataSets, labels, title, subtitle, 2);
|
// String svgWithoutDecimals = generatePriceTrendSvg(dataSets, labels, title, subtitle, 2);
|
||||||
// System.out.println("With 2 decimal places:");
|
// System.out.println("With 2 decimal places:");
|
||||||
// System.out.println(svgWithoutDecimals);
|
// System.out.println(svgWithoutDecimals);
|
||||||
|
|
||||||
// 测试小数位为0(整数等差)
|
// 测试小数位为0(整数等差)
|
||||||
String svgWithoutDecimals = generatePriceTrendSvg(dataSets, labels, title, subtitle, 0);
|
String svgWithoutDecimals = generatePriceTrendSvg(dataSets, labels, title, subtitle, 0);
|
||||||
System.out.println("With 0 decimal places (integer steps):");
|
System.out.println("With 0 decimal places (integer steps):");
|
||||||
System.out.println(svgWithoutDecimals);
|
System.out.println(svgWithoutDecimals);
|
||||||
|
|||||||
Reference in New Issue
Block a user