DEV Community

loading...

Create Charts in Excel in Java

eiceblue profile image E-iceblue Product Family ・4 min read

In this article, we will introduce how to create popular types of charts in Java using Free Spire.XLS for Java library.

Free Spire.XLS for Java library supports more than 70 types of charts including clustered column chart, pie chart, doughnut chart and many more. The following examples demonstrates how to create clustered column chart, pie chart and doughnut chart using Free Spire.XLS for Java.

Prerequisites

Before getting started, please download Free Spire.XLS for Java package through this link, unzip the package and then import Spire.Xls.jar from lib folder into our application.

Using the code

Clustered Column Chart

import com.spire.xls.*;

public class ColumnChart {
    public static void main(String[] args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load the Excel template
        workbook.loadFromFile("Test.xlsx");

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

        //Set worksheet name
        sheet.setName("Clustered Column Chart");
        //Set gridlines invisible
        sheet.setGridLinesVisible(false);

        //Create clustered column chart
        Chart chart = sheet.getCharts().add(ExcelChartType.ColumnClustered);

        //Set data range for the chart
        chart.setDataRange(sheet.getRange().get("A1:B5"));
        chart.setSeriesDataFromRange(false);

        //Set chart position
        chart.setLeftColumn(5);
        chart.setTopRow(2);
        chart.setRightColumn(15);
        chart.setBottomRow(20);

        //Set chart title
        chart.setChartTitle("Sales Report");
        chart.getChartTitleArea().isBold(true);
        chart.getChartTitleArea().setSize(12);

        //Set category axis title
        chart.getPrimaryCategoryAxis().setTitle("Month");
        chart.getPrimaryCategoryAxis().getFont().isBold(true);
        chart.getPrimaryCategoryAxis().getTitleArea().isBold(true);

        //Set value axis title
        chart.getPrimaryValueAxis().setTitle("Amount");
        chart.getPrimaryValueAxis().hasMajorGridLines(false);
        chart.getPrimaryValueAxis().getTitleArea().setTextRotationAngle(90);
        chart.getPrimaryValueAxis().setMinValue(0);
        chart.getPrimaryValueAxis().getTitleArea().isBold(true);

        //Set legend position
        chart.getLegend().setPosition(LegendPositionType.Right);

        //Save the resultant file
        workbook.saveToFile("ColumnChart.xlsx", ExcelVersion.Version2013);
    }
}

Clustered Column Chart

Pie Chart

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

import java.awt.*;

