From dacf6eda316f5d47f571963a054bb5787842e79d Mon Sep 17 00:00:00 2001 From: yu Date: Thu, 9 Jan 2025 15:26:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BAexcel,=E5=8E=BB=E6=8E=89?= =?UTF-8?q?=E5=9B=BE=E8=A1=A8;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- das/pom.xml | 6 - .../impl/StatisticalAnalysisServiceImpl.java | 130 ++---------------- 2 files changed, 13 insertions(+), 123 deletions(-) diff --git a/das/pom.xml b/das/pom.xml index aea4ba76..6666105e 100644 --- a/das/pom.xml +++ b/das/pom.xml @@ -32,7 +32,6 @@ 3.4.4 5.4.3 8.4.3 - 1.5.3 @@ -217,11 +216,6 @@ minio ${minio.version} - - org.jfree - jfreechart - ${jfreechart.version} - diff --git a/das/src/main/java/com/das/modules/page/service/impl/StatisticalAnalysisServiceImpl.java b/das/src/main/java/com/das/modules/page/service/impl/StatisticalAnalysisServiceImpl.java index 22f6c198..b3ee1b0a 100644 --- a/das/src/main/java/com/das/modules/page/service/impl/StatisticalAnalysisServiceImpl.java +++ b/das/src/main/java/com/das/modules/page/service/impl/StatisticalAnalysisServiceImpl.java @@ -28,35 +28,18 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment; -import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xddf.usermodel.chart.*; import org.apache.poi.xssf.usermodel.XSSFChart; -import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFDrawing; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.jetbrains.annotations.NotNull; -import org.jfree.chart.ChartFactory; -import org.jfree.chart.ChartUtils; -import org.jfree.chart.JFreeChart; -import org.jfree.chart.axis.CategoryAxis; -import org.jfree.chart.axis.CategoryLabelPositions; -import org.jfree.chart.plot.CategoryPlot; -import org.jfree.chart.plot.PlotOrientation; -import org.jfree.chart.renderer.category.CategoryItemRenderer; -import org.jfree.chart.title.LegendTitle; -import org.jfree.chart.title.TextTitle; -import org.jfree.data.category.DefaultCategoryDataset; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.awt.*; -import java.io.File; -import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; -import java.util.List; import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -105,13 +88,9 @@ public class StatisticalAnalysisServiceImpl implements StatisticalAnalysisServic List> dataList = new ArrayList<>(); // 遍历数据,填充Excel数据集 setTrendAnalyseExcelValue(mapsList, dataList); - //获取图表类别集 - List chartKey = getCharKey(map); ExcelWriter writer = ExcelUtil.getWriter(RandomUtil.randomInt(100, 1000) + "statistics" + ".xlsx"); //设置Excel样式 setExcelStyle(writer, map, dataList); - //生成折线图 - addChartToExcel(writer,dataList,StatisticalAnalysisConstant.TREND_ANALYSE,chartKey); //下载Excel downloadExcel(response, writer,StatisticalAnalysisConstant.TREND_ANALYSE); } @@ -150,18 +129,9 @@ public class StatisticalAnalysisServiceImpl implements StatisticalAnalysisServic setPowerCurveExcelValue(resultMap, dataList, curveItemEntitieList); //获取功率曲线的列 LinkedHashMap map = getPowerCurveColumnName(param); - //获取图表类别集 - List chartKey = getCharKey(map); ExcelWriter writer = ExcelUtil.getWriter(RandomUtil.randomInt(100, 1000) + "statistics" + ".xlsx"); //设置Excel样式 setExcelStyle(writer, map, dataList); - //生成折线图 - if (param.getDisplayCurve()==1){ - addChartToExcel(writer,dataList,StatisticalAnalysisConstant.POWER_CURVE,chartKey); - }else { - //生成散点图 - addScattersChartToExcel(writer,dataList,StatisticalAnalysisConstant.POWER_CURVE,chartKey); - } //下载Excel downloadExcel(response, writer,StatisticalAnalysisConstant.POWER_CURVE); } @@ -191,15 +161,11 @@ public class StatisticalAnalysisServiceImpl implements StatisticalAnalysisServic //自定义别名 别名的key和实体类中的名称要对应上! LinkedHashMap map = gettrendContrastColumnName(param); List> dataList = new ArrayList<>(); - //图表类别集 - List charKey = getCharKey(map); // 遍历数据,将数据添加到dataList中 setTrendContrastExcelValue(maps, dataList); ExcelWriter writer = ExcelUtil.getWriter(RandomUtil.randomInt(100, 1000) + "statistics" + ".xlsx"); //设置Excel样式 setExcelStyle(writer, map, dataList); - //生成折线图 - addChartToExcel(writer,dataList,StatisticalAnalysisConstant.TREND_CONTRAST,charKey); //下载Excel downloadExcel(response, writer,StatisticalAnalysisConstant.TREND_CONTRAST); } @@ -284,22 +250,7 @@ public class StatisticalAnalysisServiceImpl implements StatisticalAnalysisServic } } - /** - * 获取图表类别集 - * @param map Excel的列 - * @return 图表类别集 - */ - private List getCharKey(LinkedHashMap map) { - //获取图表类别集 - List chartKey = new ArrayList<>(); - for (Map.Entry stringStringEntry : map.entrySet()) { - String value = stringStringEntry.getValue(); - if (!value.contains("时间")){ - chartKey.add(value); - } - } - return chartKey; - } + /** * 趋势对比填充Excel数据集 @@ -542,75 +493,20 @@ public class StatisticalAnalysisServiceImpl implements StatisticalAnalysisServic } /** - * 使用JFreeChart生成折线图(图片),已弃用 - * @param data excel数据集 - * @param writer excel对象 - * @param dataset 图表数据集 - * @param titleStr 标题 + * 获取图表类别集 + * @param map Excel的列 + * @return 图表类别集 */ - private void createChart(List> data, ExcelWriter writer, - DefaultCategoryDataset dataset, String titleStr) { - // 获取Sheet对象 - XSSFSheet xssfSheet = (XSSFSheet) writer.getSheet(); - Workbook workbook = writer.getWorkbook(); - JFreeChart chart = ChartFactory.createLineChart( - titleStr, // 图表标题 - "", // 横轴标签 - "", // 纵轴标签 - dataset, // 数据集 - PlotOrientation.VERTICAL, // 图表方向 - true, // 是否显示图例 - true, // 是否使用工具提示 - false // 是否生成URL链接 - ); - // 设置图表标题的字体 - TextTitle title = chart.getTitle(); - title.setFont(new java.awt.Font("SimSun", java.awt.Font.BOLD, 16)); - // 获取图表的绘图区域 - CategoryPlot plot = chart.getCategoryPlot(); - // 设置横轴标签 - CategoryAxis domainAxis = plot.getDomainAxis(); - domainAxis.setLabelFont(new java.awt.Font("SimSun", java.awt.Font.PLAIN, 12)); - domainAxis.setMaximumCategoryLabelLines(1); // 可以控制标签行数 - domainAxis.setCategoryMargin(3); // 控制类别之间的间距 - domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 旋转横轴标签为45度,90度为:UP_90 - domainAxis.setLabelFont(new Font("SansSerif", Font.PLAIN, 7));//调整字体大小 -// if (data.size() > 50) { -// domainAxis.setVisible(false); // 隐藏横坐标 -// } - // 设置图例的字体 - LegendTitle legend = chart.getLegend(); - if (legend != null) { - legend.setItemFont(new java.awt.Font("SimSun", java.awt.Font.PLAIN, 12)); + private List getCharKey(LinkedHashMap map) { + //获取图表类别集 + List chartKey = new ArrayList<>(); + for (Map.Entry stringStringEntry : map.entrySet()) { + String value = stringStringEntry.getValue(); + if (!value.contains("时间")){ + chartKey.add(value); + } } - // 设置绘图区域的背景颜色 - plot.setBackgroundPaint(Color.WHITE); - // 设置绘图区域的边框 - plot.setOutlinePaint(Color.LIGHT_GRAY); - plot.setOutlineVisible(true); - // 设置网格线的颜色 - plot.setDomainGridlinePaint(Color.LIGHT_GRAY); - plot.setRangeGridlinePaint(Color.LIGHT_GRAY); - // 设置线条的宽度 - CategoryItemRenderer renderer1 = plot.getRenderer(); - renderer1.setSeriesStroke(0, new BasicStroke(2.0f)); // 设置线条宽度 - // 将图表保存为 PNG 文件 - String chartFilePath = "lineChart.png"; - // 调整图表尺寸 - int width = 750; - int height = 400; - try { - ChartUtils.saveChartAsPNG(new File(chartFilePath), chart, width, height); - byte[] bytes = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(chartFilePath)); - int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG); - // 计算数据的最后一行,创建图表插入位置的锚点 - XSSFClientAnchor anchor = new XSSFClientAnchor(0, 1, 0, 1, 0, data.size() + 2, 15, data.size() + 30); - XSSFDrawing drawing = xssfSheet.createDrawingPatriarch(); - drawing.createPicture(anchor, pictureIdx); - } catch (IOException e) { - throw new ServiceException("图表保存失败==" + e); - } - + return chartKey; } /**