DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on • Updated on

How to Insert Charts in a PowerPoint Presentation in Java

Charts can display data in the form of visual graphs, so that we can easily find the essential information behind the data, such as the proportion of men and women in a department, market share, data trends over time, etc. In this article, I am going to show you how to create a clustered column chart, a pie chart and a line chart in a PowerPoint presentation by using Free Spire.Presentation for Java.

Add Spire.Presentation.jar as dependency

Method 1: Download Free Spire.Presentation for Java pack, unzip it and you’ll get Spire.Presentation.jar file from the “lib” folder. Import the jar file in your project as a dependency.

Method 2: If you are creating a Maven project, you can easily add the jar dependency by adding the following configurations to the pom.xml.

<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>
Enter fullscreen mode Exit fullscreen mode

Example 1. Column chart

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

Example 2. Pie chart

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

Example 3. Line chart

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

Top comments (0)