Java creates a pie chart in Excel

Pie charts are a popular graphing tool in Excel. Pie charts allow you to directly graph the overall proportions of each component, helping you to analyze and understand abstract data more quickly and intuitively. A ring chart is a variant of a pie chart. Visually, the ring chart removes the central part, but its main function is to interpret the ratio relationship between the data. This article will show you how to use Free Spire.XLS for Java to create pie charts and ring charts in an Excel document through your code.

** Environment configuration: ** ** 1. ** Free Spire.XLS for Java Download the package, unzip it, and in the lib folder Import the Spire.Xls.jar package into your Java application as a dependency.

** 2. ** You can also install the JAR package directly from the Maven repository The code that makes up the pom.xml file is:

<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.xls.free</artifactId>
        <version>2.2.0</version>
    </dependency>
</dependencies>

pie chart:

import com.spire.xls.*;
import com.spire.xls.charts.ChartSerie;

import java.awt.*;

public class CreatePieChart {

    public static void main(String[] args) {

        //Create a workbook object
        Workbook workbook = new Workbook();

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Write data to worksheet
        sheet.getCellRange("A1").setValue("Year");
        sheet.getCellRange("A2").setValue("2002");
        sheet.getCellRange("A3").setValue("2003");
        sheet.getCellRange("A4").setValue("2004");
        sheet.getCellRange("A5").setValue("2005");

        sheet.getCellRange("B1").setValue("amount of sales");
        sheet.getCellRange("B2").setNumberValue(4000);
        sheet.getCellRange("B3").setNumberValue(6000);
        sheet.getCellRange("B4").setNumberValue(7000);
        sheet.getCellRange("B5").setNumberValue(8500);

        //Set cell style
        sheet.getCellRange("A1:B1").setRowHeight(15);
        sheet.getCellRange("A1:B1").getCellStyle().setColor(Color.darkGray);
        sheet.getCellRange("A1:B1").getCellStyle().getExcelFont().setColor(Color.white);
        sheet.getCellRange("A1:B1").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);
        sheet.getCellRange("A1:B1").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);
        sheet.getCellRange("B2:C5").getCellStyle().setNumberFormat("\"$\"#,##0");

        //Add pie chart
        Chart chart = sheet.getCharts().add(ExcelChartType.Pie);

        //Set the graph data area
        chart.setDataRange(sheet.getCellRange("B2:B5"));
        chart.setSeriesDataFromRange(false);

        //Set the position of the chart
        chart.setLeftColumn(3);
        chart.setTopRow(1);
        chart.setRightColumn(11);
        chart.setBottomRow(20);

        //Set the title of the graph
        chart.setChartTitle("Annual sales");
        chart.getChartTitleArea().isBold(true);
        chart.getChartTitleArea().setSize(12);

        //Series label settings
        ChartSerie cs = chart.getSeries().get(0);
        cs.setCategoryLabels(sheet.getCellRange("A2:A5"));
        cs.setValues(sheet.getCellRange("B2:B5"));
        cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true);
        chart.getPlotArea().getFill().setVisible(false);

        //Save the document
        workbook.saveToFile("piechart.xlsx", ExcelVersion.Version2016);
    }
}

piechart.jpg

** Donut Graph: **

import com.spire.xls.*;
import com.spire.xls.charts.ChartSerie;
import com.spire.xls.charts.ChartSeries;

import java.awt.*;

public class CreateDoughnutChart {

