散点图是指在回归分析中,数据点在直角坐标系平面上的分布图,表示因变量随自变量而变化的大致趋势,常用于比较跨类别的聚合数据。下面的示例中,将演示通过Spire.XLS在Excel表格中创建散点图表的方法。
C#
//实例化Workbook类的对象,并获取第一个工作表
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
//设置列宽,工作表名
sheet.Range["A1:B1"].ColumnWidth = 15F;
sheet.Name = "散点图";
//创建散点图
Chart chart = sheet.Charts.Add(ExcelChartType.ScatterMarkers);
chart.DataRange = sheet.Range["B2:B10"];
chart.SeriesDataFromRange = false;
//指定散点图在sheet中的位置
chart.LeftColumn = 4;
chart.TopRow = 1;
chart.RightColumn = 15;
chart.BottomRow = 25;
//添加图表标题
chart.ChartTitle = "城市化与耕地面积变化散点图";
chart.ChartTitleArea.IsBold = true;
chart.ChartTitleArea.Size = 12;
chart.PlotArea.ForeGroundColor = Color.Beige;
//添加图表数据源
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 = "趋势线";
//添加坐标轴名称
chart.PrimaryValueAxis.Title = "城市化水平(%)";
chart.PrimaryCategoryAxis.Title = "耕地面积(万hm^2)";
//保存文档
workbook.SaveToFile("result.xlsx", FileFormat.Version2013);
VB.NET
'实例化Workbook类的对象,并获取第一个工作表
Dim workbook As New Workbook()
Dim sheet As Worksheet = workbook.Worksheets(0)
'设置列宽,工作表名
sheet.Range("A1:B1").ColumnWidth = 15F
sheet.Name = "散点图"
'创建散点图
Dim chart As Chart = sheet.Charts.Add(ExcelChartType.ScatterMarkers)
chart.DataRange = sheet.Range("B2:B10")
chart.SeriesDataFromRange = False
'指定散点图在sheet中的位置
chart.LeftColumn = 4
chart.TopRow = 1
chart.RightColumn = 15
chart.BottomRow = 25
'添加图表标题
chart.ChartTitle = "城市化与耕地面积变化散点图"
chart.ChartTitleArea.IsBold = True
chart.ChartTitleArea.Size = 12
chart.PlotArea.ForeGroundColor = Color.Beige
'添加图表数据源
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
'添加趋势线
Dim trendLine As IChartTrendLine = chart.Series(0).TrendLines.Add(TrendLineType.Exponential)
trendLine.Name = "趋势线"
'添加坐标轴名称
chart.PrimaryValueAxis.Title = "城市化水平(%)"
chart.PrimaryCategoryAxis.Title = "耕地面积(万hm^2)"
'保存文档
workbook.SaveToFile("result.xlsx", FileFormat.Version2013)
散点图效果: