Word 中的图表能够直观地展示数据的分布、趋势和比例等,可以帮助读者更快速、准确地理解分析数据提高效率。在 Spire.Doc 中,同样支持如微软 Word 涵盖的各种图表类型。在本文中,您将学习如何使用 Spire.Doc for Java 在 Word 文档中创建柱状图表,饼型图表和气泡图表。
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。JAR 文件可以从此链接下载。如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而轻松地在应用程序中导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>13.3.0</version>
</dependency>
</dependencies>
在 Word 中创建柱状图表
使用 Paragraph.appendChart(ChartType. Column, float width, float height) 方法将添加柱状图表,以下是具体步骤:
- 创建 Document 对象。
- 添加 Section 节和 Paragraph 段落元素。
- 使用 Paragraph.appendChart(ChartType.Column, float width, float height) 方法在段落上添加柱状图表。
- 使用 Chart.getSeries().add() 方法向图表添加数据系列。
- 使用 Chart.getTitle().setText() 方法设置图表标题。
- 使用 Chart.getAxisY().getNumberFormat().setFormatCode() 方法设置 Y 轴数据格式
- 使用 Chart.getLegend().setPosition(LegendPosition value) 方法设置图列位置
- 使用 Document.saveToFile() 方法保存文档到 Word 文件。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import com.spire.doc.fields.shapes.charts.*;
public class createChart
{
public static void main(String[] args)
{
// 创建Document 实例
Document document = new Document();
// 添加Section节
Section section = document.addSection();
// 添加段落并设置段落文本
section.addParagraph().appendText("柱状图表:");
// 添加新段落以添加图表
Paragraph newPara = section.addParagraph();
// 在段落中添加指定宽度和高度(单位为point)的柱状图表
ShapeObject shape = newPara.appendChart(ChartType.Column, 500, 300);
// 获取图表对象
Chart chart = shape.getChart();
// 清除默认数据
chart.getSeries().clear();
// 向图表添加数据系列,包括系列名称、类别名称和系列值
chart.getSeries().add("Java 测试系列",
new String[] { "Word", "PDF", "Excel", "PPT", },
new double[] { 790000, 850000, 610000, 400000 });
chart.getSeries().add("NET 测试系列",
new String[] { "Word", "PDF", "Excel", "PPT" },
new double[] { 1500000, 9000012, 900000, 720000 });
// 设置图表标题
chart.getTitle().setText("汇总");
// 设置 Y 轴数据格式
chart.getAxisY().getNumberFormat().setFormatCode("#,##0");
// 设置图例位置
chart.getLegend().setPosition(LegendPosition.Bottom);
// 保存文档
document.saveToFile("柱状图表.docx", FileFormat.Docx_2016);
// 释放文档对象
document.dispose();
}
}
在 Word 中创建饼型图表
使用 Paragraph.appendChart(ChartType.Pie, float width, float height) 方法将添加饼型图表,以下是具体步骤:
- 创建 Document 对象。
- 添加 Section 节和 Paragraph 段落元素。
- 使用 Paragraph.appendChart(ChartType.Pie, float width, float height) 方法在段落上添加饼型图表。
- 使用 Chart.getSeries().add() 方法向图表添加数据系列。
- 使用 Chart.getTitle().setText() 方法设置图表标题。
- 使用Chart.getLegend().setPosition(LegendPosition value) 方法设置图列位置
- 使用 Document.saveToFile() 方法保存文档到 Word 文件。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import com.spire.doc.fields.shapes.charts.*;
public class createChart
{
public static void main(String[] args)
{
// 创建Document 实例
Document document = new Document();
// 添加Section节
Section section = document.addSection();
// 添加段落并设置段落文本
section.addParagraph().appendText("饼型图表:");
// 添加新段落以添加图表
Paragraph newPara = section.addParagraph();
// 在段落中添加指定宽度和高度(单位为point)的饼型图表
ShapeObject shape = newPara.appendChart(ChartType.Pie, 500, 300);
// 获取图表对象
Chart chart = shape.getChart();
// 清除默认数据
chart.getSeries().clear();
// 向图表添加数据系列,包括系列名称、类别名称和系列值
chart.getSeries().add("NET 系列",
new String[] { "Word", "PDF", "Excel", "PPT" },
new double[] { 150, 200, 140, 120 });
// 设置图表标题
chart.getTitle().setText("测试");
// 设置图例位置
chart.getLegend().setPosition(LegendPosition.Right);
// 保存文档
document.saveToFile("饼型图表.docx", FileFormat.Docx_2016);
// 释放文档对象
document.dispose();
}
}
在 Word 中创建气泡图表
使用 Paragraph.appendChart(ChartType.Bubble, float width, float height) 方法将添加气泡图表,以下是具体步骤:
- 创建 Document 对象。
- 添加 Section 节和 Paragraph 段落元素。
- 使用 Paragraph.appendChart(ChartType.Bubble, float width, float height) 方法在段落上添加气泡图表。
- 使用 Chart.getSeries().add() 方法向图表添加数据系列。
- 使用 Chart.getTitle().setText() 方法设置图表标题。
- 使用 Document.saveToFile() 方法保存文档到 Word 文件。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import com.spire.doc.fields.shapes.charts.*;
public class createChart
{
public static void main(String[] args)
{
// 创建Document 实例
Document document = new Document();
// 添加Section节
Section section = document.addSection();
// 添加段落并设置段落文本
section.addParagraph().appendText("气泡图表:");
// 添加新段落以添加图表
Paragraph newPara = section.addParagraph();
// 在段落中添加指定宽度和高度(单位为point)的气泡图表
ShapeObject shape = newPara.appendChart(ChartType.Bubble, 500, 300);
// 获取图表对象
Chart chart = shape.getChart();
// 清除默认数据
chart.getSeries().clear();
// 向图表添加数据系列,包括系列名称、系列值
ChartSeries series1 = chart.getSeries().add("系列",
new double[] { 2.9, 3.5, 1.1, 4.0, 4.0 },
new double[] { 1.9, 8.5, 2.1, 6.0, 1.5 },
new double[] { 9.0, 4.5, 2.5, 8.0, 5.0 });
// 设置图表标题
chart.getTitle().setText("测试");
// 保存文档
document.saveToFile("气泡图表.docx", FileFormat.Docx_2016);
// 释放文档对象
document.dispose();
}
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。