Java在PowerPoint幻灯片中创建散点图的方法

散点图是通过两组数据构成多个坐标点,考察坐标点的分布,判断两变量之间是否存在某种关联或总结坐标点的分布模式。散点图将序列显示为一组点,值由点在图表中的位置表示,类别由图表中的不同标记表示,通常用于比较跨类别的聚合数据。本文将为您介如何通过Java代码在PowerPoint幻灯片中创建散点图。以下是我整理的具体方法及思路,并附上Java代码供大家参考。

代码编译环境:

IntelliJ IDEA 2018(jdk 1.8.0)

Presentation Jar包:Free Spire.Presentation for Java 5.1.0

引入jar包

导入方法1:

手动引入。将Free Spire. Presentation for Java下载到本地,解压,找到lib文件夹下的Spire. Presentation.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序:

-1

导入方法2:如果您想通过 Maven安装,则可以在 pom.XML 文件中添加以下代码导入 JAR 文件。

  1. <repositories>
  2.  
  3.      <repository>
  4.  
  5.          <id>com.e-iceblue</id>
  6.  
  7.          <name>e-iceblue</name>
  8.  
  9.          <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
  10.  
  11.      </repository>
  12.  
  13. </repositories>
  14.  
  15. <dependencies>
  16.  
  17.      <dependency>
  18.  
  19.          <groupId>e-iceblue</groupId>
  20.  
  21.          <artifactId>spire.presentation.free</artifactId>
  22.  
  23.          <version>5.1.0</version>
  24.  
  25.      </dependency>
  26.  
  27. </dependencies>

创建散点图

下面是创建散点图的具体方法和步骤:

  • 创建 Presentation 类的实例。
  • 使用 ShapeCollection.appendChart() 方法将散点图附加到特定的幻灯片。
  • 通过 ChartData.get().setValue() 方法设置图表数据。
  • 使用 IChart 接口提供的方法设置图表标题、坐标轴标题、系列标签等。
  • 设置网格线样式和数据点线样式。
  • 使用 Presentation.saveToFile() 方法将文档保存到指定路径。

完整代码

Java

  1. import com.spire.presentation.FileFormat;
  2. import com.spire.presentation.Presentation;
  3. import com.spire.presentation.SlideSizeType;
  4. import com.spire.presentation.TextLineStyle;
  5. import com.spire.presentation.charts.ChartType;
  6. import com.spire.presentation.charts.IChart;
  7. import com.spire.presentation.charts.entity.ChartDataLabel;
  8. import com.spire.presentation.drawing.FillFormatType;
  9.  
  10. import java.awt.*;
  11. import java.awt.geom.Rectangle2D;
  12.  
  13. public class ScatterChart {
  14.      public static void main(String[] args) throws Exception{
  15.          //创建Presentation类的实例
  16.          Presentation presentation = new Presentation();
  17.          presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
  18.  
  19.      //添加散点图表到第一张幻灯片
  20.          IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.SCATTER_MARKERS,new Rectangle2D.Float(180, 80, 550, 320),false);
  21.  
  22.          //设置图表标题
  23.          chart.getChartTitle().getTextProperties().setText(“散点图表”);
  24.          chart.getChartTitle().getTextProperties().isCentered(true);
  25.          chart.getChartTitle().setHeight(20f);
  26.          chart.hasTitle(true);
  27.  
  28.          //设置图表数据源
  29.          Double[] xData = new Double[] { 1.0, 3.4, 5.0, 7.9 };
  30.          Double[] yData = new Double[] { 4.3, 13.2, 7.7, 6.0 };
  31.          chart.getChartData().get(0,0).setText(“X-值”);
  32.          chart.getChartData().get(0,1).setText(“Y-值”);
  33.          for (int i = 0; i < xData.length; i++) {
  34.              chart.getChartData().get(i+1,0).setValue(xData[i]);
  35.              chart.getChartData().get(i+1,1).setValue(yData[i]);
  36.          }
  37.  
  38.          //设置系列标签
  39.          chart.getSeries().setSeriesLabel(chart.getChartData().get(“B1”,“B1”));
  40.  
  41.          //设置X和Y轴值
  42.          chart.getSeries().get(0).setXValues(chart.getChartData().get(“A2”,“A5”));
  43.          chart.getSeries().get(0).setYValues(chart.getChartData().get(“B2”,“B5”));
  44.  
  45.          //添加数据标签
  46.          for (int i = 0; i < 4; i++)
  47.          {
  48.              ChartDataLabel dataLabel = chart.getSeries().get(0).getDataLabels().add();
  49.              dataLabel.setLabelValueVisible(true);
  50.          }
  51.  
  52.          //设置主轴标题和次轴标题
  53.          chart.getPrimaryValueAxis().hasTitle(true);
  54.          chart.getPrimaryValueAxis().getTitle().getTextProperties().setText(“X-轴 标题”);
  55.          chart.getSecondaryValueAxis().hasTitle(true);
  56.          chart.getSecondaryValueAxis().getTitle().getTextProperties().setText(“Y-轴 标题”);
  57.  
  58.          //设置网格线
  59.          chart.getSecondaryValueAxis().getMajorGridTextLines().setFillType(FillFormatType.SOLID);
  60.          chart.getSecondaryValueAxis().getMajorGridTextLines().setStyle(TextLineStyle.THIN_THIN);
  61.          chart.getSecondaryValueAxis().getMajorGridTextLines().getSolidFillColor().setColor(Color.GRAY);
  62.          chart.getPrimaryValueAxis().getMajorGridTextLines().setFillType(FillFormatType.NONE);
  63.  
  64.          //设置数据点线
  65.          chart.getSeries().get(0).getLine().setFillType(FillFormatType.SOLID);
  66.          chart.getSeries().get(0).getLine().setWidth(0.1f);
  67.          chart.getSeries().get(0).getLine().getSolidFillColor().setColor(Color.GREEN);
  68.  
  69.          //保存文档
  70.          presentation.saveToFile(“ScatterChart.pptx”, FileFormat.PPTX_2013);
  71.          presentation.dispose();
  72.      }
  73. }

效果图

-2

到此这篇关于Java如何在PowerPoint幻灯片中创建散点图的文章就介绍到这了,更多相关Java PowerPoint幻灯片散点图内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

标签

发表评论