Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AsposeCellsBlog/b1b8be4736e3eb508ac9ea2914a882f9 to your computer and use it in GitHub Desktop.
Save AsposeCellsBlog/b1b8be4736e3eb508ac9ea2914a882f9 to your computer and use it in GitHub Desktop.
Create Microsoft Excel Line Chart - Java
// Directory path of input and output files.
String dirPath = "D:/Download/";
// Load source Excel file containing the chart data.
Workbook wb = new Workbook(dirPath + "sampleCreateMicrosoftExcelLineChart.xlsx");
// Access first worksheet.
Worksheet ws = wb.getWorksheets().get(0);
// Specify dimensions of the chart.
int upperLeftRow = 7;
int upperLeftColumn = 4;
int lowerRightRow = 24;
int lowerRightColumn = 13;
// Create Line chart with specified dimensions.
int idx = ws.getCharts().add(ChartType.LINE, upperLeftRow, upperLeftColumn, lowerRightRow, lowerRightColumn);
// Access the Line chart.
Chart ch = ws.getCharts().get(idx);
// Get the white and greenish color.
Color clr1 = Color.fromArgb(255, 255, 255);
Color clr2 = Color.fromArgb(226, 240, 217);
// Apply two color gradient on the chart area.
ch.getChartArea().getArea().getFillFormat().setTwoColorGradient(clr1, clr2, GradientStyleType.FROM_CENTER, 1);
// Set the chart title, make it non-bold and set its font size.
ch.getTitle().setText("Classification of Languages");
ch.getTitle().getFont().setBold(false);
ch.getTitle().getFont().setSize(15);
// Add three vertical series in chart covering the range B2:D5.
ch.getNSeries().add("B2:D5", true);
// Set the category data covering the range A2:A5.
ch.getNSeries().setCategoryData("A2:A5");
// Set the names of the chart series taken from cells.
ch.getNSeries().get(0).setName("=B1");
ch.getNSeries().get(1).setName("=C1");
ch.getNSeries().get(2).setName("=D1");
// Set the line weight of the chart series.
ch.getNSeries().get(0).getBorder().setWeight(WeightType.MEDIUM_LINE);
ch.getNSeries().get(1).getBorder().setWeight(WeightType.MEDIUM_LINE);
ch.getNSeries().get(2).getBorder().setWeight(WeightType.MEDIUM_LINE);
// Set plot area formatting as none and hide its border.
ch.getPlotArea().getArea().getFillFormat().setFillType(FillType.NONE);
ch.getPlotArea().getBorder().setVisible(false);
// Set value axis major tick mark as none and hide axis line.
// Also set the color of value axis major grid lines.
ch.getValueAxis().setMajorTickMark(TickMarkType.NONE);
ch.getValueAxis().getAxisLine().setVisible(false);
ch.getValueAxis().getMajorGridLines().setColor(Color.fromArgb(217, 217, 217));
// Set category axis major tick mark as none.
// Also set the color of category axis major grid lines.
ch.getCategoryAxis().setMajorTickMark(TickMarkType.NONE);
ch.getCategoryAxis().getAxisLine().setColor(Color.fromArgb(217, 217, 217));
// Save the output Excel file in XLSX format.
wb.save(dirPath + "outputCreateMicrosoftExcelLineChart.xlsx", SaveFormat.XLSX);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment