| | |
| | | package com.ots.project.tool.report.APIFan.chart; |
| | | |
| | | import com.ots.framework.config.EssConfig; |
| | | import com.ots.project.tool.report.MAQ.base.RowData; |
| | | import com.ots.project.tool.report.MAQ.base.SeriesData; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.ooxml.POIXMLDocument; |
| | | import org.apache.poi.ooxml.POIXMLDocumentPart; |
| | | import org.apache.poi.util.Units; |
| | | import org.apache.poi.xssf.usermodel.XSSFCell; |
| | | import org.apache.poi.xssf.usermodel.XSSFRow; |
| | | import org.apache.poi.xssf.usermodel.XSSFSheet; |
| | | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| | | import org.apache.poi.xwpf.usermodel.XWPFChart; |
| | | import org.apache.poi.xwpf.usermodel.XWPFDocument; |
| | | import org.apache.poi.xwpf.usermodel.*; |
| | | import org.openxmlformats.schemas.drawingml.x2006.chart.*; |
| | | import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; |
| | | |
| | | import java.io.FileInputStream; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.math.BigInteger; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | |
| | | |
| | | //自我认知准确度 |
| | | private static String p_sdecepT1; |
| | | private static String p_sdecept10; |
| | | private static String p_sdecep10; |
| | | //自我阐述的真实度 |
| | | private static String p_impmgT1; |
| | | private static String p_impmg11; |
| | |
| | | textMap.put("P_Emotct07","60"); |
| | | textMap.put("P_Stress08","61"); |
| | | textMap.put("P_SdecepT1","62"); |
| | | textMap.put("P_Sdecept10","63"); |
| | | textMap.put("P_Sdecep10","63"); |
| | | textMap.put("P_ImpmgT1","64"); |
| | | textMap.put("P_Impmg11","65"); |
| | | textMap.put("BST1","66"); |
| | | textMap.put("BST2","67"); |
| | | textMap.put("Box","1"); |
| | | changeChart(document,textMap); |
| | | document.write(os); |
| | | }catch (Exception e){ |
| | |
| | | return false; |
| | | } |
| | | //图表数据 |
| | | changePAQChart(document,seriesDatas); |
| | | changeAPIFanChart(document,seriesDatas); |
| | | |
| | | |
| | | //替换图片 |
| | | try{ |
| | | // 获取所有段落 |
| | | List<XWPFParagraph> paragraphs = document.getParagraphs(); |
| | | |
| | | //获取图片路径 |
| | | String selectedPic = EssConfig.getReportTemplates()+"apifanSelected.png"; |
| | | String defaultPic =EssConfig.getReportTemplates()+"apifanDefalut.png"; |
| | | |
| | | //测试用的 |
| | | // String selectedPic = "C:\\测评系统\\home\\reportTemplates\\apifanSelected.png"; |
| | | // String defaultPic ="C:\\测评系统\\home\\reportTemplates\\apifanDefalut.png"; |
| | | |
| | | |
| | | // 遍历段落进行替换操作 |
| | | synchronized (paragraphs) { |
| | | for (XWPFParagraph paragraph : paragraphs) { |
| | | String text = paragraph.getText(); |
| | | |
| | | //用%%号在前面被替换了 |
| | | if (text.contains("{{box0}}")) { // 检查是否包含要替换的文字变量 |
| | | String picturePath = ""; // 图片路径 |
| | | |
| | | String Box = (String) textMap.get("Box"); |
| | | int box = Integer.parseInt(Box); |
| | | if(box== 0) |
| | | { |
| | | picturePath =selectedPic; |
| | | } |
| | | else |
| | | { |
| | | picturePath = defaultPic; |
| | | } |
| | | |
| | | // 创建新的图片段落 |
| | | // XWPFParagraph paragraph = document.createParagraph(); |
| | | // 获取所有运行的列表 |
| | | List<XWPFRun> runs = paragraph.getRuns(); |
| | | // 移除所有运行 |
| | | for (int i = runs.size() - 1; i >= 0 ; i--) { |
| | | paragraph.removeRun(i); |
| | | } |
| | | // 创建图片对象 |
| | | XWPFRun run = paragraph.createRun(); |
| | | int pictureType = Document.PICTURE_TYPE_PNG; // 图片类型 |
| | | |
| | | // 将图片插入到段落中 |
| | | int width = Units.toEMU(20); // 图片宽度 |
| | | int height = Units.toEMU(20); // 图片高度 |
| | | run.addPicture(new FileInputStream(picturePath), pictureType, "image.png", width, height); |
| | | |
| | | // 添加空格或制表符,使文本居中对齐 |
| | | int numberOfSpaces = 3; |
| | | for (int i = 0; i < numberOfSpaces; i++) { |
| | | run.setText(" "); |
| | | } |
| | | // 设置字符间距为居中对齐 |
| | | paragraph.setVerticalAlignment(TextAlignment.CENTER); |
| | | run.setText("结果可信,可以直接解读(测试中未发现明显的“伪装好”倾向)"); |
| | | |
| | | } |
| | | if (text.contains("{{box1}}")) { // 检查是否包含要替换的文字变量 |
| | | String picturePath = ""; // 图片路径 |
| | | |
| | | String Box = (String) textMap.get("Box"); |
| | | int box = Integer.parseInt(Box); |
| | | if(box== 1) |
| | | { |
| | | picturePath =selectedPic; |
| | | } |
| | | else |
| | | { |
| | | picturePath = defaultPic; |
| | | } |
| | | |
| | | // 创建新的图片段落 |
| | | // XWPFParagraph paragraph = document.createParagraph(); |
| | | // 获取所有运行的列表 |
| | | List<XWPFRun> runs = paragraph.getRuns(); |
| | | // 移除所有运行 |
| | | for (int i = runs.size() - 1; i >= 0 ; i--) { |
| | | paragraph.removeRun(i); |
| | | } |
| | | // 创建图片对象 |
| | | XWPFRun run = paragraph.createRun(); |
| | | int pictureType = Document.PICTURE_TYPE_PNG; // 图片类型 |
| | | |
| | | // 将图片插入到段落中 |
| | | int width = Units.toEMU(20); // 图片宽度 |
| | | int height = Units.toEMU(20); // 图片高度 |
| | | run.addPicture(new FileInputStream(picturePath), pictureType, "image.png", width, height); |
| | | // 添加空格或制表符,使文本居中对齐 |
| | | int numberOfSpaces = 3; |
| | | for (int i = 0; i < numberOfSpaces; i++) { |
| | | run.setText(" "); |
| | | } |
| | | // 设置字符间距为居中对齐 |
| | | paragraph.setVerticalAlignment(TextAlignment.CENTER); |
| | | run.setText("结果不可信(测试中有明显的“伪装好”倾向)"); |
| | | |
| | | } |
| | | if (text.contains("{{box2}}")) { // 检查是否包含要替换的文字变量 |
| | | String picturePath = ""; // 图片路径 |
| | | |
| | | String Box = (String) textMap.get("Box"); |
| | | int box = Integer.parseInt(Box); |
| | | if(box== 2) |
| | | { |
| | | picturePath =selectedPic; |
| | | } |
| | | else |
| | | { |
| | | picturePath = defaultPic; |
| | | } |
| | | |
| | | // 创建新的图片段落 |
| | | // XWPFParagraph paragraph = document.createParagraph(); |
| | | // 获取所有运行的列表 |
| | | List<XWPFRun> runs = paragraph.getRuns(); |
| | | // 移除所有运行 |
| | | for (int i = runs.size() - 1; i >= 0 ; i--) { |
| | | paragraph.removeRun(i); |
| | | } |
| | | // 创建图片对象 |
| | | XWPFRun run = paragraph.createRun(); |
| | | int pictureType = Document.PICTURE_TYPE_PNG; // 图片类型 |
| | | |
| | | // 将图片插入到段落中 |
| | | int width = Units.toEMU(20); // 图片宽度 |
| | | int height = Units.toEMU(20); // 图片高度 |
| | | run.addPicture(new FileInputStream(picturePath), pictureType, "image.png", width, height); |
| | | // 添加空格或制表符,使文本居中对齐 |
| | | int numberOfSpaces = 3; |
| | | for (int i = 0; i < numberOfSpaces; i++) { |
| | | run.setText(" "); |
| | | } |
| | | // 设置字符间距为居中对齐 |
| | | paragraph.setVerticalAlignment(TextAlignment.CENTER); |
| | | run.setText("结果可能可信,解读时要谨慎(测试中有一定的“伪装好倾向”)"); |
| | | |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | System.out.println(ex.getMessage()); |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | |
| | | p_stress08 = (String) textMap.get("P_Stress08"); |
| | | |
| | | p_sdecepT1 = (String) textMap.get("P_SdecepT1"); |
| | | p_sdecept10 = (String) textMap.get("P_Sdecept10"); |
| | | p_sdecep10 = (String) textMap.get("P_Sdecep10"); |
| | | p_impmgT1 = (String) textMap.get("P_ImpmgT1"); |
| | | p_impmg11 = (String) textMap.get("P_Impmg11"); |
| | | bst1 = (String) textMap.get("BST1"); |
| | | bst2 = (String) textMap.get("BST2"); |
| | | |
| | | String[] paramValue = new String[]{p_total, p_retent09, p_social01,p_cooper02,p_analth03,p_initia04, |
| | | p_persis05,p_depend06,p_emotct07,p_stress08,p_sdecepT1, p_sdecept10,p_impmgT1,p_impmg11,bst1,bst2}; |
| | | p_persis05,p_depend06,p_emotct07,p_stress08,p_sdecepT1, p_sdecep10,p_impmgT1,p_impmg11,bst1,bst2}; |
| | | |
| | | //判断是否为空 |
| | | if (checkParamsIsNull(paramValue)) |
| | |
| | | Double P_Emotct07 = Double.valueOf(p_emotct07); |
| | | Double P_Stress08 = Double.valueOf(p_stress08); |
| | | Double P_SdecepT1 = Double.valueOf(p_sdecepT1); |
| | | Double P_Sdecept10 = Double.valueOf(p_sdecept10); |
| | | Double P_Sdecep10 = Double.valueOf(p_sdecep10); |
| | | Double P_ImpmgT1 = Double.valueOf(p_impmgT1); |
| | | Double P_Impmg11 = Double.valueOf(p_impmg11); |
| | | Double BST1 = Double.valueOf(bst1); |
| | |
| | | |
| | | )), |
| | | new SeriesData("自我认知的准确度", Arrays.asList( |
| | | new RowData("自我认知的准确度", P_Sdecept10),//顺序要倒置 |
| | | new RowData("自我认知的准确度", P_Sdecep10),//顺序要倒置 |
| | | new RowData("自我认知的准确度", P_SdecepT1)//顺序要倒置 |
| | | |
| | | )), |
| | |
| | | * @param doc |
| | | * @param seriesDatas |
| | | */ |
| | | public static void changePAQChart(XWPFDocument doc,List<SeriesData> seriesDatas) { |
| | | public static void changeAPIFanChart(XWPFDocument doc,List<SeriesData> seriesDatas) { |
| | | try { |
| | | //动态刷新图表 |
| | | List<POIXMLDocumentPart> relations = doc.getRelations(); |
| | |
| | | // excel内置表格 |
| | | XSSFWorkbook workbook = chart.getWorkbook(); |
| | | XSSFSheet sheet = workbook.getSheetAt(0); |
| | | |
| | | String sheetName = sheet.getSheetName(); |
| | | |
| | | //柱形图 |
| | | if (!plot.getBarChartList().isEmpty()) { |
| | | CTBarChart barChart = plot.getBarChartArray(0); |
| | | //刷新内置excel数据 |
| | | List<SeriesData> excelData = new ArrayList<>(); |
| | | excelData.add(seriesDatas.get(k)); |
| | | //excelData.add(seriesDatas.get(k)); |
| | | |
| | | //通过sheetName来解决匹配多表的问题 |
| | | SeriesData seriesData = seriesDatas.stream() |
| | | .filter(item -> item.getName().equals(sheetName)) |
| | | .findFirst() |
| | | .orElse(null); |
| | | excelData.add(seriesData); |
| | | refreshExcel(excelData, workbook, sheet); |
| | | workbook.write(chart.getPackagePart().getOutputStream()); |
| | | |
| | | //柱形图数据源 |
| | | List<SeriesData> barDatas = seriesDatas; |
| | | // List<SeriesData> barDatas = seriesDatas; |
| | | int i = 0; |
| | | for (CTBarSer ser : barChart.getSerList()) { |
| | | //更新柱形图数据缓存 |
| | | updateBarChart(barDatas.get(k), ser.getVal()); |
| | | updateBarChart(seriesData, ser.getVal()); |
| | | ++i; |
| | | } |
| | | |