From 73e810fc9569597be99accd08d63aab6e7c0daac Mon Sep 17 00:00:00 2001
From: xyc <jc_xiong@hotmail.com>
Date: 星期二, 06 十月 2026 09:36:41 +0800
Subject: [PATCH] fix(holiday): 提高平均拥挤度走势图标签精度

---
 traffic-audit-server/src/main/java/com/trafficaudit/holiday/service/HolidayTrendChartService.java |   27 +++++++++++++++++----------
 1 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/traffic-audit-server/src/main/java/com/trafficaudit/holiday/service/HolidayTrendChartService.java b/traffic-audit-server/src/main/java/com/trafficaudit/holiday/service/HolidayTrendChartService.java
index 4343f08..e0b9c88 100644
--- a/traffic-audit-server/src/main/java/com/trafficaudit/holiday/service/HolidayTrendChartService.java
+++ b/traffic-audit-server/src/main/java/com/trafficaudit/holiday/service/HolidayTrendChartService.java
@@ -95,6 +95,11 @@
 
     public byte[] lineChart(String yAxisLabel, List<ChartSeries> series, boolean percentAxis,
                             int width, int height) {
+        return lineChart(yAxisLabel, series, percentAxis, 2, width, height);
+    }
+
+    public byte[] lineChart(String yAxisLabel, List<ChartSeries> series, boolean percentAxis,
+                            int valueDecimals, int width, int height) {
         XYSeriesCollection dataset = new XYSeriesCollection();
         for (int seriesIndex = 0; seriesIndex < series.size(); seriesIndex++) {
             ChartSeries item = series.get(seriesIndex);
@@ -148,7 +153,7 @@
         }
         NumberFormat valueFormat = percentAxis
                 ? NumberFormat.getPercentInstance() : new DecimalFormat("#,##0.##");
-        valueFormat.setMaximumFractionDigits(percentAxis ? 0 : 2);
+        valueFormat.setMaximumFractionDigits(percentAxis ? 0 : valueDecimals);
         rangeAxis.setNumberFormatOverride(valueFormat);
         plot.setRangeAxis(rangeAxis);
         plot.setAxisOffset(new RectangleInsets(2.0, 2.0, 2.0, 2.0));
@@ -166,7 +171,7 @@
                     BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
         }
         plot.setRenderer(renderer);
-        return render(chart, width, height, yAxisLabel, true, percentAxis);
+        return render(chart, width, height, yAxisLabel, true, percentAxis, valueDecimals);
     }
 
     public byte[] pieChart(String valueSuffix, List<ChartCategory> categories,
@@ -212,7 +217,7 @@
         for (ChartCategory category : categories) {
             plot.setSectionPaint(category.getName(), seriesColor(index++));
         }
-        return render(chart, width, height, valueSuffix, false, false);
+        return render(chart, width, height, valueSuffix, false, false, 2);
     }
 
     public byte[] stackedBarChart(String yAxisLabel, List<String> categories,
@@ -282,7 +287,7 @@
             renderer.setSeriesPaint(i, seriesColor(i));
         }
         plot.setRenderer(renderer);
-        return render(chart, width, height, yAxisLabel, true, percentAxis);
+        return render(chart, width, height, yAxisLabel, true, percentAxis, 2);
     }
 
     private static class WaterRightPiePlot<K extends Comparable<K>> extends PiePlot<K> {
@@ -445,7 +450,7 @@
     }
 
     private byte[] render(JFreeChart chart, int width, int height, String unitLabel,
-                         boolean axisChart, boolean percentItemLabels) {
+                         boolean axisChart, boolean percentItemLabels, int valueDecimals) {
         try {
             reserveUnitLabelBand(chart, axisChart, unitLabel);
             ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -465,7 +470,7 @@
                     drawDateLabel(graphics, info.getPlotInfo().getDataArea());
                 }
                 drawXYItemLabels(graphics, image, chart,
-                        info.getPlotInfo().getDataArea(), percentItemLabels, unitLabel);
+                        info.getPlotInfo().getDataArea(), percentItemLabels, unitLabel, valueDecimals);
                 drawStackedBarLabels(graphics, image, chart,
                         info.getPlotInfo().getDataArea(), percentItemLabels);
             } finally {
@@ -830,7 +835,7 @@
 
     private void drawXYItemLabels(Graphics2D graphics, BufferedImage image, JFreeChart chart,
                                   Rectangle2D dataArea, boolean percentItemLabels,
-                                  String unitLabel) {
+                                  String unitLabel, int valueDecimals) {
         if (!(chart.getPlot() instanceof XYPlot) || dataArea == null
                 || dataArea.getWidth() <= 0 || dataArea.getHeight() <= 0) {
             return;
@@ -859,7 +864,7 @@
                 Point2D point = itemPoint(plot, dataset, seriesIndex, itemIndex, dataArea);
                 if (yValue == null || point == null) continue;
                 labels.add(new DayLabel(seriesIndex, point,
-                        formatLineLabel(yValue, percentItemLabels)));
+                        formatLineLabel(yValue, percentItemLabels, valueDecimals)));
             }
             // A day's labels are stacked in the same top-to-bottom order as its data
             // points, so a label never looks attached to a neighbouring line.
@@ -1108,13 +1113,15 @@
         return total;
     }
 
-    private String formatLineLabel(Number value, boolean percentItemLabels) {
+    private String formatLineLabel(Number value, boolean percentItemLabels, int valueDecimals) {
         if (percentItemLabels) {
             NumberFormat formatter = NumberFormat.getPercentInstance();
             formatter.setMaximumFractionDigits(1);
             return formatter.format(value.doubleValue());
         }
-        return new DecimalFormat("#,##0.##").format(value.doubleValue());
+        DecimalFormat formatter = new DecimalFormat("#,##0");
+        formatter.setMaximumFractionDigits(valueDecimals);
+        return formatter.format(value.doubleValue());
     }
 
     private void drawLabelWithHalo(Graphics2D graphics, String text, Rectangle2D rectangle,

--
Gitblit v1.9.1