diff --git a/projects/fangjia-core/src/main/java/com/fangjia/core/util/SvgUtils.java b/projects/fangjia-core/src/main/java/com/fangjia/core/util/SvgUtils.java index ce45c16da..8d3b92739 100644 --- a/projects/fangjia-core/src/main/java/com/fangjia/core/util/SvgUtils.java +++ b/projects/fangjia-core/src/main/java/com/fangjia/core/util/SvgUtils.java @@ -11,29 +11,29 @@ import java.util.stream.Collectors; /** - * SVG工具类,用于生æˆ�价格趋势的SVG图表 + * SVG¹¤¾ßÀ࣬ÓÃÓÚÉú³É¼Û¸ñÇ÷ÊÆµÄSVGͼ±í */ public class SvgUtils { - // SVG画布尺寸 + // SVG»­²¼³ß´ç private static final int WIDTH = 800; private static final int HEIGHT = 500; private static final int PADDING = 60; - // 生æˆ�SVG折线图(支æŒ�多æ�¡çº¿ï¼‰ï¼Œæ–°å¢ždecimalPlaceså�‚数控制Yè½´å°�æ•°ä½� + // Éú³ÉSVGÕÛÏßͼ£¨Ö§³Ö¶àÌõÏߣ©£¬ÐÂÔödecimalPlaces²ÎÊý¿ØÖÆYÖáСÊýλ public static String generatePriceTrendSvg(List> dataSets, List labels, String title, String subtitle, int decimalPlaces) { if (dataSets.isEmpty() || dataSets.get(0).isEmpty()) { return ""; } - // 过滤为空的数æ�® + // ¹ýÂËΪ¿ÕµÄÊý¾Ý 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) @@ -50,14 +50,14 @@ public class SvgUtils { minPrice = minPrice * (1 - buffer); maxPrice = maxPrice * (1 + buffer); - // 如果decimalPlaces为0,调整minPriceå’ŒmaxPrice为整数等差 + // Èç¹û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) @@ -69,22 +69,22 @@ public class SvgUtils { .max(String::compareTo) .orElse("2025-01"); - // 构建SVG内容 + // ¹¹½¨SVGÄÚÈÝ StringBuilder svg = new StringBuilder(); svg.append(String.format("", WIDTH, HEIGHT)); svg.append(String.format("", WIDTH, HEIGHT)); - // 添加标题和副标题 + // Ìí¼Ó±êÌâºÍ¸±±êÌâ svg.append(String.format("%s", WIDTH / 2, 30, title)); svg.append(String.format("%s", 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)); @@ -96,14 +96,14 @@ public class SvgUtils { return svg.toString(); } - // 绘制平滑折线 + // »æÖÆÆ½»¬ÕÛÏß private static String drawSmoothLine(List data, String minYearMonth, String maxYearMonth, double minPrice, double maxPrice, String color, int decimalPlaces) { // if (data.size() < 2) return ""; StringBuilder path = new StringBuilder("", PADDING, HEIGHT - PADDING, WIDTH - PADDING, HEIGHT - PADDING)); axis.append(String.format("", PADDING, PADDING, PADDING, HEIGHT - PADDING)); - // X轴刻度 + // 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); @@ -196,7 +196,7 @@ public class SvgUtils { x, HEIGHT - PADDING + 20, monthLabel)); } - // Y轴刻度 + // YÖá¿Ì¶È double step = calculateYStep(minPrice, maxPrice, decimalPlaces); int numSteps = (int) ((maxPrice - minPrice) / step) + 1; for (int i = 0; i < numSteps; i++) { @@ -211,18 +211,18 @@ public class SvgUtils { return axis.toString(); } - // 计算Y轴步长 + // ¼ÆËã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 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; @@ -230,7 +230,7 @@ public class SvgUtils { return step; } - // 获å�–月份标签和索引(ä¿�æŒ�ä¸�å�˜ï¼‰ + // »ñȡԷݱêÇ©ºÍË÷Òý£¨±£³Ö²»±ä£© private static String getMonthLabel(String minYearMonth, int offset) { int totalMonths = getMonthIndex(minYearMonth); int targetMonthIndex = totalMonths + offset; @@ -255,44 +255,44 @@ public class SvgUtils { } /** - * 对é½�已有系列数æ�®ä¸Žæ–°çš„æ—¶é—´è½´ + * ¶ÔÆëÒÑÓÐϵÁÐÊý¾ÝÓëеÄʱ¼äÖá */ public static void alignSeriesData(List newCategories, List series, PriceDto.PriceTrend rentTrend) { for (PriceDto.DoubleSeries s : series) { List originalData = s.getData(); - List originalCategories = new ArrayList<>(rentTrend.getCategories()); // å�‡è®¾åŽŸå§‹æ—¶é—´è½´å�¯ä»Žåˆ«å¤„获å�– + List originalCategories = new ArrayList<>(rentTrend.getCategories()); // ¼ÙÉèԭʼʱ¼äÖá¿É´Ó±ð´¦»ñÈ¡ List 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 + alignedData.add(null); // ÈÕÆÚ²»´æÔÚʱ²¹ null } } s.setData(alignedData); } } - // 测试方法 + // ²âÊÔ·½·¨ public static void main(String[] args) { List data1 = PriceData.getSampleData(5200); List> dataSets = new ArrayList<>(); dataSets.add(data1); - String name = "新世纪大厦"; + String name = "ÐÂÊÀ¼Í´óÏÃ"; List labels = Arrays.asList(name); - String title = "价格走势图"; - String subtitle = "2024-12 至 2025-11"; + String title = "¼Û¸ñ×ßÊÆÍ¼"; + String subtitle = "2024-12 ÖÁ 2025-11"; -// // 测试å°�æ•°ä½�为2 +// // ²âÊÔСÊýλΪ2 // String svgWithoutDecimals = generatePriceTrendSvg(dataSets, labels, title, subtitle, 2); // System.out.println("With 2 decimal places:"); // System.out.println(svgWithoutDecimals); - // 测试å°�æ•°ä½�为0(整数等差) + // ²âÊÔСÊýλΪ0£¨ÕûÊýµÈ²î£© String svgWithoutDecimals = generatePriceTrendSvg(dataSets, labels, title, subtitle, 0); System.out.println("With 0 decimal places (integer steps):"); System.out.println(svgWithoutDecimals);