Create Column Charts, Pie Charts or Line Charts in PowerPoint in Java

Overview

Charts can display data as visual graphs, so that we can easily find the key information behind the data, like the proportion of sales based on region, the trend of data over time, etc. Including a chart in your presentation is always a good way to display your numerical or statistical data.

This article will introduce how to create a clustered column chart, a pie chart and a line chart in a PowerPoint document in Java by using Spire.Presentation for Java.

  • Create a Column Chart in PowerPoint in Java
  • Create a Pie Chart in PowerPoint in Java
  • Create a Line Chart in PowerPoint in Java

Add Spire.Presentation.jar as a Dependency

Spire.Presentation for Java is a class library for processing PowerPoint documents in Java applications. You can download it from E-iceblue website, and add the jar file in your program as a dependency. If you use Maven, you can easily import the jar file in your application by adding the following code to your project's pom.xml file.

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

Background Knowledge 

Basically, Spire.Presentation offers a Presentation class to represent a PowerPoint document model, which consists of at least one slide (represented by ISlide interface). A chart can be added to a slide, functioning as a shape. The IChart interface provides a set of methods to work with the type, data and appearance of a chart. To help you get started quickly, the following table lists the core classes, interfaces and methods relating to the creation of charts.

Member Description
Presentation class Represents a PowerPoint document model. 
ISlide interface Represents a slide in a PowerPoint document.
ShapeList class Represents a collection of shapes in a PowerPoint document.
ShapeList.appendChart() method Adds a chart to the shape collection.
IChart interface Represents a chart in a PowerPoint document.
ChartType enumeration Defines a variety of chart types preset in MS PowerPoint.
IChart.getChartTitle() method Returns a ChartTextArea object, which is used to set or format chart title.
IChart.getChartData() method Returns a ChartData object, which is used to set data from a chart.
IChart.getSeries() method Returns a ChartSeriesFormatCollection object, which is used to set series value and labels.
IChart.getCategories() method Returns a ChartCategoryCollection object, which is used to set category value and labels.
IChart.setChartStyle() method Sets style of a chart.
IChart.getChartLegend() method Returns a ChartLegend object, which is used to format chart legend.


Create a Column Chart in PowerPoint in Java

Column charts are a classic way to show values, with vertical lines showing values based upon their height. The following code snippet shows you how to create a clustered column chart in a PowerPoint slide using this library.

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;
import java.math.BigDecimal;

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

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

        //Insert a clustered column chart
        Rectangle2D.Float rect = new Rectangle2D.Float(40, 100, 600, 300);
        IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.COLUMN_CLUSTERED, rect);

        //Set chart title
        chart.getChartTitle().getTextProperties().setText("Quarterly Sales 2021");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30f);
        chart.hasTitle(true);

        //Create a String[][] array to store data
        String[][] data = new String[][]{
                {"Quarterly Sales 2021", "Product A", "Product B"},
                {"Q1", "25000", "15000"},
                {"Q2", "13000", "21000"},
                {"Q3", "16000", "25000"},
                {"Q4", "21000", "18000"}
        };

        //Loop through the elements in the array
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[0].length; j++) {
                int number;
                //If a string can be converted to a integer
                if (isNum(data[i][j])) {
                    //Convert string to integer
                    number = Integer.parseInt(data[i][j]);
                    //Write the int value into the chart
                    chart.getChartData().get(i, j).setValue(number);
                } else {
                    //Write the string value into the chart
                    chart.getChartData().get(i, j).setValue(data[i][j]);
                }
            }
        }

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

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

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

        //Set chart style
        chart.setChartStyle(ChartStyle.STYLE_10);

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

        //Set gap width
        chart.setGapWidth(200);

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

    //Determine if a string can be converted to a integer
    public static boolean isNum(String str) {

        try {
            new BigDecimal(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

Create a Column Chart in PowerPoint in Java

Create a Pie Chart in PowerPoint in Java

Pie charts help in easy comparison of proportions. Each value is shown as a portion or slice of the pie, so it is easy to see which values make up the percentage of a whole. The code snippet below gives you an example of how to create a pie chart in PowerPoint using Spire.Presentation for 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.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 CreatePieChart {

    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_3_D, rect);

        //Set title
        chart.getChartTitle().getTextProperties().setText("SALES BY REGION");
        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("Regions");
        chart.getChartData().get(1,0).setText("West");
        chart.getChartData().get(2,0).setText("East");
        chart.getChartData().get(3,0).setText("North");
        chart.getChartData().get(4,0).setText("South");
        chart.getChartData().get(0,1).setText("Sales");
        chart.getChartData().get(1,1).setNumberValue(124000);
        chart.getChartData().get(2,1).setNumberValue(92000);
        chart.getChartData().get(3,1).setNumberValue(183000);
        chart.getChartData().get(4,1).setNumberValue(65000);

        //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_10);

        //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("output/PieChart.pptx", FileFormat.PPTX_2016);
    }
}

Create a Pie Chart in PowerPoint in Java

Create a Line Chart in PowerPoint in Java

Line charts are ideal for showing trends, continuous data over time on an evenly scaled axis. The data points joined with lines makes it easy to see whether values are decreasing or increasing over time. The following code snippet demonstrates how to create a line chart using Spire.Presentation for 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 CreateLineChart {
    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("Vehicle Sales by Year");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);
        chart.hasTitle(true);

        //Set axis title
        chart.getPrimaryCategoryAxis().getTitle().getTextProperties().setText("Years");
        chart.getPrimaryCategoryAxis().hasTitle(true);
        chart.getPrimaryValueAxis().getTitle().getTextProperties().setText("No. of Vehicles in Thousands");
        chart.getPrimaryValueAxis().hasTitle(true);

        //Write data to chart as chart data
        chart.getChartData().get(0,0).setText("Year");
        chart.getChartData().get(1,0).setText("2016");
        chart.getChartData().get(2,0).setText("2017");
        chart.getChartData().get(3,0).setText("2018");
        chart.getChartData().get(4,0).setText("2019");
        chart.getChartData().get(5,0).setText("2020");
        chart.getChartData().get(6,0).setText("2021");

        chart.getChartData().get(0,1).setText("Sedan");
        chart.getChartData().get(1,1).setNumberValue(120);
        chart.getChartData().get(2,1).setNumberValue(105);
        chart.getChartData().get(3,1).setNumberValue(136);
        chart.getChartData().get(4,1).setNumberValue(124);
        chart.getChartData().get(5,1).setNumberValue(154);
        chart.getChartData().get(6,1).setNumberValue(116);

        chart.getChartData().get(0,2).setText("SUV");
        chart.getChartData().get(1,2).setNumberValue(180);
        chart.getChartData().get(2,2).setNumberValue(162);
        chart.getChartData().get(3,2).setNumberValue(140);
        chart.getChartData().get(4,2).setNumberValue(176);
        chart.getChartData().get(5,2).setNumberValue(143);
        chart.getChartData().get(6,2).setNumberValue(190);

        chart.getChartData().get(0,3).setText("Sports");
        chart.getChartData().get(1,3).setNumberValue(100);
        chart.getChartData().get(2,3).setNumberValue(86);
        chart.getChartData().get(3,3).setNumberValue(94);
        chart.getChartData().get(4,3).setNumberValue(116);
        chart.getChartData().get(5,3).setNumberValue(130);
        chart.getChartData().get(6,3).setNumberValue(124);

        //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("output/LineChart.pptx", FileFormat.PPTX_2016);
    }
}

Create a Line Chart in PowerPoint in Java


Similar Articles