// 新規ワークブックの作成 var workbook = new GrapeCity.Documents.Excel.Workbook(); IWorksheet worksheet = workbook.Worksheets[0]; worksheet.Range["A:A"].ColumnWidth = 8; worksheet.Range["B:B"].ColumnWidth = 10; 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[,] { { "月", "販売費", "売上高" }, { 1, 184900, 291100 }, { 2, 270800, 577700 }, { 3, 347400, 862500 }, { 4, 468100, 917100 }, { 5, 520500, 1030800 }, { 6, 598200, 1177900 }, { 7, 837100, 1213800 }, { 8, 845700, 1707400 }, { 9, 955400, 1572900 }, { 10, 960400, 1961000 } }; worksheet.Range["A1:C1"].HorizontalAlignment = HorizontalAlignment.Right; worksheet.Range["B2:C11"].NumberFormat = "¥#,##0"; shape.Chart.ChartTitle.Text = "売上高 対 販売費"; 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 = "販売費"; categoryAxis.MaximumScale = 1200000; categoryAxis.MinimumScale = 0; categoryAxis.MajorUnit = 200000; categoryAxis.MinorUnit = 40000; Drawing.IAxis valueAxis = shape.Chart.Axes.Item(Drawing.AxisType.Value); valueAxis.HasTitle = true; valueAxis.AxisTitle.Text = "売上高"; valueAxis.MaximumScale = 2500000; valueAxis.MinimumScale = 0; valueAxis.MajorUnit = 500000; valueAxis.MinorUnit = 100000; // 近似曲線を追加 Drawing.ITrendline trendline = series1.Trendlines.Add(); trendline.Type = Drawing.TrendlineType.Linear; // 近似曲線の数式を表示 trendline.DisplayEquation = true; // 近似曲線のR-2乗値を表示 trendline.DisplayRSquared = true; // 近似曲線の数式ラベルをカスタマイズ 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; // xlsx ファイルに保存 workbook.Save("AddTrendlineLabel.xlsx");
' 新規ワークブックの作成 Dim workbook As New Workbook Dim worksheet As IWorksheet = workbook.Worksheets(0) worksheet.Range("A:A").ColumnWidth = 8 worksheet.Range("B:B").ColumnWidth = 10 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(,) { {"月", "販売費", "売上高"}, {1, 184900, 291100}, {2, 270800, 577700}, {3, 347400, 862500}, {4, 468100, 917100}, {5, 520500, 1030800}, {6, 598200, 1177900}, {7, 837100, 1213800}, {8, 845700, 1707400}, {9, 955400, 1572900}, {10, 960400, 1961000} } worksheet.Range("A1:C1").HorizontalAlignment = HorizontalAlignment.Right worksheet.Range("B2:C11").NumberFormat = "¥#,##0" shape.Chart.ChartTitle.Text = "売上高 対 販売費" 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 = "販売費" categoryAxis.MaximumScale = 1200000 categoryAxis.MinimumScale = 0 categoryAxis.MajorUnit = 200000 categoryAxis.MinorUnit = 40000 Dim valueAxis As Drawing.IAxis = shape.Chart.Axes.Item(Drawing.AxisType.Value) valueAxis.HasTitle = True valueAxis.AxisTitle.Text = "売上高" valueAxis.MaximumScale = 2500000 valueAxis.MinimumScale = 0 valueAxis.MajorUnit = 500000 valueAxis.MinorUnit = 100000 ' 近似曲線を追加 Dim trendline As ITrendline = series1.Trendlines.Add() trendline.Type = Drawing.TrendlineType.Linear ' 近似曲線の数式を表示 trendline.DisplayEquation = True ' 近似曲線のR-2乗値を表示 trendline.DisplayRSquared = True ' 近似曲線の数式ラベルをカスタマイズ 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 ' xlsx ファイルに保存 workbook.Save("AddTrendlineLabel.xlsx")