fix: 添加字体文件,用代码让JChartFree用上simsun字体

11111
winter 10 months ago
parent 7aa14b1b82
commit 1a006ee304

@ -10,6 +10,4 @@ public class TeamModelExtensionApplication {
SpringApplication.run(TeamModelExtensionApplication.class, args);
}
}

@ -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);

@ -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;
}
}

Loading…
Cancel
Save