はじめに
散点図とは、あるデータを元にして縦軸と横軸の2つの項目で量を計測し、分布を表現するために使うグラフのことです。散点図には2つの軸があります。1つの水平軸(x軸)と1つの垂直軸(y軸)は、1つのデータ系列だけを受け入れます。今回はSpire.Presentation for Javaを利用してスライドに散布図を作成する方法を紹介します。
下準備
1.E-iceblueの公式サイトからSpire. Presentation for Javaをダウンロードしてください。
2. IDEを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire. Presentation.jarを参照に追加してください。
Mavenの側なら
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.com/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.Presentation</artifactId>
<version>6.6</version>
</dependency>
</dependencies>
スライドに散点図を作成
Spire.Presentation for Javaは、ShapeCollection.apendChart(ChartType、Rectangle 2 D rectangle、bolean init)方法でスライドに特定のタイプのグラフを追加し、ChartTypeは、散点図、柱図、円グラフなどを含むが、73種類以上の図表タイプをプリ定義しています。
以下は、点々図を作成する具体的な方法とステップになります。
- Presentationクラスの例を作成します。
- Shape Collection.appndChartを使用して、散点図を特定のスライドに添付します。
- チャートデータはChartData.get().setValue()で設定します。
- IChartインターフェースで提供する方法で、グラフタイトル、軸タイトル、シリーズラベルなどを設定します。
- グリッド線スタイルとデータ点線スタイルを設定します。
- Presentation.saveToFile()メソッドを使用してドキュメントを指定のパスに保存します。
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 CreateScatterChart {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//Add a scatter chart to the first slide
IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.SCATTER_SMOOTH_LINES_AND_MARKERS,new Rectangle2D.Float(40, 80, 550, 320),false);
//Set the chart title
chart.getChartTitle().getTextProperties().setText("スライドに散点図");
chart.getChartTitle().getTextProperties().isCentered(true);
chart.getChartTitle().setHeight(20f);
chart.hasTitle(true);
//Set the chart data
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-Values");
chart.getChartData().get(0,1).setText("Y-Values");
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]);
}
//Set the series label
chart.getSeries().setSeriesLabel(chart.getChartData().get("B1","B1"));
//Set the X and Y values
chart.getSeries().get(0).setXValues(chart.getChartData().get("A2","A5"));
chart.getSeries().get(0).setYValues(chart.getChartData().get("B2","B5"));
//Add data labels
for (int i = 0; i < 4; i++)
{
ChartDataLabel dataLabel = chart.getSeries().get(0).getDataLabels().add();
dataLabel.setLabelValueVisible(true);
}
//Set the primary axis title and the secondary axis title
chart.getPrimaryValueAxis().hasTitle(true);
chart.getPrimaryValueAxis().getTitle().getTextProperties().setText("X軸タイトル");
chart.getSecondaryValueAxis().hasTitle(true);
chart.getSecondaryValueAxis().getTitle().getTextProperties().setText("Y軸タイトル");
//Set the grid line
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);
//Set the data point line
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);
//Save the document to file
presentation.saveToFile("output/ScatterChart.pptx", FileFormat.PPTX_2016);
}
}
最後に
ここまで読んでくださってありがとうございます!もしSpire.Presentation for Javaを利用している時にご不明なところがございましたら、ぜひご連絡ください