宏信相关

This commit is contained in:
2025-12-25 08:53:00 +08:00
parent 469ee5f2ed
commit 5abf2ddaaa
7 changed files with 712 additions and 4 deletions
@@ -0,0 +1,82 @@
package com.fangjia.core.util;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@Data
public class PriceData {
private String yearmonth;
private Double price;
private Double volume; // 新增成交量字段
public PriceData() {}
public PriceData(String yearmonth, Double price) {
this.yearmonth = yearmonth;
this.price = price;
}
public PriceData(String yearmonth, Double price, Double volume) {
this.yearmonth = yearmonth;
this.price = price;
this.volume = volume;
}
public static List<PriceData> getSampleData() {
List<PriceData> data = new ArrayList<>();
double basePrice = 22000;
double minPrice = basePrice * (1 - 0.04);
double maxPrice = basePrice * (1 + 0.04);
Random random = new Random();
for (int i = 0; i < 2; i++) {
int year = 2024;
int month = i + 4;
if (month > 12) {
year++;
month -= 12;
}
String date = String.format("%d-%02d", year, month);
double price = minPrice + random.nextDouble() * (maxPrice - minPrice);
double volume = 100 + random.nextInt(100);
data.add(new PriceData(date, price, volume));
System.out.println(data.get(i));
}
return data;
}
public static List<PriceData> getSampleData(double basePrice) {
List<PriceData> data = new ArrayList<>();
double minPrice = basePrice * (1 - 0.02);
double maxPrice = basePrice * (1 + 0.02);
Random random = new Random();
for (int i = 0; i < 12; i++) {
int year = 2025;
int month = i + 1;
if (month > 12) {
year++;
month -= 12;
}
String date = String.format("%d-%02d", year, month);
double price = minPrice + random.nextDouble() * (maxPrice - minPrice);
double volume = 100 + random.nextInt(200);
data.add(new PriceData(date, price, volume));
System.out.println(data.get(i));
}
return data;
}
public static List<PriceData> getSampleData2() {
List<PriceData> data = new ArrayList<>();
data.add(new PriceData("2025-01", 11110.0, 90.0));
data.add(new PriceData("2025-02", 11115.0, 110.0));
data.add(new PriceData("2025-03", 11112.0, 95.0));
data.add(new PriceData("2025-04", 11120.0, 150.0));
data.add(new PriceData("2025-05", 11118.0, 140.0));
return data;
}
}
@@ -0,0 +1,49 @@
package com.fangjia.core.util;
import lombok.Builder;
import lombok.Data;
import java.util.List;
/**
* @author shenjian
* @since 2024/12/17
*/
@Data
public class PriceDto {
private Integer id;
private Integer avgPrice;//本周均价
private PriceTrend priceTrend; //均价走势
private List<RentPrice> rentPrices;//本月租金
private PriceTrend rentTrend;//租金走势
@Data
@Builder
public static class RentPrice {
private String name;
private Integer price;
}
@Data
public static class PriceTrend {
private List<String> categories;
private List<Series> series;
private List<DoubleSeries> doubleSeries;
}
@Data
public static class Series {
private String name;
private List<Integer> data;
}
@Data
public static class DoubleSeries {
private String name;
private List<Double> data;
}
}
@@ -0,0 +1,316 @@
package com.fangjia.core.util;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* SVG工具类,用于生成价格趋势的SVG图表
*/
public class SvgUtils {
// SVG画布尺寸
private static final int WIDTH = 800;
private static final int HEIGHT = 500;
private static final int PADDING = 60;
// 生成SVG折线图(支持多条线),新增decimalPlaces参数控制Y轴小数位
public static String generatePriceTrendSvg(List<List<PriceData>> dataSets, List<String> labels, String title, String subtitle, int decimalPlaces) {
if (dataSets.isEmpty() || dataSets.get(0).isEmpty()) {
return "<svg xmlns='http://www.w3.org/2000/svg' width='" + WIDTH + "' height='" + HEIGHT + "'></svg>";
}
// 过滤为空的数据
dataSets = dataSets.stream()
.map(innerList -> innerList.stream()
.filter(priceData -> priceData != null && priceData.getPrice() != null)
.collect(Collectors.toList()))
.collect(Collectors.toList());
// 计算价格和时间的范围
double minPrice = dataSets.stream()
.flatMap(List::stream)
.filter((p) -> p != null && p.getPrice() != null)
.mapToDouble(PriceData::getPrice)
.min()
.orElse(0) * 0.8;
double maxPrice = dataSets.stream()
.flatMap(List::stream)
.filter((p) -> p != null && p.getPrice() != null)
.mapToDouble(PriceData::getPrice)
.max()
.orElse(1) * 1.2;
double buffer = 0.1;
minPrice = minPrice * (1 - buffer);
maxPrice = maxPrice * (1 + buffer);
// 如果decimalPlaces为0,调整minPrice和maxPrice为整数等差
// if (decimalPlaces == 0) {
// long step = calculateStep((long) Math.ceil(maxPrice) - (long) Math.floor(minPrice));
// minPrice = Math.floor(minPrice / step) * step;
// maxPrice = Math.ceil(maxPrice / step) * step;
// }
// 解析最小和最大日期
String minYearMonth = dataSets.stream()
.flatMap(List::stream)
.map(PriceData::getYearmonth)
.min(String::compareTo)
.orElse("2024-08");
String maxYearMonth = dataSets.stream()
.flatMap(List::stream)
.map(PriceData::getYearmonth)
.max(String::compareTo)
.orElse("2025-01");
// 构建SVG内容
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("<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>",
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>",
WIDTH / 2, 50, subtitle));
// 画虚线网格线和坐标轴
svg.append(drawGrid(minYearMonth, maxYearMonth, minPrice, maxPrice, decimalPlaces));
svg.append(drawAxis(minYearMonth, maxYearMonth, minPrice, maxPrice, decimalPlaces));
// 画多条折线和图例
String[] colors = {"#66B3FF", "#FF6666", "#FFA500", "#8A2BE2"};
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(String.format("<rect x='%d' y='%d' width='10' height='10' fill='%s'/>", 650, 60 + i * 20, colors[i % colors.length]));
svg.append(String.format("<text x='%d' y='%d' font-size='12' fill='#333333'>%s</text>", 670, 70 + i * 20, labels.get(i)));
}
svg.append("</svg>");
return svg.toString();
}
// 绘制平滑折线
private static String drawSmoothLine(List<PriceData> data, String minYearMonth, String maxYearMonth, double minPrice, double maxPrice, String color, int decimalPlaces) {
// if (data.size() < 2) return "";
StringBuilder path = new StringBuilder("<path d='M");
StringBuilder pointsAndLabels = new StringBuilder();
int totalMonths = getMonthIndex(maxYearMonth) - getMonthIndex(minYearMonth) + 1;
if (data.size() == 1) {
// 处理只有一个数据点的情况
PriceData point = data.get(0);
int monthIndex = Math.max(0, Math.min(totalMonths - 1, getMonthIndex(point.getYearmonth()) - getMonthIndex(minYearMonth)));
int x = WIDTH / 2;
int y = HEIGHT - PADDING - (HEIGHT - 2 * PADDING) / 2;
// int y = HEIGHT - PADDING - (int) ((point.getPrice() - minPrice) * (HEIGHT - 2 * PADDING) / (maxPrice - minPrice));
pointsAndLabels.append(String.format("<circle cx='%d' cy='%d' r='3' fill='%s'/>", x, y, color));
pointsAndLabels.append(String.format("<text x='%d' y='%d' font-size='10' fill='%s'>%." + decimalPlaces + "f</text>",
x + 5, y - 5, color, point.getPrice()));
path.append("M").append(x).append(",").append(y);
} else if (data.size() > 1) {
for (int i = 0; i < data.size() - 1; i++) {
PriceData point1 = data.get(i);
PriceData point2 = data.get(i + 1);
int monthIndex1 = Math.max(0, Math.min(totalMonths - 1, getMonthIndex(point1.getYearmonth()) - getMonthIndex(minYearMonth)));
int monthIndex2 = Math.max(0, Math.min(totalMonths - 1, getMonthIndex(point2.getYearmonth()) - getMonthIndex(minYearMonth)));
int x1 = PADDING + (monthIndex1 * (WIDTH - 2 * PADDING) / (totalMonths - 1));
int y1 = HEIGHT - PADDING - (int) ((point1.getPrice() - minPrice) * (HEIGHT - 2 * PADDING) / (maxPrice - minPrice));
int x2 = PADDING + (monthIndex2 * (WIDTH - 2 * PADDING) / (totalMonths - 1));
int y2 = HEIGHT - PADDING - (int) ((point2.getPrice() - minPrice) * (HEIGHT - 2 * PADDING) / (maxPrice - minPrice));
int controlX1 = x1 + (x2 - x1) / 3;
int controlY1 = y1;
int controlX2 = x2 - (x2 - x1) / 3;
int controlY2 = y2;
if (i == 0) path.append(x1).append(",").append(y1);
path.append(" C").append(controlX1).append(",").append(controlY1).append(" ").append(controlX2).append(",").append(controlY2).append(" ").append(x2).append(",").append(y2);
pointsAndLabels.append(String.format("<circle cx='%d' cy='%d' r='3' fill='%s'/>", x1, y1, color));
pointsAndLabels.append(String.format("<text x='%d' y='%d' font-size='10' fill='%s'>%." + decimalPlaces + "f</text>",
x1 + 5, y1 - 5, color, point1.getPrice()));
}
PriceData lastPoint = data.get(data.size() - 1);
int lastMonthIndex = Math.max(0, Math.min(totalMonths - 1, getMonthIndex(lastPoint.getYearmonth()) - getMonthIndex(minYearMonth)));
int lastX = PADDING + (lastMonthIndex * (WIDTH - 2 * PADDING) / (totalMonths - 1));
int lastY = HEIGHT - PADDING - (int) ((lastPoint.getPrice() - minPrice) * (HEIGHT - 2 * PADDING) / (maxPrice - minPrice));
pointsAndLabels.append(String.format("<circle cx='%d' cy='%d' r='3' fill='%s'/>", lastX, lastY, color));
pointsAndLabels.append(String.format("<text x='%d' y='%d' font-size='10' fill='%s'>%." + decimalPlaces + "f</text>",
lastX + 5, lastY - 5, color, lastPoint.getPrice()));
}
path.append("' fill='none' stroke='").append(color).append("' stroke-width='2'/>");
path.append(pointsAndLabels);
return path.toString();
}
// 绘制网格线
private static String drawGrid(String minYearMonth, String maxYearMonth, double minPrice, double maxPrice, int decimalPlaces) {
StringBuilder grid = new StringBuilder();
double step = calculateYStep(minPrice, maxPrice, decimalPlaces);
int numSteps = (int) ((maxPrice - minPrice) / step) + 1;
for (int i = 0; i < numSteps; i++) {
double price = minPrice + i * step;
if (price > maxPrice) break;
int y = HEIGHT - PADDING - (int) ((price - minPrice) * (HEIGHT - 2 * PADDING) / (maxPrice - minPrice));
grid.append(String.format("<line x1='%d' y1='%d' x2='%d' y2='%d' stroke='#e0e0e0' stroke-dasharray='5,5'/>",
PADDING, y, WIDTH - PADDING, y));
}
return grid.toString();
}
// 绘制坐标轴
private static String drawAxis(String minYearMonth, String maxYearMonth, double minPrice, double maxPrice, int decimalPlaces) {
StringBuilder axis = new StringBuilder();
// X轴
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));
axis.append(String.format("<line x1='%d' y1='%d' x2='%d' y2='%d' stroke='#333333' stroke-width='2'/>",
PADDING, PADDING, PADDING, HEIGHT - PADDING));
// X轴刻度
int totalMonths = getMonthIndex(maxYearMonth) - getMonthIndex(minYearMonth) + 1;
for (int i = 0; i < totalMonths; i++) {
int x;
if (totalMonths == 1) {
// 当只有一个月时,将刻度放在中间位置
x = PADDING + (WIDTH - 2 * PADDING) / 2;
} else {
x = PADDING + i * (WIDTH - 2 * PADDING) / (totalMonths - 1);
}
String monthLabel = getMonthLabel(minYearMonth, i);
axis.append(String.format("<line x1='%d' y1='%d' x2='%d' y2='%d' stroke='#333333' stroke-width='1'/>",
x, HEIGHT - PADDING + 5, x, HEIGHT - PADDING));
axis.append(String.format("<text x='%d' y='%d' font-size='12' fill='#333333' text-anchor='middle'>%s</text>",
x, HEIGHT - PADDING + 20, monthLabel));
}
// Y轴刻度
double step = calculateYStep(minPrice, maxPrice, decimalPlaces);
int numSteps = (int) ((maxPrice - minPrice) / step) + 1;
for (int i = 0; i < numSteps; i++) {
double price = minPrice + i * step;
int y = HEIGHT - PADDING - (int) ((price - minPrice) * (HEIGHT - 2 * PADDING) / (maxPrice - minPrice));
axis.append(String.format("<line x1='%d' y1='%d' x2='%d' y2='%d' stroke='#333333' stroke-width='1'/>",
PADDING - 5, y, PADDING, y));
axis.append(String.format("<text x='%d' y='%d' font-size='12' fill='#333333' text-anchor='end'>%." + decimalPlaces + "f</text>",
PADDING - 10, y + 5, price));
}
return axis.toString();
}
// 计算Y轴步长
private static double calculateYStep(double minPrice, double maxPrice, int decimalPlaces) {
double range = maxPrice - minPrice;
if (decimalPlaces == 0) {
return calculateStep((long) range);
}
double step = range / 10.0; // 默认10个间隔
double scale = Math.pow(10, decimalPlaces);
return Math.round(step * scale) / scale;
}
// 计算整数步长
private static long calculateStep(long range) {
long step = (long) Math.pow(10, Math.floor(Math.log10(range)));
if (range / step < 4) step /= 2;
else if (range / step > 8) step *= 2;
return step;
}
// 获取月份标签和索引(保持不变)
private static String getMonthLabel(String minYearMonth, int offset) {
int totalMonths = getMonthIndex(minYearMonth);
int targetMonthIndex = totalMonths + offset;
int year = 2024 + targetMonthIndex / 12;
int month = targetMonthIndex % 12 + 1;
return String.format("%d-%02d", year, month);
}
private static int getMonthIndex(String yearMonth) {
int year = Integer.parseInt(yearMonth.substring(0, 4));
int month = Integer.parseInt(yearMonth.substring(5));
return (year - 2024) * 12 + (month - 1);
}
public static void generateSvg(String svgContent, String outputPath) {
try (FileWriter writer = new FileWriter(outputPath)) {
writer.write(svgContent);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 对齐已有系列数据与新的时间轴
*/
public static void alignSeriesData(List<String> newCategories, List<PriceDto.DoubleSeries> series, PriceDto.PriceTrend rentTrend) {
for (PriceDto.DoubleSeries s : series) {
List<Double> originalData = s.getData();
List<String> originalCategories = new ArrayList<>(rentTrend.getCategories()); // 假设原始时间轴可从别处获取
List<Double> alignedData = new ArrayList<>();
// 根据新的时间轴对齐数据
for (String date : newCategories) {
int index = originalCategories.indexOf(date);
if (index >= 0 && index < originalData.size()) {
alignedData.add(originalData.get(index));
} else {
alignedData.add(null); // 日期不存在时补 null
}
}
s.setData(alignedData);
}
}
// 测试方法
public static void main(String[] args) {
List<PriceData> data1 = PriceData.getSampleData(5200);
List<List<PriceData>> dataSets = new ArrayList<>();
dataSets.add(data1);
String name = "新世纪大厦";
List<String> labels = Arrays.asList(name);
String title = "价格走势图";
String subtitle = "2024-12 至 2025-11";
// // 测试小数位为2
// String svgWithoutDecimals = generatePriceTrendSvg(dataSets, labels, title, subtitle, 2);
// System.out.println("With 2 decimal places:");
// System.out.println(svgWithoutDecimals);
// 测试小数位为0(整数等差)
String svgWithoutDecimals = generatePriceTrendSvg(dataSets, labels, title, subtitle, 0);
System.out.println("With 0 decimal places (integer steps):");
System.out.println(svgWithoutDecimals);
try {
generateSvg(svgWithoutDecimals, name + ".svg");
System.out.println("PNG image generated successfully at: price_trend.png");
} catch (Exception e) {
e.printStackTrace();
}
// String locationStr = "114.407048,30.481859";
// String[] location = locationStr.split(",");
// Double lng = Double.parseDouble(location[0]);
// Double lat = Double.parseDouble(location[1]);
// String mapUrl = "https://restapi.amap.com/v3/staticmap?location=" + lng + "," + lat
// + "&zoom=14&size=780*300&markers=mid,,A:" + lng + "," + lat
// + "&key=ee95e52bf08006f63fd29bcfbcf21df0";
// System.out.println(mapUrl);
}
}