[测评系统]--测评系统核心代码库
wzp
2024-08-21 f891000086af5ac150fc120e1f3c781266f1d063
src/main/java/com/ots/project/tool/report/LAQ/chart/LAQAllChart.java
@@ -2,6 +2,7 @@
import com.ots.common.utils.StringUtils;
import com.ots.common.utils.poi.ExcelUtil;
import com.ots.framework.config.EssConfig;
import com.ots.project.tool.report.LAQ.LAQTemplate;
import com.ots.project.tool.report.MAQ.base.RowData;
import com.ots.project.tool.report.MAQ.base.SeriesData;
@@ -9,13 +10,17 @@
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.openxml4j.util.ZipSecureFile;
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.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.drawingml.x2006.chart.*;
import org.openxmlformats.schemas.drawingml.x2006.chartDrawing.CTDrawing;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor;
import java.io.*;
import java.util.*;
@@ -24,6 +29,7 @@
/**
 * 全景图图表渲染
 * (2024.8.21作废)
 */
@Slf4j
public class LAQAllChart {
@@ -35,7 +41,7 @@
     */
    public static void main(String[] args) throws Exception {
        System.out.println("启动测试..");
        String dataPath = "D:\\桌面文件\\LAQ全景图报告\\LAQ导入测试数据.xlsx";
        String dataPath = "D:\\桌面文件\\LAQ全景图报告\\53208d066321f94f47deecec7e9c6907.xlsx";
        // 获取导入数据
        File file = new File(dataPath);
        InputStream in = new FileInputStream(file);
@@ -66,19 +72,111 @@
     * @param textMap 数据源
     */
    public static void changeChart(XWPFDocument document, Map<String, Object> textMap) {
//        Map<Integer, List<SeriesData>> seriesDatas = initData(textMap);
//        changeChart(document,seriesDatas,textMap);
        System.out.println("进入全景图渲染模式");
        log.info("【进入全景图渲染模式】");
        try{
            List<LAQTemplate> laqTemplateList = (List<LAQTemplate>)textMap.get("allList");
            changeChartNew(document,laqTemplateList);
            //changeChartNew(document,laqTemplateList);
            updateImage(document);
            System.out.println("全景图渲染完成");
        }
        catch (Exception ex)
        {
            log.error("LAQ全景图异常:"+ex.getMessage());
        }
    }
    public static void updateImage(XWPFDocument document){
        //替换图片
        try{
            // 获取所有段落
            List<XWPFParagraph> paragraphs = document.getParagraphs();
            //获取图片路径
//            String selectedPic = EssConfig.getReportTemplates()+"apifanSelected.png";
//            String defaultPic =EssConfig.getReportTemplates()+"apifanDefalut.png";
            //测试用的
//            String img1 = "D:\\桌面文件\\LAQ全景图报告\\111.png";
//            String img2 ="D:\\桌面文件\\LAQ全景图报告\\222.png";
//            String img3 ="D:\\桌面文件\\LAQ全景图报告\\333.png";
            //  /home/data
            String img1 = EssConfig.getReportTemplates()+"111.png";
            String img2 =EssConfig.getReportTemplates()+"222.png";
            String img3 =EssConfig.getReportTemplates()+"333.png";
            // 遍历段落进行替换操作
            synchronized (paragraphs) {
                for (XWPFParagraph paragraph : paragraphs) {
                    String text = paragraph.getText();
                    log.info("【获取到文本】:"+text);
                    // 将图片插入到段落中
                    int width = Units.toEMU(580); // 图片宽度
                    int height = Units.toEMU(700); // 图片高度
                    //用%%号在前面就被替换了
                    if (text.contains("{{box0}}")) {  // 检查是否包含要替换的文字变量
                        log.info("【获取到box0】:"+img1);
                        // 获取所有运行的列表
                        List<XWPFRun> runs = paragraph.getRuns();
                        // 移除所有运行
                        for (int i = runs.size() - 1; i >= 0 ; i--) {
                            paragraph.removeRun(i);
                        }
                        //创建Random类对象
                        Random random = new Random();
                        //产生随机数
                        int number = random.nextInt(999) + 1;
                        // 创建图片对象
                        XWPFRun run = paragraph.createRun();
                        int pictureType = Document.PICTURE_TYPE_PNG; // 图片类型
                        run.addPicture(new FileInputStream(img1), pictureType, "Seal"+number, width, height);
                    }
                    if (text.contains("{{box1}}")) {  // 检查是否包含要替换的文字变量
                        // 获取所有运行的列表
                        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; // 图片类型
                        run.addPicture(new FileInputStream(img2), pictureType, "image.png", width, height);
                    }
                    if (text.contains("{{box2}}")) {  // 检查是否包含要替换的文字变量
                        // 获取所有运行的列表
                        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; // 图片类型
                        run.addPicture(new FileInputStream(img3), pictureType, "image.png", width, height);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            log.info("【异常】"+ex.getMessage());
        }
    }
@@ -373,11 +471,13 @@
                perfomance = 0.0;
            }
            String name = laqTemplate.getName();//标签名字。考虑到英文会比较长,会根据长度做适配
            //如果y轴小于1,要设置成3,往上移动一下
            //如果y轴小于1,要设置成3,往上移动一下。y轴调整
            if(perfomance<=1.2)
            {
                perfomance=perfomance+1.5;
@@ -385,7 +485,7 @@
            //如果y轴大于97,设置成97,往下移动一点,好看
            if(perfomance>=99)
            {
                perfomance=99.0;
                perfomance=98.0;
            }
            if(perfomance>33.0&&perfomance<34.0)
@@ -405,12 +505,30 @@
                //x轴要往右边移动
                if(olas>67.0&olas<=70.0)
                {
                    olas=olas+1.5;
                }
                    //根据名字长度进行位移
                    double len = Math.ceil(name.length()/10.0)+1.0;
                    if(name.length()>15)
                    {
                        System.out.println("取整值:"+len);
                        olas=olas+len;
                    }
                    else
                    {
                        olas=olas+1.5;
                    }
                }
                if(olas>=99.0)
                {
                    olas = olas-2.0;
                    if(name.length()>15)
                    {
                        olas = olas-3.0;
                    }
                    else {
                        olas = olas-2.0;
                    }
                }
            }
@@ -419,26 +537,59 @@
                //x轴要往右边移动
                if(olas>=33.0&olas<=35.0)
                {
                    olas= olas+1;
                    //根据名字长度进行位移
                    double len = Math.ceil(name.length()/10.0)+1.0;
                    if(name.length()>15)
                    {
                        System.out.println("取整值:"+len);
                        olas=olas+len;
                    }
                    else
                    {
                        olas=olas+1.5;
                    }
                }
                if(olas>66.0&olas<=67.0)
                {
                    olas = olas -2.0;
                    if(name.length()>15)
                    {
                        olas = olas-3.0;
                    }
                    else {
                        olas = olas-2.0;
                    }
                }
            }
            if(type==3)
            {
                //x轴要往右边移动
                if(olas>0.0 && olas<=3.0)
                if(olas>=0.0 && olas<=3.0)
                {
                    olas= olas + 1;
                    double len = Math.ceil(name.length()/10.0)+1.0;
                    if(name.length()>15)
                    {
                        System.out.println("取整值:"+len);
                        olas=olas+len;
                    }
                    else
                    {
                        olas=olas+1.5;
                    }
                }
                if(olas>=32.0&olas<=33.0)
                {
                    olas = olas -2.0;
                    if(name.length()>15)
                    {
                        olas = olas-3.0;
                    }
                    else {
                        olas = olas-2.0;
                    }
                }
            }
@@ -526,7 +677,7 @@
            {
                if(olas>67)
                {
                    seriesDataMapLables.put(dataIndex,laqTemplate.getName()+"\n("+olasStr+"-"+perfomanceStr+")");
                    seriesDataMapLables.put(dataIndex,laqTemplate.getName()+"("+olasStr+"-"+perfomanceStr+")");
                    dataIndex++;
                }
            }
@@ -535,7 +686,7 @@
            {
                if(olas<=67 && olas>33)
                {
                    seriesDataMapLables.put(dataIndex,laqTemplate.getName()+"\n("+olasStr+"-"+perfomanceStr+")");
                    seriesDataMapLables.put(dataIndex,laqTemplate.getName()+"("+olasStr+"-"+perfomanceStr+")");
                    dataIndex++;
                }
            }
@@ -544,7 +695,7 @@
            {
                if(olas<=33)
                {
                    seriesDataMapLables.put(dataIndex,laqTemplate.getName()+"\n("+olasStr+"-"+perfomanceStr+")");
                    seriesDataMapLables.put(dataIndex,laqTemplate.getName()+"("+olasStr+"-"+perfomanceStr+")");
                    dataIndex++;
                }