// Create a new workbook Workbook workbook = new Workbook(); IWorksheet worksheet = workbook.getWorksheets().get(0); worksheet.getRange("A:A").setColumnWidth(8); worksheet.getRange("B:B").setColumnWidth(18); worksheet.getRange("C:C").setColumnWidth(10); IShape shape = worksheet.getShapes().addChart(ChartType.XYScatter, 250, 10, 420, 250); worksheet.getRange("A1:C11").setValue(new Object[][] { { "Month", "Marketing Expense", "Revenue" }, { 1, 1849, 2911 }, { 2, 2708, 5777 }, { 3, 3474, 8625 }, { 4, 4681, 9171 }, { 5, 5205, 10308 }, { 6, 5982, 11779 }, { 7, 8371, 12138 }, { 8, 8457, 17074 }, { 9, 9554, 15729 }, { 10, 9604, 19610 } }); worksheet.getRange("A1:C1").setHorizontalAlignment(HorizontalAlignment.Right); worksheet.getRange("B2:C11").setNumberFormat("$#,##0.00"); shape.getChart().getChartTitle().setText("Revenue v/s Marketing Expense"); shape.getChart().getSeriesCollection().add(worksheet.getRange("B1:C11"), RowCol.Columns, true, true); ISeries series1 = shape.getChart().getSeriesCollection().get(0); IAxis categoryAxis = shape.getChart().getAxes().item(AxisType.Category); categoryAxis.setHasTitle(true); categoryAxis.getAxisTitle().setText("Marketing Expense"); categoryAxis.setMaximumScale(12000); categoryAxis.setMinimumScale(0); categoryAxis.setMajorUnit(2000); categoryAxis.setMinorUnit(400); IAxis valueAxis = shape.getChart().getAxes().item(AxisType.Value); valueAxis.setHasTitle(true); valueAxis.getAxisTitle().setText("Revenue"); valueAxis.setMaximumScale(25000); valueAxis.setMinimumScale(0); valueAxis.setMajorUnit(5000); valueAxis.setMinorUnit(1000); //Add trendline. ITrendline trendline = series1.getTrendlines().add(); trendline.setType(TrendlineType.Linear); //Display equation for the trendline trendline.setDisplayEquation(true); //Display R-squared value for the trendline trendline.setDisplayRSquared(true); //Format label for trendline equation IDataLabel trendlineDataLabel = trendline.getDataLabel(); trendlineDataLabel.getFont().getColor().setRGB(Color.GetPurple()); trendlineDataLabel.getFont().setSize(11); trendlineDataLabel.getFormat().getFill().getColor().setObjectThemeColor(ThemeColor.Accent4); trendlineDataLabel.getFormat().getLine().getColor().setObjectThemeColor(ThemeColor.Accent2); // Save to an excel file workbook.save("AddTrendlineLabel.xlsx");