散点图(XY) 是一个二维图表,显示了两组变量之间的关系。每个散点图有两个轴:一个水平轴(x轴)和一个垂直轴(y轴),它只接受一个数据系列。在本文中,您将学习如何使用 Spire.Presentation for Java 向 PowerPoint 幻灯片添加散点图。
安装 Spire.Presentation for Java
首先,您需要在 Java 程序中添加 Spire.Presentation.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.presentation</artifactId>
<version>10.1.2</version>
</dependency>
</dependencies>
创建散点图
Spire.Presentation for Java 提供了 ShapeCollection.appendChart(ChartType type, Rectangle2D rectangle, boolean init) 方法向幻灯片添加特定类型的图表,ChartType 枚举预定义了多达 73 种图表类型,包括但不限于散点图、柱图、饼图等。
下面是创建散点图的具体方法和步骤:
- 创建 Presentation 类的实例。
- 使用 ShapeCollection.appendChart() 方法将散点图附加到特定的幻灯片。
- 通过 ChartData.get().setValue() 方法设置图表数据。
- 使用 IChart 接口提供的方法设置图表标题、坐标轴标题、系列标签等。
- 设置网格线样式和数据点线样式。
- 使用 Presentation.saveToFile() 方法将文档保存到指定路径。
- Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideSizeType;
import com.spire.presentation.TextLineStyle;
import com.spire.presentation.charts.ChartType;
import com.spire.presentation.charts.IChart;
import com.spire.presentation.charts.entity.ChartDataLabel;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class ScatterChart {
public static void main(String[] args) throws Exception{
//创建Presentation类的实例
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//添加散点图表到第一张幻灯片
IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.SCATTER_MARKERS,new Rectangle2D.Float(180, 80, 550, 320),false);
//设置图表标题
chart.getChartTitle().getTextProperties().setText("散点图表");
chart.getChartTitle().getTextProperties().isCentered(true);
chart.getChartTitle().setHeight(20f);
chart.hasTitle(true);
//设置图表数据源
Double[] xData = new Double[] { 1.0, 2.4, 5.0, 8.9 };
Double[] yData = new Double[] { 5.3, 15.2, 6.7, 8.0 };
chart.getChartData().get(0,0).setText("X-值");
chart.getChartData().get(0,1).setText("Y-值");
for (int i = 0; i < xData.length; i++) {
chart.getChartData().get(i+1,0).setValue(xData[i]);
chart.getChartData().get(i+1,1).setValue(yData[i]);
}
//设置系列标签
chart.getSeries().setSeriesLabel(chart.getChartData().get("B1","B1"));
//设置X和Y轴值
chart.getSeries().get(0).setXValues(chart.getChartData().get("A2","A5"));
chart.getSeries().get(0).setYValues(chart.getChartData().get("B2","B5"));
//添加数据标签
for (int i = 0; i < 4; i++)
{
ChartDataLabel dataLabel = chart.getSeries().get(0).getDataLabels().add();
dataLabel.setLabelValueVisible(true);
}
//设置主轴标题和次轴标题
chart.getPrimaryValueAxis().hasTitle(true);
chart.getPrimaryValueAxis().getTitle().getTextProperties().setText("X-轴 标题");
chart.getSecondaryValueAxis().hasTitle(true);
chart.getSecondaryValueAxis().getTitle().getTextProperties().setText("Y-轴 标题");
//设置网格线
chart.getSecondaryValueAxis().getMajorGridTextLines().setFillType(FillFormatType.SOLID);
chart.getSecondaryValueAxis().getMajorGridTextLines().setStyle(TextLineStyle.THIN_THIN);
chart.getSecondaryValueAxis().getMajorGridTextLines().getSolidFillColor().setColor(Color.GRAY);
chart.getPrimaryValueAxis().getMajorGridTextLines().setFillType(FillFormatType.NONE);
//设置数据点线
chart.getSeries().get(0).getLine().setFillType(FillFormatType.SOLID);
chart.getSeries().get(0).getLine().setWidth(0.1f);
chart.getSeries().get(0).getLine().getSolidFillColor().setColor(Color.BLUE);
//保存文档
presentation.saveToFile("ScatterChart.pptx", FileFormat.PPTX_2013);
presentation.dispose();
}
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。 获取有效期 30 天的临时许可证。