public class PieChart {
    public static void main(String[] args){
        //Create a Workbook
        Workbook workbook = new Workbook();

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

        //Set worksheet name
        sheet.setName("Pie Chart");
        //Set gridlines invisible
        sheet.setGridLinesVisible(false);

        //Add data to the worksheet
        sheet.getRange().get("A1").setValue("Year");;
        sheet.getRange().get("A2").setValue("2002");
        sheet.getRange().get("A3").setValue("2003");
        sheet.getRange().get("A4").setValue("2004");
        sheet.getRange().get("A5").setValue("2005");
        sheet.getRange().get("B1").setValue("Amount");
        sheet.getRange().get("B2").setNumberValue(4000);
        sheet.getRange().get("B3").setNumberValue(6000);
        sheet.getRange().get("B4").setNumberValue(7000);
        sheet.getRange().get("B5").setNumberValue(8500);
        sheet.getRange().get("A1:B1").getStyle().getFont().isBold (true);
        sheet.getRange().get("A2:B2").getStyle().setKnownColor(ExcelColors.LightYellow);
        sheet.getRange().get("A3:B3").getStyle().setKnownColor(ExcelColors.LightGreen1);
        sheet.getRange().get("A4:B4").getStyle().setKnownColor(ExcelColors.LightOrange);
        sheet.getRange().get("A5:B5").getStyle().setKnownColor(ExcelColors.LightTurquoise);
        sheet.getRange().get("A1:B5").getStyle().getBorders().getByBordersLineType(BordersLineType.EdgeTop).setColor(new Color(0, 0, 128));;
        sheet.getRange().get("A1:B5").getStyle().getBorders().getByBordersLineType(BordersLineType.EdgeTop).setLineStyle(LineStyleType.Thin);
        sheet.getRange().get("A1:B5").getStyle().getBorders().getByBordersLineType(BordersLineType.EdgeBottom).setColor(new Color(0, 0, 128));
        sheet.getRange().get("A1:B5").getStyle().getBorders().getByBordersLineType(BordersLineType.EdgeBottom).setLineStyle(LineStyleType.Thin);
        sheet.getRange().get("A1:B5").getStyle().getBorders().getByBordersLineType(BordersLineType.EdgeLeft).setColor(new Color(0, 0, 128));
        sheet.getRange().get("A1:B5").getStyle().getBorders().getByBordersLineType(BordersLineType.EdgeLeft).setLineStyle(LineStyleType.Thin);
        sheet.getRange().get("A1:B5").getStyle().getBorders().getByBordersLineType(BordersLineType.EdgeRight).setColor(new Color(0, 0, 128));
        sheet.getRange().get("A1:B5").getStyle().getBorders().getByBordersLineType(BordersLineType.EdgeRight).setLineStyle(LineStyleType.Thin);
        sheet.getRange().get("B2:C5").getStyle().setNumberFormat("\"$\"#,##0");

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

        //Set data range for the chart
        chart.setDataRange(sheet.getRange().get("B2:B5"));
        chart.setSeriesDataFromRange(false);

        //Set chart position
        chart.setLeftColumn(1);
        chart.setTopRow(7);
        chart.setRightColumn(9);
        chart.setBottomRow(26);

        //Set chart title
        chart.setChartTitle("Sales Report");
        chart.getChartTitleArea().isBold(true);
        chart.getChartTitleArea().setSize(12);

        //Set value of chart series
        ChartSerie cs = chart.getSeries().get(0);
        cs.setCategoryLabels(sheet.getRange().get("A2:A5"));;
        cs.setValues(sheet.getRange().get("B2:B5"));
        cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true);
        chart.getPlotArea().getFill().setVisible(false);

        //Save the resultant file
        workbook.saveToFile("PieChart.xlsx", FileFormat.Version2013);
    }
}

Pie Chart

Doughnut Chart

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

import java.awt.*;

public class DoughnutChart {
    public static void main(String[] args){
        //Create a Workbook
        Workbook workbook = new Workbook();

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

        //Set worksheet name
        sheet.setName("Doughnut Chart");

        //Set gridlines invisible
        sheet.setGridLinesVisible(false);

        //Add data to the worksheet
        sheet.getRange().get("A1").setValue("Country");
        sheet.getRange().get("A1").getStyle().getFont().isBold(true);
        sheet.getRange().get("A2").setValue("Cuba");
        sheet.getRange().get("A3").setValue("Mexico");
        sheet.getRange().get("A4").setValue("France");
        sheet.getRange().get("A5").setValue("Germany");
        sheet.getRange().get("B1").setValue("Amount");
        sheet.getRange().get("B1").getStyle().getFont().isBold(true);
        sheet.getRange().get("B2").setNumberValue(6000);
        sheet.getRange().get("B3").setNumberValue(8000);
        sheet.getRange().get("B4").setNumberValue(9000);
        sheet.getRange().get("B5").setNumberValue(8500);

        //Create doughnut chart
        Chart chart = sheet.getCharts().add(ExcelChartType.Doughnut);

        //Set data range for the chart
        chart.setDataRange(sheet.getRange().get("A1:B5"));
        chart.setSeriesDataFromRange(false);

        //Set chart position
        chart.setLeftColumn(4);
        chart.setTopRow(2);
        chart.setRightColumn(12);
        chart.setBottomRow(22);

        //Set chart title
        chart.setChartTitle("Sales Percentage");;
        chart.getChartTitleArea().isBold(true);
        chart.getChartTitleArea().setSize(10);

        //Set value of chart series
        for (ChartSerie cs: (Iterable<ChartSerie>) chart.getSeries()
             ) {
            cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasPercentage(true);
        }

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

        //Save the resultant file
        workbook.saveToFile("DoughnutChart.xlsx", FileFormat.Version2013);
    }
}

Doughnut Chart

Discussion (0)

pic
Editor guide