文檔首頁>>Spire.XLS 系列教程>>Spire.XLS 圖表系列教程:C# 在 Excel 中創(chuàng)建散點(diǎn)圖表
Spire.XLS 圖表系列教程:C# 在 Excel 中創(chuàng)建散點(diǎn)圖表
更多資源查看:Spire.XLS工作表教程 | Spire.Doc系列教程 | Spire.PDF系列教程
散點(diǎn)圖是指在回歸分析中,數(shù)據(jù)點(diǎn)在直角坐標(biāo)系平面上的分布圖,表示因變量隨自變量而變化的大致趨勢,常用于比較跨類別的聚合數(shù)據(jù)。下面的示例中,將演示通過Spire.XLS在Excel表格中創(chuàng)建散點(diǎn)圖表的方法。
//實(shí)例化Workbook類的對象,并獲取第一個(gè)工作表 Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; //設(shè)置列寬,工作表名 sheet.Range["A1:B1"].ColumnWidth = 15F; sheet.Name = "散點(diǎn)圖"; //創(chuàng)建散點(diǎn)圖 Chart chart = sheet.Charts.Add(ExcelChartType.ScatterMarkers); chart.DataRange = sheet.Range["B2:B10"]; chart.SeriesDataFromRange = false; //指定散點(diǎn)圖在sheet中的位置 chart.LeftColumn = 4; chart.TopRow = 1; chart.RightColumn = 15; chart.BottomRow = 25; //添加圖表標(biāo)題 chart.ChartTitle = "城市化與耕地面積變化散點(diǎn)圖"; chart.ChartTitleArea.IsBold = true; chart.ChartTitleArea.Size = 12; chart.PlotArea.ForeGroundColor = Color.Beige; //添加圖表數(shù)據(jù)源 sheet.Range["A1"].Value = "城市化水平"; sheet.Range["A2"].Value = "10"; sheet.Range["A3"].Value = "15"; sheet.Range["A4"].Value = "17"; sheet.Range["A5"].Value = "25"; sheet.Range["A6"].Value = "35"; sheet.Range["A7"].Value = "40"; sheet.Range["A8"].Value = "38"; sheet.Range["A10"].Value = "17"; sheet.Range["A9"].Value = "24"; sheet.Range["B1"].Value = "耕地面積"; sheet.Range["B2"].Value = "26780"; sheet.Range["B3"].Value = "24086"; sheet.Range["B4"].Value = "20546"; sheet.Range["B5"].Value = "15057"; sheet.Range["B6"].Value = "11036"; sheet.Range["B7"].Value = "12546"; sheet.Range["B8"].Value = "9854"; sheet.Range["B9"].Value = "13506"; sheet.Range["B10"].Value = "18756"; chart.Series[0].CategoryLabels = sheet.Range["B2:B10"]; chart.Series[0].Values = sheet.Range["A2:A10"]; sheet.Range["A1:B10"].Style.HorizontalAlignment = HorizontalAlignType.Center; sheet.Range["A1:B1"].Style.Font.IsBold = true; //添加趨勢線 IChartTrendLine trendLine = chart.Series[0].TrendLines.Add(TrendLineType.Exponential); trendLine.Name = "趨勢線"; //添加坐標(biāo)軸名稱 chart.PrimaryValueAxis.Title = "城市化水平(%)"; chart.PrimaryCategoryAxis.Title = "耕地面積(萬hm^2)"; //保存文檔 workbook.SaveToFile("result.xlsx", FileFormat.Version2013);
散點(diǎn)圖效果:
如果你有任何問題或意見,請?jiān)谙路皆u論區(qū)留言~