Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 19, 2022 07:44 am GMT

Create a Line Chart in Excel in Java

A Line Chart is a basic type of charts that depicts trends and behaviors over time. It displays information as a series of data points connected by straight line segments. This article will share how to create a line chart in Excel using Free Spire.XLS for Java.

Import Dependency (2 Methods)

1# Download the free library and unzip it, then add the Spire.Xls.jar file to your project as dependency.

2# Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

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

Sample Code

The Worksheet.getCharts().add(ExcelChartType.Line) method offered by Free Spire.XLS for Java allows you to insert a line chart to the Excel worksheet. The complete sample code are shown as below.

import com.spire.xls.*;import com.spire.xls.charts.ChartSerie;import java.awt.*;public class CreateLineChart {    public static void main(String []args){        //Create a Workbook instance        Workbook workbook = new Workbook();        //Get the first worksheet        Worksheet sheet = workbook.getWorksheets().get(0);        //Set sheet name        sheet.setName("Line Chart");;        //Hide gridlines        sheet.setGridLinesVisible(false);        //Add some data to the worksheet        sheet.getRange().get("A1").setValue("Country");        sheet.getRange().get("A2").setValue("Cuba");        sheet.getRange().get("A3").setValue("Mexico");        sheet.getRange().get("A4").setValue("France");        sheet.getRange().get("A5").setValue("German");        sheet.getRange().get("B1").setValue("Jun");        sheet.getRange().get("B2").setNumberValue(3300);        sheet.getRange().get("B3").setNumberValue(2300);        sheet.getRange().get("B4").setNumberValue(4500);        sheet.getRange().get("B5").setNumberValue(6700);        sheet.getRange().get("C1").setValue("Jul");        sheet.getRange().get("C2").setNumberValue(7500);        sheet.getRange().get("C3").setNumberValue(2900);        sheet.getRange().get("C4").setNumberValue(2300);        sheet.getRange().get("C5").setNumberValue(4200);        sheet.getRange().get("D1").setValue("Aug");        sheet.getRange().get("D2").setNumberValue(7700);        sheet.getRange().get("D3").setNumberValue(6900);        sheet.getRange().get("D4").setNumberValue(8400);        sheet.getRange().get("D5").setNumberValue(4200);        sheet.getRange().get("E1").setValue("Sep");        sheet.getRange().get("E2").setNumberValue(8000);        sheet.getRange().get("E3").setNumberValue(7200);        sheet.getRange().get("E4").setNumberValue(8300);        sheet.getRange().get("E5").setNumberValue(5600);        //Set font and fill color for specified cells        sheet.getRange().get("A1:E1").getStyle().getFont().isBold(true);        sheet.getRange().get("A1:E1").getStyle().setKnownColor(ExcelColors.LightGreen1);;        sheet.getRange().get("A2:A5").getStyle().setKnownColor(ExcelColors.LightYellow);        //Set cell borders        sheet.getRange().get("A1:E5").borderInside(LineStyleType.Thin, Color.BLUE);        sheet.getRange().get("A1:E5").borderAround(LineStyleType.Medium, Color.BLACK);        //Set number format        sheet.getRange().get("B2:D5").getStyle().setNumberFormat("\"$\"#,##0");        //Add a line chart to the worksheet        Chart chart = sheet.getCharts().add(ExcelChartType.Line);        //Set data range for the chart        chart.setDataRange(sheet.getRange().get("A1:E5"));        //Set position of the chart        chart.setLeftColumn(1);        chart.setTopRow(6);        chart.setRightColumn(11);        chart.setBottomRow(29);        //Set and format chart title        chart.setChartTitle("Sales Report");        chart.getChartTitleArea().isBold(true);        chart.getChartTitleArea().setSize(12);        //Set and format category axis title        chart.getPrimaryCategoryAxis().setTitle("Month");        chart.getPrimaryCategoryAxis().getFont().isBold(true);        chart.getPrimaryCategoryAxis().getTitleArea().isBold(true);        //Set and format value axis title        chart.getPrimaryValueAxis().setTitle("Sales (in USD)");        chart.getPrimaryValueAxis().hasMajorGridLines(false);        chart.getPrimaryValueAxis().getTitleArea().setTextRotationAngle(-90);        chart.getPrimaryValueAxis().setMinValue(1000);        chart.getPrimaryValueAxis().getTitleArea().isBold(true);        //Loop through the data series of the chart        for(ChartSerie cs : (Iterable<ChartSerie>) chart.getSeries())        {            cs.getFormat().getOptions().isVaryColor(true);            //Show data labels for data points            cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true);        }        //Set position of chart legend        chart.getLegend().setPosition(LegendPositionType.Top);        //Save the result file        workbook.saveToFile("LineChart.xlsx", ExcelVersion.Version2016);    }}

The Result Document

Line Chart


Original Link: https://dev.to/carlwils/create-a-line-chart-in-excel-in-java-3nn7

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To