From c0dbb0919f55662751009d66641657c5828cc65e Mon Sep 17 00:00:00 2001
From: cjl <276999030@qq.com>
Date: Thu, 03 Aug 2023 15:08:56 +0800
Subject: [PATCH] feat:空气质量报告
---
screen-api/pom.xml | 25 ++++++-
screen-api/src/main/java/com/moral/api/controller/SysTestController.java | 104 +++++++++++++++++++++++++++++++++-
2 files changed, 119 insertions(+), 10 deletions(-)
diff --git a/screen-api/pom.xml b/screen-api/pom.xml
index 70f3c4b..d55d642 100644
--- a/screen-api/pom.xml
+++ b/screen-api/pom.xml
@@ -24,11 +24,6 @@
<!-- easyexcel-->
<dependency>
<groupId>org.apache.poi</groupId>
- <artifactId>poi</artifactId>
- <version>4.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
@@ -52,6 +47,26 @@
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.0.0</version>
</dependency>
+ <dependency>
+ <groupId>cn.afterturn</groupId>
+ <artifactId>easypoi-base</artifactId>
+ <version>4.1.0</version>
+ </dependency>
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ <version>19.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jfree</groupId>
+ <artifactId>jcommon</artifactId>
+ <version>1.0.24</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jfree</groupId>
+ <artifactId>jfreechart</artifactId>
+ <version>1.5.0</version>
+ </dependency>
</dependencies>
<build>
diff --git a/screen-api/src/main/java/com/moral/api/controller/SysTestController.java b/screen-api/src/main/java/com/moral/api/controller/SysTestController.java
index 8071454..aa0e1f3 100644
--- a/screen-api/src/main/java/com/moral/api/controller/SysTestController.java
+++ b/screen-api/src/main/java/com/moral/api/controller/SysTestController.java
@@ -1,5 +1,6 @@
package com.moral.api.controller;
+import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.word.WordExportUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
@@ -13,19 +14,27 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
+import org.jfree.chart.ChartFactory;
+import org.jfree.chart.ChartUtils;
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.StandardChartTheme;
+import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
+import org.jfree.chart.plot.PiePlot;
+import org.jfree.data.general.DefaultPieDataset;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.OutputStream;
+import java.awt.*;
+import java.io.*;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.*;
+import java.util.List;
import java.util.stream.Collectors;
/**
@@ -123,6 +132,26 @@
}
}
+ @GetMapping("resultWordMonth")
+ public void resultWordMonth(HttpServletResponse response) {
+ String ks = "2020-06-01";
+ Date ksDate = DateUtils.convertDate(ks);
+
+ Date jsDate = DateUtils.getLastDayOfCurrMonth(ksDate);;
+ String time = DateUtils.dateToDateString(ksDate,DateUtils.yyyy_MM_dd_EN);
+ String endTime = DateUtils.dateToDateString(jsDate,DateUtils.yyyy_MM_dd_EN);
+ List<SysTest> listAll = sysTestService.listAllAvg(time,endTime,null);
+ if(CollectionUtils.isEmpty(listAll)){
+ return;
+ }
+ String fileName = "������������������.docx";
+ String time1 = DateUtils.dateToDateString(ksDate,"yyyy.MM.dd");
+ String endTime1= DateUtils.dateToDateString(jsDate,"yyyy.MM.dd");
+ Map<String,Object> map = resultWeekMap(listAll,1,time1+"-"+endTime1);
+ downloadWord(fileName, map, response,"������������������"+time+".docx");
+ //ksDate = DateUtils.addDays(ksDate,7);
+
+ }
public static void downloadWord(String fileName, Map<String, Object> params, HttpServletResponse response,String newFileName) {
String path = getPath(fileName);//��������� resources������
@@ -238,8 +267,13 @@
resultMap.put(name+"TV", StringUtils.isNotEmpty(s.getVoc())?s.getVoc():"--");
resultMap.put(name+"S", StringUtils.isNotEmpty(s.getSu())?s.getSu():"--");
}
- String bodyName = stringButterStrWeek(listAll);
- resultMap.put("������������",bodyName);
+ // String bodyName = stringButterStrWeek(listAll);
+ HashMap<String, Integer> datas = new HashMap<>(3);
+ datas.put("������",10);
+ datas.put("������",20);
+ datas.put("������",40);
+ ImageEntity imageEntity = pieChart("������",datas, 500, 300);
+ resultMap.put("������������",imageEntity);
return resultMap;
}
private String stringButterStr(List<SysTest> listAll){
@@ -463,6 +497,66 @@
return map;
}
+ private static String tempImgPath="E:\\home\\image\\tempJfree.jpg";
+ private static byte[] imgToByte(){
+ File file = new File(tempImgPath);
+ byte[] buffer = null;
+ try {
+ FileInputStream fis = new FileInputStream(file);
+ ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
+ byte[] b = new byte[1000];
+ int n;
+ while ((n = fis.read(b)) != -1) {
+ bos.write(b, 0, n);
+ }
+ fis.close();
+ bos.close();
+ buffer = bos.toByteArray();
+ } catch (IOException e) {
+ log.error(e.getMessage());
+ }
+ //������������������
+ //file.delete();
+ return buffer;
+ }
+ public ImageEntity pieChart(String title, Map<String, Integer> datas, int width, int height) {
+
+ //������������������
+ StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
+ //������������������
+ standardChartTheme.setExtraLargeFont(new Font("������", Font.BOLD, 20));
+ //���������������������
+ standardChartTheme.setRegularFont(new Font("������", Font.PLAIN, 15));
+ //���������������������
+ standardChartTheme.setLargeFont(new Font("������", Font.PLAIN, 15));
+ //������������������
+ ChartFactory.setChartTheme(standardChartTheme);
+
+ //������jfree���������������������������
+ DefaultPieDataset pds = new DefaultPieDataset();
+ datas.forEach(pds::setValue);
+ //���������������������������������������������������������������������tooltips������������������������
+ JFreeChart chart = ChartFactory.createPieChart(title, pds, true, false, false);
+ //���������������
+ chart.setTextAntiAlias(false);
+ PiePlot plot = (PiePlot) chart.getPlot();
+ plot.setNoDataMessage("������������");
+ //���������������������
+ plot.setIgnoreNullValues(true);
+ plot.setBackgroundAlpha(0f);
+ //������������������������
+ plot.setShadowPaint(new Color(255,255,255));
+ //���������������������(������{0})
+ plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1})/{2}"));
+ try {
+ ChartUtils.saveChartAsJPEG(new File(tempImgPath), chart, width, height);
+ } catch (IOException e1) {
+ log.error("������������������������");
+ }
+ ImageEntity imageEntity = new ImageEntity(imgToByte(), width, height);
+ Assert.notNull(imageEntity.getData(),"������������������������������");
+ return imageEntity;
+ }
public static void main(String[] args) {
/*String ks = "2020-06-01";
--
Gitblit v1.8.0