diff --git a/src/main/java/cn/teammodel/TeamModelExtensionApplication.java b/src/main/java/cn/teammodel/TeamModelExtensionApplication.java index 2ee5074..b25bb74 100644 --- a/src/main/java/cn/teammodel/TeamModelExtensionApplication.java +++ b/src/main/java/cn/teammodel/TeamModelExtensionApplication.java @@ -10,6 +10,4 @@ public class TeamModelExtensionApplication { SpringApplication.run(TeamModelExtensionApplication.class, args); } - - } diff --git a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java index 8598c49..ab0255b 100644 --- a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java @@ -31,7 +31,6 @@ import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.jfree.chart.ChartUtils; import org.jfree.chart.JFreeChart; -import org.jfree.chart.plot.PiePlot; import org.jfree.data.general.DefaultPieDataset; import org.springframework.core.io.ClassPathResource; import org.springframework.data.domain.Page; @@ -41,14 +40,12 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; -import java.awt.*; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.time.Instant; import java.time.LocalDate; import java.util.*; -import java.util.List; import java.util.stream.Collectors; /** @@ -659,10 +656,6 @@ public class EvaluationServiceImpl implements EvaluationService { praiseDistributionDataset.setValue(FiveEducations.ART.getName(), reportVo.getPraiseDistribution().get(FiveEducations.ART.getCode())); praiseDistributionDataset.setValue(FiveEducations.LABOUR.getName(), reportVo.getPraiseDistribution().get(FiveEducations.LABOUR.getCode())); JFreeChart praisePieChart = ChartUtil.pieChart("五育表扬指标分布", praiseDistributionDataset); - PiePlot plot1 = (PiePlot) praisePieChart.getPlot(); - plot1.setNoDataMessage("无数据"); - plot1.setNoDataMessageFont(new Font("宋体", Font.PLAIN, 18)); - ByteArrayOutputStream praiseBos = new ByteArrayOutputStream(); ChartUtils.writeChartAsJPEG(praiseBos, praisePieChart, 340, 330); @@ -674,11 +667,6 @@ public class EvaluationServiceImpl implements EvaluationService { criticalDistributionDataset.setValue(FiveEducations.ART.getName(), reportVo.getCriticalDistribution().get(FiveEducations.ART.getCode())); criticalDistributionDataset.setValue(FiveEducations.LABOUR.getName(), reportVo.getCriticalDistribution().get(FiveEducations.LABOUR.getCode())); JFreeChart criticalPieChart = ChartUtil.pieChart("五育待改进指标分布", criticalDistributionDataset); - - PiePlot plot2 = (PiePlot) criticalPieChart.getPlot(); - plot2.setNoDataMessage("无数据"); - plot2.setNoDataMessageFont(new Font("宋体", Font.PLAIN, 18)); - ByteArrayOutputStream criticalBos = new ByteArrayOutputStream(); ChartUtils.writeChartAsJPEG(criticalBos, criticalPieChart, 340, 330); diff --git a/src/main/java/cn/teammodel/utils/ChartUtil.java b/src/main/java/cn/teammodel/utils/ChartUtil.java index fc7796a..923f341 100644 --- a/src/main/java/cn/teammodel/utils/ChartUtil.java +++ b/src/main/java/cn/teammodel/utils/ChartUtil.java @@ -15,8 +15,10 @@ import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; import java.awt.*; +import java.io.InputStream; import java.text.DecimalFormat; import java.text.NumberFormat; +import java.util.Objects; /** * 生成图表工具类 @@ -47,14 +49,31 @@ public class ChartUtil { new Color(165, 165, 165), }; + public static Font loadFontFromFile() { + try { + String fontFilePath = "/fonts/simsun.ttf"; + // 创建 TrueTypeFont 对象,从指定文件路径加载字体 + InputStream fontInputStream = ChartUtil.class.getResourceAsStream(fontFilePath); + assert fontInputStream != null; + return Font.createFont(Font.TRUETYPE_FONT, fontInputStream).deriveFont(Font.BOLD, 14); + } catch (Exception e) { + e.printStackTrace(); + // 处理异常情况,例如文件不存在或加载失败 + return null; + } + } private static StandardChartTheme initChartTheme(){ + + // 加载字体文件 + Font customFont = loadFontFromFile(); StandardChartTheme currentTheme = new StandardChartTheme("JFree"); // 横轴纵轴标题文字大小 - currentTheme.setLargeFont(new Font("宋体", Font.BOLD, 15)); + assert customFont != null; + currentTheme.setLargeFont(customFont); // 横轴纵轴数值文字大小 - currentTheme.setRegularFont(new Font("宋体", Font.PLAIN, 13)); - currentTheme.setExtraLargeFont(new Font("宋体", Font.BOLD, 20)); + currentTheme.setRegularFont(customFont); + currentTheme.setExtraLargeFont(customFont); // 背景颜色 currentTheme.setPlotBackgroundPaint(new Color(255, 255, 204, 0)); // 边框线条 @@ -175,6 +194,8 @@ public class ChartUtil { plot.setShadowPaint(new Color(255, 255, 255, 0)); // 添加标签数字百分比显示 plot.setLabelGenerator(new StandardPieSectionLabelGenerator(("{0}{2}"), NumberFormat.getNumberInstance(),new DecimalFormat("0.00%"))); + plot.setNoDataMessage("无数据"); + plot.setNoDataMessageFont(Objects.requireNonNull(ChartUtil.loadFontFromFile()).deriveFont(Font.BOLD, 18)); return chart; } } diff --git a/src/main/resources/fonts/simsun.ttf b/src/main/resources/fonts/simsun.ttf new file mode 100644 index 0000000..e0115ab Binary files /dev/null and b/src/main/resources/fonts/simsun.ttf differ