散点图是指在回归分析中,数据点在直角坐标系平面上的分布图,表示因变量随自变量而变化的大致趋势,常用于比较跨类别的聚合数据。下面的示例中,将演示通过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)
散点图效果: