环境:Aspose.Words For Java 23.7.jar、JAVA1.8
原型如下:
image.png (15.1 KB)
生成的形状如下:
image.png (14.4 KB)
目前问题1:我在代码中设置了循环设置每个系列的颜色,但是没有生效。
问题2:请问在形状中如何插入像原型中一样的数据表格。
目前我用的是23.7版本。
代码如下:
public class Demo9 {
public static void main(String[] args) {
try {
// 创建一个空白文档
Document doc = new Document("output/school_template.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
List<GxyItemData> rowData = AttributeData.list17();
setBarChart(builder, "第一个标题", new String[]{"2021-2022", "2022-2023", "2023-2024"}, rowData, 490, 640);
// 保存文档
String fileName = "output/AsposeWord1" + new SimpleDateFormat("MMddHHmmss").format(new Date()) + ".docx";
doc.save(fileName);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setBarChart(DocumentBuilder builder, String title, String[] categories, List<GxyItemData> entity, double width, double height) throws Exception {
Shape shape = builder.insertChart(ChartType.LINE, 432, 252);
shape.setWrapSide(WrapSide.BOTH);
shape.setWrapType(WrapType.TOP_BOTTOM);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setAllowOverlap(false);
shape.getStroke().setColor(new Color(221, 221, 221)); // 设置线条颜色
shape.getFill().setColor(new Color(222, 235, 247)); // 设置填充颜色
Chart chart = shape.getChart();
chart.getTitle().setText(title);
chart.getAxisX().getNumberFormat().setFormatCode("0.00%");
chart.getAxisY().getNumberFormat().setFormatCode("0.00%");
ChartSeriesCollection seriesColl = chart.getSeries();
seriesColl.clear();
entity.forEach(c -> { c.setValue4(c.getValue4() == null ? new double[0] : c.getValue4());});
double maxY = entity.stream().mapToDouble(s -> { return Arrays.stream(s.getValue4()).max().getAsDouble();}).max().getAsDouble();
//添加数据
entity.forEach(c -> {seriesColl.add(c.getName2(), categories, c.getValue4());});
Color[] Colors = new Color[]{new Color(249,106,103),new Color(86,126,217),new Color(254,143,48),new Color(255,201,8),new Color(128,203,75)};
for (int i = 0; i < seriesColl.getCount(); i++) {
System.out.println(seriesColl.get(i));
//单独设置每个系列的颜色
seriesColl.get(i).getFormat().getFill().setColor(Colors[i]);
ChartSeries series = seriesColl.get(i);
series.hasDataLabels(true);
ChartDataLabelCollection dataLabelCollection = series.getDataLabels();
dataLabelCollection.setShowValue(true);
dataLabelCollection.getNumberFormat().setFormatCode("0.00%");
dataLabelCollection.setSeparator("\r\n");
}
ChartAxis axisY = chart.getAxisY();
if (maxY <= 0.5d){
axisY.getScaling().setMaximum(new AxisBound(0.5));
axisY.getScaling().setMinimum(new AxisBound(0));
}else if(maxY <= 0.55d){
axisY.getScaling().setMaximum(new AxisBound(0.55));
axisY.getScaling().setMinimum(new AxisBound(0));
}else if(maxY <= 0.6d){
axisY.getScaling().setMaximum(new AxisBound(0.6));
axisY.getScaling().setMinimum(new AxisBound(0));
}else if(maxY <= 0.65d){
axisY.getScaling().setMaximum(new AxisBound(0.65));
axisY.getScaling().setMinimum(new AxisBound(0));
}else if(maxY <= 0.7d){
axisY.getScaling().setMaximum(new AxisBound(0.7));
axisY.getScaling().setMinimum(new AxisBound(0.1));
}else if(maxY <= 0.75d){
axisY.getScaling().setMaximum(new AxisBound(0.75));
axisY.getScaling().setMinimum(new AxisBound(0));
}else if(maxY <= 0.8d){
axisY.getScaling().setMaximum(new AxisBound(0.8));
axisY.getScaling().setMinimum(new AxisBound(0.1));
}else if(maxY <= 0.9d){
axisY.getScaling().setMaximum(new AxisBound(0.9));
axisY.getScaling().setMinimum(new AxisBound(0.1));
}else if (maxY <= 1.0d){
axisY.getScaling().setMaximum(new AxisBound(1.0));
axisY.getScaling().setMinimum(new AxisBound(0));
}
}
}
请问大佬们如何解决?
@ denis.shvydkiy