P15GEN2\59518
2025-10-10 9f6890646993d16260d4201d613c092132856127
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package foundation.io.file.ppt;
 
import java.util.List;
import java.util.Map;
 
import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFChartData.Series;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
import org.apache.poi.xslf.usermodel.XSLFChart;
import org.apache.poi.xslf.usermodel.XSLFGraphicFrame;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;
 
import foundation.io.engine.IShapeWriter;
 
public class GraphicWriter extends IShapeWriter {
    private XSLFGraphicFrame graphicShape;
 
    public GraphicWriter(XSLFGraphicFrame shape) {
        super(shape);
        this.graphicShape = shape;
        
        //parse tables (header, mapping fields
        XSLFChart chart = shape.getChart();
        
        // 系列1
        XDDFChartData chartData = chart.getChartSeries().get(0);
        for (Series series : chartData.getSeries()) {
            XDDFDataSource<String> categorys = (XDDFDataSource<String>) series.getCategoryData();
        }
        
        // x 轴
//        XDDFDataSource<String> newCategories = chart.getChartSeries();
    }
 
    public void replaceTextKeepStyle(XSLFTextShape shape, String placeholder, String replacement) {
        // 获取所有文本段落
        List<XSLFTextParagraph> paragraphs = shape.getTextParagraphs();
 
        for (XSLFTextParagraph paragraph : paragraphs) {
            // 获取段落中的所有文本块
            List<XSLFTextRun> runs = paragraph.getTextRuns();
 
            for (XSLFTextRun run : runs) {
                String text = run.getRawText();
                if (text != null && text.contains(placeholder)) {
                    // 保留原有样式替换文本
                    placeholder = "${" + placeholder + "}";
                    run.setText(text.replace(placeholder, replacement));
                }
            }
        }
    }
 
    @Override
    public XSLFGraphicFrame getShape(){
        return graphicShape;
    }
 
    @Override
    public void writeData(XSLFSlide slide, Map<String, Object> data) throws Exception {
        XSLFGraphicFrame frame = (XSLFGraphicFrame) shape;
        if (frame.getChart() != null) {
            XSLFChart chart = frame.getChart();
 
            // 获取或创建图表数据
            XDDFChartData chartData = chart.getChartSeries().get(0);
            List<XDDFChartData.Series> chartSeries = chartData.getSeries();
            for (XDDFChartData.Series chartSerie : chartSeries){
                // 修改数据
                XDDFDataSource<String> newCategories = XDDFDataSourcesFactory.fromArray(
                        new String[]{"Jan", "Feb", "Mar", "Apr"});
                XDDFNumericalDataSource<Double> values =
                        XDDFDataSourcesFactory.fromArray(new Double[]{15.0, 25.0, 35.0, 45.0});
 
                chartSerie.getCategoryData();
                chartSerie.replaceData(newCategories, values);
            }
 
            chart.plot(chartData);
        }
    }
 
}