服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java 在PPT中创建散点图的实现示例

Java 在PPT中创建散点图的实现示例

2022-03-13 15:26E-iceblue Java教程

本文将以Java代码示例展示如何在PPT幻灯片中创建散点图表。文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文将以Java代码示例展示如何在PPT幻灯片中创建散点图表。

创建图表前

需要在Java程序中导入用于操作PPT的jar包 Free Spire.Presentation for Java。可参考如下两种方法导入:

方法1:手动导入jar包。需下载jar包到本地,并解压,找到lib文件夹下的jar文件。然后按照如下步骤执行,导入:

Java 在PPT中创建散点图的实现示例

Java 在PPT中创建散点图的实现示例

Java 在PPT中创建散点图的实现示例

方法2:maven仓库下载导入。需在pom.xml文件中配置maven仓库路径,并指定依赖。配置内容如下:

<repositories>
      <repository>
          <id>com.e-iceblue</id>
          <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
      </repository>
  </repositories>
<dependencies>
  <dependency>
      <groupId> e-iceblue </groupId>
      <artifactId>spire.presentation.free</artifactId>
      <version>3.9.0</version>
  </dependency>
</dependencies>

 

创建图表时

通过指定数据源,并在幻灯片中的指定坐标位置插入图表。该Jar包提供了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();
  }
}

图表效果图:

Java 在PPT中创建散点图的实现示例

 

其他注意事项

本次代码中的PPT结果文档路径为IDEA程序项目文件夹路径,如F:\IDEAProject\Chart_PPT\ScatterChart.pptx ,路径也可以自定义。

到此这篇关于Java 在PPT中创建散点图的实现示例的文章就介绍到这了,更多相关Java PPT创建散点图内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/Yesi/p/15507874.html

延伸 · 阅读

精彩推荐