折れ線グラフは、散布図の一種であり、プロットされた点を直線でつないだものをいいます。今日はSpire.Presentation for Javaというライブラリを利用して、パワーポイントで折れ線グラフを作る方法を紹介します。
下準備
1.E-iceblueの公式サイトからFree Spire. Presentation for Java無料版をダウンロードしてください。
2.IDEを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire. Presentation.jarを参照に追加してください。
```JAVA import com.spire.presentation.FileFormat; import com.spire.presentation.Presentation; import com.spire.presentation.SlideSizeType; import com.spire.presentation.charts.ChartLegendPositionType; import com.spire.presentation.charts.ChartType; import com.spire.presentation.charts.IChart;import java.awt.geom.Rectangle2D;
public class LineChart {
public static void main(String[] args) throws Exception {
//Presentation オブジェクトを作成します。
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//折れ線グラフを挿入します。
Rectangle2D.Double rect = new Rectangle2D.Double(100, 50, 600, 430);
IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.LINE, rect);
//グラフのタイトルを設定します。
chart.getChartTitle().getTextProperties().setText("月間売上推移");
chart.getChartTitle().getTextProperties().isCentered(true);
chart.getChartTitle().setHeight(30);
chart.hasTitle(true);
//軸のタイトルを設定します。
chart.getPrimaryCategoryAxis().getTitle().getTextProperties().setText("2020年度");
chart.getPrimaryCategoryAxis().hasTitle(true);
chart.getPrimaryValueAxis().getTitle().getTextProperties().setText("売上");
chart.getPrimaryValueAxis().hasTitle(true);
//グラフのデータを設定します。
chart.getChartData().get(0,0).setText("月間");
chart.getChartData().get(1,0).setText("一月");
chart.getChartData().get(2,0).setText("二月");
chart.getChartData().get(3,0).setText("三月");
chart.getChartData().get(4,0).setText("四月");
chart.getChartData().get(5,0).setText("五月");
chart.getChartData().get(6,0).setText("六月");
chart.getChartData().get(0,1).setText("Ipad");
chart.getChartData().get(1,1).setNumberValue(80);
chart.getChartData().get(2,1).setNumberValue(45);
chart.getChartData().get(3,1).setNumberValue(25);
chart.getChartData().get(4,1).setNumberValue(20);
chart.getChartData().get(5,1).setNumberValue(10);
chart.getChartData().get(6,1).setNumberValue(5);
chart.getChartData().get(0,2).setText("Sony");
chart.getChartData().get(1,2).setNumberValue(30);
chart.getChartData().get(2,2).setNumberValue(25);
chart.getChartData().get(3,2).setNumberValue(35);
chart.getChartData().get(4,2).setNumberValue(50);
chart.getChartData().get(5,2).setNumberValue(45);
chart.getChartData().get(6,2).setNumberValue(55);
chart.getChartData().get(0,3).setText("任天堂");
chart.getChartData().get(1,3).setNumberValue(10);
chart.getChartData().get(2,3).setNumberValue(15);
chart.getChartData().get(3,3).setNumberValue(20);
chart.getChartData().get(4,3).setNumberValue(35);
chart.getChartData().get(5,3).setNumberValue(60);
chart.getChartData().get(6,3).setNumberValue(95);
//SERIESのラベルを設定します。
chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "D1"));
//タグリストを設定します。
chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));
// SERIESのグラフデータを設定します。
chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B7"));
chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C7"));
chart.getSeries().get(2).setValues(chart.getChartData().get("D2", "D7"));
//データを表示にします。
chart.getSeries().get(0).getDataLabels().setLabelValueVisible(true);
chart.getSeries().get(1).getDataLabels().setLabelValueVisible(true);
chart.getSeries().get(2).getDataLabels().setLabelValueVisible(true);
//グラフの位置を設定します。
chart.getChartLegend().setPosition(ChartLegendPositionType.TOP);
//保存します。
presentation.saveToFile("LineChart.pptx", FileFormat.PPTX_2013);
}
}
<h4> <strong>実行結果</strong></h4>
<p><strong> </strong></p>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210310/20210310145117.png" alt="f:id:lendoris:20210310145117p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>