//create a new workbook var workbook = new GrapeCity.Documents.Excel.Workbook(); IWorksheet worksheet = workbook.Worksheets[0]; worksheet.Range["A:A"].ColumnWidth = 8; worksheet.Range["B:B"].ColumnWidth = 18; worksheet.Range["C:C"].ColumnWidth = 10; Drawing.IShape shape = worksheet.Shapes.AddChart(Drawing.ChartType.XYScatter, 250, 10, 420, 250); worksheet.Range["A1:C11"].Value = 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.Range["A1:C1"].HorizontalAlignment = HorizontalAlignment.Right; worksheet.Range["B2:C11"].NumberFormat = "$#,##0.00"; shape.Chart.ChartTitle.Text = "Revenue v/s Marketing Expense"; shape.Chart.SeriesCollection.Add(worksheet.Range["B1:C11"], Drawing.RowCol.Columns, true, true); Drawing.ISeries series1 = shape.Chart.SeriesCollection[0]; Drawing.IAxis categoryAxis = shape.Chart.Axes.Item(Drawing.AxisType.Category); categoryAxis.HasTitle = true; categoryAxis.AxisTitle.Text = "Marketing Expense"; categoryAxis.MaximumScale = 12000; categoryAxis.MinimumScale = 0; categoryAxis.MajorUnit = 2000; categoryAxis.MinorUnit = 400; Drawing.IAxis valueAxis = shape.Chart.Axes.Item(Drawing.AxisType.Value); valueAxis.HasTitle = true; valueAxis.AxisTitle.Text = "Revenue"; valueAxis.MaximumScale = 25000; valueAxis.MinimumScale = 0; valueAxis.MajorUnit = 5000; valueAxis.MinorUnit = 1000; //Add trendline. Drawing.ITrendline trendline = series1.Trendlines.Add(); trendline.Type = Drawing.TrendlineType.Linear; //Display equation for the trendline trendline.DisplayEquation = true; //Display R-squared value for the trendline trendline.DisplayRSquared = true; //Format label for trendline equation Drawing.IDataLabel trendlineDataLabel = trendline.DataLabel; trendlineDataLabel.Font.Color.RGB = Color.Purple; trendlineDataLabel.Font.Size = 11; trendlineDataLabel.Format.Fill.Color.ObjectThemeColor = ThemeColor.Accent4; trendlineDataLabel.Format.Line.Color.ObjectThemeColor = ThemeColor.Accent2; // Save to an excel file workbook.Save("AddTrendlineLabel.xlsx");
' Create a new Workbook Dim workbook As New Workbook Dim worksheet As IWorksheet = workbook.Worksheets(0) worksheet.Range("A:A").ColumnWidth = 8 worksheet.Range("B:B").ColumnWidth = 18 worksheet.Range("C:C").ColumnWidth = 10 Dim shape As IShape = worksheet.Shapes.AddChart(ChartType.XYScatter, 250, 10, 360, 230) worksheet.Range("A1:C11").Value = 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.Range("A1:C1").HorizontalAlignment = HorizontalAlignment.Right worksheet.Range("B2:C11").NumberFormat = "$#,##0.00" shape.Chart.ChartTitle.Text = "Revenue v/s Marketing Expense" shape.Chart.SeriesCollection.Add(worksheet.Range("B1:C11"), RowCol.Columns, True, True) Dim series1 As ISeries = shape.Chart.SeriesCollection(0) Dim categoryAxis As Drawing.IAxis = shape.Chart.Axes.Item(Drawing.AxisType.Category) categoryAxis.HasTitle = True categoryAxis.AxisTitle.Text = "Marketing Expense" categoryAxis.MaximumScale = 12000 categoryAxis.MinimumScale = 0 categoryAxis.MajorUnit = 2000 categoryAxis.MinorUnit = 400 Dim valueAxis As Drawing.IAxis = shape.Chart.Axes.Item(Drawing.AxisType.Value) valueAxis.HasTitle = True valueAxis.AxisTitle.Text = "Revenue" valueAxis.MaximumScale = 25000 valueAxis.MinimumScale = 0 valueAxis.MajorUnit = 5000 valueAxis.MinorUnit = 1000 'Add trendline. Dim trendline As ITrendline = series1.Trendlines.Add() trendline.Type = Drawing.TrendlineType.Linear 'Display equation for the trendline trendline.DisplayEquation = True 'Display R-squared value for the trendline trendline.DisplayRSquared = True 'Format label for trendline equation Dim trendlineDataLabel As Drawing.IDataLabel = trendline.DataLabel trendlineDataLabel.Font.Color.RGB = Color.Purple trendlineDataLabel.Font.Size = 11 trendlineDataLabel.Format.Fill.Color.ObjectThemeColor = ThemeColor.Accent4 trendlineDataLabel.Format.Line.Color.ObjectThemeColor = ThemeColor.Accent2 ' save to an excel file workbook.Save("AddTrendlineLabel.xlsx")