在 Java 中開發散點圖創建器

我們的目標

這篇博客文章將使您能夠以編程方式自動創建圖表。我們將通過編寫代碼片段來開發一個 Java 散點圖創建器,儘量簡化過程。Aspose.Slides for Java 是一個功能強大且輕量級的 Java API,用於創建和操作圖表。此外,這個高級 API 提供了比其他競爭 API 更友好的開發者生態系統。因此,讓我們繼續並直接切入主題。

Java 圖表 API - 安裝

您可以考慮在這裡查看安裝詳情。簡而言之,您可以下載這個 JAR 文件或使用以下 Maven 配置來安裝這個 Java 圖表庫:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository> 
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-slides</artifactId>
    <version>24.4</version>
    <classifier>jdk16</classifier>
</dependency>

開發散點圖創建器 - 代碼片段

現在,我們將編寫需要遵循的步驟:

  • 創建 Presentation 類的實例。
  • 調用 get_Item 方法訪問第一個幻燈片。
  • 通過調用 addChart 方法創建默認圖表。
  • 調用 getChartDataWorkbook 方法獲取圖表數據工作表。
  • 通過調用 add 方法添加新的圖表類別。
  • 通過調用 addDataPointForScatterSeries 方法添加新的數據點 (1:3)。
  • 通過調用 setType 方法設置系列類型。
  • 通過調用 setSize 方法設置圖表中的標記大小。
  • 調用 setSymbol 方法設置圖表中的標記樣式。
  • 通過調用 save 方法將輸出 PPTX/PPT 文件保存在磁碟上。

以下是顯示如何以編程方式在 Java 中開發散點圖創建器的代碼示例:

package com.example;
import com.aspose.slides.*;
import java.io.File;
public class main
{
public static void main(String[] args)
{
// The path to the documents directory.
String dataDir = "/Desktop/";
// Create directory if it is not already present.
boolean IsExists = new File(dataDir).exists();
if (!IsExists)
new File(dataDir).mkdirs();
// Create an instance of the Presentation class.
Presentation pres = new Presentation();
// Invoke the get_Item method to access the first slide.
ISlide slide = pres.getSlides().get_Item(0);
// Creating the default chart by calling the addChart method.
IChart chart = slide.getShapes().addChart(ChartType.ScatterWithSmoothLines, 0, 0, 400, 400);
// Getting the default chart data worksheet index.
int defaultWorksheetIndex = 0;
// Call the getChartDataWorkbook method to get the chart data worksheet.
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();
// Delete demo series
chart.getChartData().getSeries().clear();
// Add new chart category by calling the add method.
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 1, 1, "Series 1"), chart.getType());
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 1, 3, "Series 2"), chart.getType());
// Take first chart series.
IChartSeries series = chart.getChartData().getSeries().get_Item(0);
// Add new point (1:3) by calling the addDataPointForScatterSeries method.
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 1), fact.getCell(defaultWorksheetIndex, 2, 2, 3));
// Add new point (2:10)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 2), fact.getCell(defaultWorksheetIndex, 3, 2, 10));
// Set the type of series by calling the setType method.
series.setType(ChartType.ScatterWithStraightLinesAndMarkers);
// Set the marker size in the chart by calling the setSize method.
series.getMarker().setSize(10);
// Invoke the setSymbol method to set the marker style in the chart.
series.getMarker().setSymbol(MarkerStyleType.Star);
// Take second chart series
series = chart.getChartData().getSeries().get_Item(1);
// Add new point (5:2) there.
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 3, 5), fact.getCell(defaultWorksheetIndex, 2, 4, 2));
// Add new point (3:1)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 3, 3), fact.getCell(defaultWorksheetIndex, 3, 4, 1));
// Add new point (2:2)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 4, 3, 2), fact.getCell(defaultWorksheetIndex, 4, 4, 2));
// Add new point (5:1)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 5, 3, 5), fact.getCell(defaultWorksheetIndex, 5, 4, 1));
// Changing the chart series marker
series.getMarker().setSize(10);
series.getMarker().setSymbol(MarkerStyleType.Circle);
// Save the output PPTX/PPT file on the disk by calling the save method.
pres.save(dataDir + "AsposeChart_out.pptx", SaveFormat.Pptx);
}
}
java-api-for-charts

獲得免費許可

您可以獲得一個免費臨時許可來試用這個 Java 圖表庫而不受評估限制。

總結

雖然 Aspose.Slides for Java 提供了廣泛的功能來以編程方式創建和操作圖表,但還有全面的文檔可用。此外,您可以跳轉到 API 參考GitHub 倉庫開始在 Java 中開發散點圖創建器。此外,您可以訪問這個鏈接,快速了解提供的所有功能。

隨時聯繫我們

您可以在我們的 論壇 上告訴我們您的問題或查詢。

參考