From 54e10aee11aa4a10926df02782ca07d41eeb9b98 Mon Sep 17 00:00:00 2001
From: xyc <jc_xiong@hotmail.com>
Date: 星期二, 06 十月 2026 16:19:07 +0800
Subject: [PATCH] fix(holiday): 加大折线图纵向间距并同步 Word 尺寸
---
traffic-audit-server/src/main/java/com/trafficaudit/holiday/service/HolidayTrendChartService.java | 53 +++++++++++++++++++++++++++++++++++++++--------------
1 files changed, 39 insertions(+), 14 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..47bd7ca 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
@@ -49,6 +49,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
+import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
@@ -76,6 +77,8 @@
private static final double RIGHT_LABEL_BAND = 74.0;
private static final double ITEM_LABEL_GAP = 10.0;
private static final float HALO_OUTLINE_WIDTH = 4.0f;
+ private static final int PERCENT_ITEM_LABEL_DECIMALS = 1;
+ private static final double LINE_CHART_HEIGHT_SCALE = 1.20;
private static final Color ITEM_LABEL_COLOR = new Color(55, 55, 55);
private static final Color[] SERIES_COLORS = {
new Color(192, 80, 77),
@@ -95,6 +98,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);
@@ -102,7 +110,7 @@
for (int i = 0; i < item.getValues().size(); i++) {
BigDecimal value = item.getValues().get(i);
if (value != null) {
- xySeries.add(i, value.doubleValue());
+ xySeries.add(i, displayValue(value, percentAxis, valueDecimals).doubleValue());
}
}
dataset.addSeries(xySeries);
@@ -134,8 +142,8 @@
NumberAxis rangeAxis = new NumberAxis();
rangeAxis.setAutoRangeIncludesZero(false);
- rangeAxis.setLowerMargin(0.12);
- rangeAxis.setUpperMargin(0.16);
+ rangeAxis.setLowerMargin(0.04);
+ rangeAxis.setUpperMargin(0.07);
rangeAxis.setTickLabelFont(TICK_FONT);
rangeAxis.setLabelFont(LABEL_FONT);
rangeAxis.setTickLabelPaint(MUTED_TEXT_COLOR);
@@ -148,7 +156,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 +174,8 @@
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
}
plot.setRenderer(renderer);
- return render(chart, width, height, yAxisLabel, true, percentAxis);
+ return render(chart, width, scaleLineChartHeight(height),
+ yAxisLabel, true, percentAxis, valueDecimals);
}
public byte[] pieChart(String valueSuffix, List<ChartCategory> categories,
@@ -212,7 +221,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 +291,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 +454,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 +474,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 +839,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 +868,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 +1117,29 @@
return total;
}
- private String formatLineLabel(Number value, boolean percentItemLabels) {
+ static int scaleLineChartHeight(int height) {
+ return (int) Math.round(height * LINE_CHART_HEIGHT_SCALE);
+ }
+
+ static long scaleLineChartHeight(long height) {
+ return Math.round(height * LINE_CHART_HEIGHT_SCALE);
+ }
+
+ static BigDecimal displayValue(BigDecimal value, boolean percentItemLabels, int valueDecimals) {
+ if (value == null) return null;
+ int scale = percentItemLabels ? PERCENT_ITEM_LABEL_DECIMALS + 2 : valueDecimals;
+ return value.setScale(scale, RoundingMode.HALF_UP);
+ }
+
+ private String formatLineLabel(Number value, boolean percentItemLabels, int valueDecimals) {
if (percentItemLabels) {
NumberFormat formatter = NumberFormat.getPercentInstance();
- formatter.setMaximumFractionDigits(1);
+ formatter.setMaximumFractionDigits(PERCENT_ITEM_LABEL_DECIMALS);
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