Javaで散布図作成者を開発する

目指すもの

このブログ投稿では、プログラム的にチャートを自動作成する方法を説明します。散布図作成者をJavaで開発するためのコードスニペットを書いて、できるだけ簡単にしたいと思います。Aspose.Slides for Javaは、チャートを作成および操作するための強力でありながら軽量なJava APIです。さらに、この高レベルのAPIは、他の競合APIに比べて開発者に優しいエコシステムを提供します。それでは、先に進み、要点に入っていきましょう。

Java API for Charts - インストール

インストール詳細については、こちらを訪問することを検討してください。要するに、この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で散布図作成者を開発し始めることができます。さらに、提供されるすべての機能についての簡単な概要を学ぶために、このリンクを訪れることができます。

お気軽にお問い合わせください

ご質問やお問い合わせがある場合は、フォーラムでお知らせください。

関連情報