    public static void main(String[] args) {

        //Create a workbook object
        Workbook workbook = new Workbook();

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Write data to worksheet
        sheet.getCellRange("A1").setValue("Country");
        sheet.getCellRange("A2").setValue("Cuba");
        sheet.getCellRange("A3").setValue("Mexico");
        sheet.getCellRange("A4").setValue("Germany");
        sheet.getCellRange("A5").setValue("Thailand");

        sheet.getCellRange("B1").setValue("amount of sales");
        sheet.getCellRange("B2").setNumberValue(6000);
        sheet.getCellRange("B3").setNumberValue(8000);
        sheet.getCellRange("B4").setNumberValue(9000);
        sheet.getCellRange("B5").setNumberValue(8500);

        //Set cell style
        sheet.getCellRange("A1:B1").setRowHeight(15);
        sheet.getCellRange("A1:B1").getCellStyle().setColor(Color.darkGray);
        sheet.getCellRange("A1:B1").getCellStyle().getExcelFont().setColor(Color.white);
        sheet.getCellRange("A1:B1").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);
        sheet.getCellRange("A1:B1").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);
        sheet.getCellRange("B2:C5").getCellStyle().setNumberFormat("\"$\"#,##0");

        //Add pie chart
        Chart chart = sheet.getCharts().add(ExcelChartType.Doughnut);

        //Set the graph data area
        chart.setDataRange(sheet.getCellRange("A1:B5"));
        chart.setSeriesDataFromRange(false);

        //Set the position of the chart
        chart.setLeftColumn(3);
        chart.setTopRow(1);
        chart.setRightColumn(11);
        chart.setBottomRow(20);

        //Set the title of the chart
        chart.setChartTitle("market share");
        chart.getChartTitleArea().isBold(true);
        chart.getChartTitleArea().setSize(12);

        //Set the color of the series
        ChartSeries series = chart.getSeries();
        for (int i = 0 ; i < series.size() ; i++) {
            ChartSerie cs = series.get(i);
            cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasPercentage(true);
        }

        //Set the position of the legend
        chart.getLegend().setPosition(LegendPositionType.Top);

        //Save the document
        workbook.saveToFile("doughnutchart.xlsx", ExcelVersion.Version2016);
    }
}

doughnutchart.jpg

Recommended Posts

Java creates a pie chart in Excel
Java creates a table in a Word document
Save Java PDF in Excel
Importing Excel data in Java 2
Find a subset in Java
Import Excel data in Java
Java creates an Excel document
Importing Excel data in Java 3
Java creates a Word document
Activate Excel file A1 cell of each sheet in Java
3 Implement a simple interpreter in Java
A simple sample callback in Java
Draw a pie chart with Chart.js
Get stuck in a Java primer
About returning a reference in a Java Getter
What is a class in Java language (3 /?)
When seeking multiple in a Java array
[Creating] A memorandum about coding in Java
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Create a TODO app in Java 7 Create Header
Try making a calculator app in Java
Delete blank rows / columns in Java Excel
Implement something like a stack in Java
Split a string with ". (Dot)" in Java
Creating a matrix class in Java Part 1
I made a primality test program in Java
GetInstance () from a @Singleton class in Groovy from Java
Two ways to start a thread in Java + @
Read a string in a PDF file with Java
Create a CSR with extended information in Java
A story about the JDK in the Java 11 era
Partization in Java
How to display a web page in Java
[Android / Java] Operate a local database in Room
Code to escape a JSON string in Java
Rock-paper-scissors in Java
A note when you want Tuple in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I wrote a primality test program in Java
Let's create a super-simple web framework in Java
I made a rock-paper-scissors game in Java (CLI)
A bat file that uses Java in windows
Add, read, and delete Excel comments in Java
Java tips-Create a Spring Boot project in Gradle
A quick review of Java learned in class
I wrote a prime factorization program in Java
Pi in Java
FizzBuzz in Java
I made a simple calculation problem game in Java
How to create a Java environment in just 3 seconds
Make "I'm not a robot" in Java EE (Jakarta EE)
A quick review of Java learned in class part4
Write a class in Kotlin and call it in Java
I tried to create a Clova skill in Java
[Personal memo] Make a simple deep copy in Java
How to create a data URI (base64) in Java
A note for Initializing Fields in the Java tutorial
I tried to make a login function in Java
Quickly implement a singleton with an enum in Java
What I learned when building a server in Java