简体   繁体   English

在 Apache POI-TL 中使用 SingleSeriesCharts

[英]Using SingleSeriesCharts in Apache POI-TL

I am trying to include a Pie/Bar Chart in an MSWord docx template.我正在尝试在 MSWord docx 模板中包含饼图/条形图。 I have written the code as described in the documentation.我已经按照文档中的描述编写了代码。 However, I do not know how to tag or name the chart on the template to ensure it is read from the code.但是,我不知道如何在模板上标记或命名图表以确保从代码中读取它。 Here is my code:这是我的代码:

Map<String, Object> map = new HashMap<>();
String c[] = {"Male", "Female", "Others"};
Number x[] = {23,13.4,11};
File t = new File(System.getProperty("user.dir")+"/chart.docx");
ChartSingleSeriesRenderData pie = Charts.ofSingleSeries("Test", c).series("data", x).create();
map.put("chart", pie);


XWPFTemplate.compile(t).render(map).writeToFile("out.docx");

We are talking about that POI-TL : http://deepoove.com/poi-tl/ and https://github.com/Sayi/poi-tl .我们正在谈论那个POI-TLhttp://deepoove.com/poi-tl/https://github.com/Sayi/poi-tl

How to use a label for single series charts in the template is documented here 7.3.如何将 label 用于模板中的单系列图表记录在此处7.3。 Single Series Charts : 单系列图表

The label for a single series chart is one text: {{var}}, the label position is: chart area format - alternative text - title (the new version of Microsoft Office label location is: Edit alt text - alt text).单系列图表label为一个文本:{{var}},label position为:图表区域格式-替代文本-标题(新版Microsoft Office label位置为:编辑替代文本-替代文本)。

Note: Documentarion is all in Chinese.注:纪录片均为中文。 But modern browsers should be able to translate into English.但是现代浏览器应该能够翻译成英文。

So insert a pie chart into the template.所以在模板中插入一个饼图。 Then add alt text .然后添加替代文字 For the chart, right-click the chart object and select Edit Alt Text .对于图表,右键单击图表 object 和 select Edit Alt Text In the alt-text-field you can write the label {{chart}}.在替代文本字段中,您可以编写 label {{chart}}。

My template.docx looks like so:我的template.docx看起来像这样:

在此处输入图像描述

Now following code should replace the chart data with the given data in data model.现在,以下代码应将图表数据替换为数据 model 中的给定数据。

import java.util.*;

import com.deepoove.poi.*;
import com.deepoove.poi.data.*;
import com.deepoove.poi.data.style.*;

public class TestPOITL {

 public static void main(String[] args) throws Exception {
  Map<String, Object> dataMap = new HashMap<>();
  dataMap.put("title", "Title inserted by POI-TL");
  String[] c = {"Male", "Female", "Others"};
  Number[] x = {23,13.4,11};
  String[] xS = Arrays.stream(x).mapToDouble(v -> v.doubleValue()).mapToObj(String::valueOf).toArray(String[]::new);
  dataMap.put("table", Tables.of(new String[][] {c, xS}).border(BorderStyle.DEFAULT).create());
  ChartSingleSeriesRenderData pie = Charts.ofSingleSeries("Test", c).series("data", x).create();
  dataMap.put("chart", pie);
  XWPFTemplate.compile("./template.docx").render(dataMap).writeToFile("./out.docx");
 }
}

Resulting out.docx looks like so:结果out.docx如下所示:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM