Java에서 PowerPoint 프레젠테이션에 차트를 삽입하는 방법

53313 단어 powerpointchartjava
차트는 시각적 그래프 형태로 데이터를 표시할 수 있으므로 부서의 남녀 비율, 시장 점유율, 시간 경과에 따른 데이터 추세 등과 같은 데이터 이면의 필수 정보를 쉽게 찾을 수 있습니다. Free Spire.Presentation for Java를 사용하여 PowerPoint 프레젠테이션에서 묶은 세로 막대형 차트, 원형 차트 및 선 차트를 만드는 방법을 보여 드리겠습니다.

Spire.Presentation.jar을 종속성으로 추가



방법 1: Free Spire.Presentation for Java 팩을 다운로드하고 압축을 풀면 "lib"폴더에서 Spire.Presentation.jar 파일을 얻을 수 있습니다. 프로젝트의 jar 파일을 종속성으로 가져옵니다.

방법 2: Maven 프로젝트를 생성하는 경우 다음 구성을 pom.xml에 추가하여 jar 종속성을 쉽게 추가할 수 있습니다.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.presentation.free</artifactId>
        <version>2.6.1</version>
    </dependency>
</dependencies>


예 1. 세로 막대형 차트




import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideSizeType;
import com.spire.presentation.charts.ChartStyle;
import com.spire.presentation.charts.ChartType;
import com.spire.presentation.charts.IChart;
import java.awt.geom.Rectangle2D;

public class ColumnChart {

    public static void main(String[] args) throws Exception {

        //Create a Presentation object
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //Insert a clustered column chart
        Rectangle2D.Double rect = new   Rectangle2D.Double(100, 50, 650, 400);
        IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.COLUMN_CLUSTERED, rect);

        //Set chart title
        chart.getChartTitle().getTextProperties().setText("Male/Female Ratio Per Dept.");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);
        chart.hasTitle(true);

        //Set axis title
        chart.getPrimaryCategoryAxis().getTitle().getTextProperties().setText("Department");
        chart.getPrimaryCategoryAxis().hasTitle(true);
        chart.getPrimaryValueAxis().getTitle().getTextProperties().setText("Number of people");
        chart.getPrimaryValueAxis().hasTitle(true);

        //Write data to chart as chart data
        chart.getChartData().get(0,0).setText("Department");
        chart.getChartData().get(1,0).setText("Development");
        chart.getChartData().get(2,0).setText("Testing");
        chart.getChartData().get(3,0).setText("Sales");
        chart.getChartData().get(4,0).setText("Support");
        chart.getChartData().get(0,1).setText("Male");
        chart.getChartData().get(1,1).setNumberValue(65);
        chart.getChartData().get(2,1).setNumberValue(21);
        chart.getChartData().get(3,1).setNumberValue(12);
        chart.getChartData().get(4,1).setNumberValue(30);
        chart.getChartData().get(0,2).setText("Female");
        chart.getChartData().get(1,2).setNumberValue(13);
        chart.getChartData().get(2,2).setNumberValue(33);
        chart.getChartData().get(3,2).setNumberValue(28);
        chart.getChartData().get(4,2).setNumberValue(21);

        //Set series labels
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "C1"));

        //Set categories labels
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A5"));

        //Assign data to series values
        chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B5"));
        chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C5"));

        //Display values in data labels
        chart.getSeries().get(0).getDataLabels().setLabelValueVisible(true);
        chart.getSeries().get(1).getDataLabels().setLabelValueVisible(true);

        //Set overlap
        chart.setOverLap(-50);

        //Set gap width
        chart.setGapDepth(350);

        //Apply built-in chart style
        chart.setChartStyle(ChartStyle.STYLE_11);

        //Save to file
        presentation.saveToFile("ColumnChart.pptx", FileFormat.PPTX_2013);
    }
}




예 2. 원형 차트




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.ChartStyle;
import com.spire.presentation.charts.ChartType;
import com.spire.presentation.charts.IChart;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class PieChart {

    public static void main(String[] args) throws Exception {

        //Create a Presentation object
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //Insert a pie chart
        Rectangle2D.Double rect = new   Rectangle2D.Double(100, 50, 600, 350);
        IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.PIE, rect);

        //Set title
        chart.getChartTitle().getTextProperties().setText("Market Share by Country");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);
        chart.hasTitle(true);

        //Write data to chart as chart data
        chart.getChartData().get(0,0).setText("Country");
        chart.getChartData().get(1,0).setText("United States");
        chart.getChartData().get(2,0).setText("Canada");
        chart.getChartData().get(3,0).setText("Mexico");
        chart.getChartData().get(4,0).setText("Brazil");
        chart.getChartData().get(0,1).setText("Sales");
        chart.getChartData().get(1,1).setNumberValue(700000);
        chart.getChartData().get(2,1).setNumberValue(620000);
        chart.getChartData().get(3,1).setNumberValue(570000);
        chart.getChartData().get(4,1).setNumberValue(480000);

        //Set series labels
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1","B1"));

        //Set categories labels
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A5"));

        //Assign data to series values
        chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B5"));

        //Display values in data labels
        chart.getSeries().get(0).getDataLabels().setLabelValueVisible(true);
        chart.getSeries().get(0).getDataLabels().setPercentValueVisible(true);

        //Apply built-in chart style
        chart.setChartStyle(ChartStyle.STYLE_3);

        //Set chart legend position
        chart.getChartLegend().setPosition(ChartLegendPositionType.LEFT);

        //Fill chart with solid color
        chart.getFill().setFillType(FillFormatType.SOLID);
        chart.getFill().getSolidColor().setColor(Color.lightGray);

        //Save to file
        presentation.saveToFile("PieChart.pptx", FileFormat.PPTX_2013);
    }
}




예 3. 라인 차트




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 {

        //Create a Presentation object
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //Insert a line chart
        Rectangle2D.Double rect = new   Rectangle2D.Double(100, 50, 600, 430);
        IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.LINE, rect);

        //Set chart title
        chart.getChartTitle().getTextProperties().setText("Product Trends by Month");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);
        chart.hasTitle(true);

        //Set axis title
        chart.getPrimaryCategoryAxis().getTitle().getTextProperties().setText("Month");
        chart.getPrimaryCategoryAxis().hasTitle(true);
        chart.getPrimaryValueAxis().getTitle().getTextProperties().setText("Sales Volume");
        chart.getPrimaryValueAxis().hasTitle(true);

        //Write data to chart as chart data
        chart.getChartData().get(0,0).setText("Month");
        chart.getChartData().get(1,0).setText("Jan");
        chart.getChartData().get(2,0).setText("Feb");
        chart.getChartData().get(3,0).setText("Mar");
        chart.getChartData().get(4,0).setText("Apr");
        chart.getChartData().get(5,0).setText("May");
        chart.getChartData().get(6,0).setText("Jun");

        chart.getChartData().get(0,1).setText("Desktops");
        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("Laptops");
        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("Tablets");
        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);

        //Set series labels
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "D1"));

        //Set categories labels
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));

        //Assign data to series values
        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"));

        //Display values in data labels
        chart.getSeries().get(0).getDataLabels().setLabelValueVisible(true);
        chart.getSeries().get(1).getDataLabels().setLabelValueVisible(true);
        chart.getSeries().get(2).getDataLabels().setLabelValueVisible(true);

        //Set chart legend position
        chart.getChartLegend().setPosition(ChartLegendPositionType.TOP);

        //Save to file
        presentation.saveToFile("LineChart.pptx", FileFormat.PPTX_2013);
    }
}


좋은 웹페이지 즐겨찾기