LoginSignup
0
0

More than 3 years have passed since last update.

Java PowerPointでグラフにデータラベルを追加

Posted at

データラベルによって、データ系列またはその個々のデータポイントに関する詳細情報が表示されるので、グラフが理解しやすくなります。今回はSpire.Presentation for Javaを使ってPowerPointでグラフにデータラベルを追加する方法を説明します。

下準備

1.E-iceblueの公式サイトからFree Spire. Presentation for Java無料版をダウンロードしてください。

f:id:lendoris:20210317120248p:plain
2. IDEを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire. Presentation.jarを参照に追加してください。

f:id:lendoris:20210317120303p:plain

元のファイル

f:id:lendoris:20210317121128p:plain

 

import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.charts.IChart;
import com.spire.presentation.charts.entity.ChartDataLabel;
import com.spire.presentation.charts.entity.ChartSeriesDataFormat;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.*;

public class AddDataLabelsToChart {
    public static void main(String[] args) throws Exception {
        //PowerPointをロードします。
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Chart.pptx");

        //スライドを取得します。
        ISlide slide = ppt.getSlides().get(0);
        //チャートを取得します。
        IChart chart = (IChart)slide.getShapes().get(0);

        //グラフの系列を取得します。
        for (ChartSeriesDataFormat series:(Iterable)chart.getSeries()
             ) {
            //各系列にデータラベルをつけます。
            for(int i = 0; i < 4; i++){
                ChartDataLabel dataLabel = series.getDataLabels().add();
                //ラベルの値を表示します。
                dataLabel.setLabelValueVisible(true);
                //ラベルの系列を表示します。
                dataLabel.setSeriesNameVisible(true);
                //ラベルの枠を設定します。
                dataLabel.getLine().setFillType(FillFormatType.SOLID);
                dataLabel.getLine().getSolidFillColor().setColor(Color.RED);
                //ラベルの塗りつぶしを設定します。
                dataLabel.getFill().setFillType(FillFormatType.SOLID);
                dataLabel.getFill().getSolidColor().setColor(Color.YELLOW);
            }
        }

        //保存します。
        ppt.saveToFile("DataLabels.pptx", FileFormat.PPTX_2013);
    }
}

実行結果

f:id:lendoris:20210317121158p:plain

 

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0