在excel图表中,有时候仅用一个轴标签往往无法准确表达一个数据集的实际含义,这种情况在时间序列图表中十分常见。因此,我们需要添加多层分类标签,来对数据进行分组显示。本文将介绍如何使用spire.xls,通过编程的方式创建含多层分类标签的图表。
c#
//创建workbook实例,获取第一个工作表
workbook wb = new workbook();
worksheet sheet = wb.worksheets[0];
//写入数据到工作表
sheet.range["a1"].text = "年份";
sheet.range["a2"].text = "2016年";
sheet.range["a6"].text = "2017年";
sheet.range["b1"].text = "季度";
sheet.range["b2"].text = "第一季度";
sheet.range["b3"].text = "第二季度";
sheet.range["b4"].text = "第三季度";
sheet.range["b5"].text = "第四季度";
sheet.range["b6"].text = "第一季度";
sheet.range["b7"].text = "第二季度";
sheet.range["b8"].text = "第三季度";
sheet.range["b9"].text = "第四季度";
sheet.range["c1"].text = "季度销量";
sheet.range["c2"].value = "1544";
sheet.range["c3"].value = "1102";
sheet.range["c4"].value = "2558";
sheet.range["c5"].value = "1609";
sheet.range["c6"].value = "1700";
sheet.range["c7"].value = "1304";
sheet.range["c8"].value = "1572";
sheet.range["c9"].value = "3210";
//纵向合并单元格a2到a5、a6到a9
sheet.range["a2:a5"].merge();
sheet.range["a6:a9"].merge();
//添加柱状图
chart chart = sheet.charts.add(excelcharttype.columnclustered);
chart.charttitle = "季度销量"; //设置图标标题
chart.plotarea.fill.filltype = shapefilltype.nofill; //不填充绘图区域(默认填充灰色)
chart.legend.delete(); //删除图例
//设置柱状图位置及宽度
chart.leftcolumn = 5;
chart.toprow = 1;
chart.rightcolumn = 14;
//设置系列数据来源
chart.datarange = sheet.range["c2:c9"];
chart.seriesdatafromrange = false;
//设置系列分类标签数据来源
chartserie serie = chart.series[0];
serie.categorylabels = sheet.range["a2:b9"];
//显示多层分类标签
chart.primarycategoryaxis.multilevellable = true;
//保存文档
wb.savetofile("output.xlsx", excelversion.version2013);
vb.net
'创建workbook实例,获取第一个工作表
dim wb as new workbook()
dim sheet as worksheet = wb.worksheets(0)
'写入数据到工作表
sheet.range("a1").text = "年份"
sheet.range("a2").text = "2016年"
sheet.range("a6").text = "2017年"
sheet.range("b1").text = "季度"
sheet.range("b2").text = "第一季度"
sheet.range("b3").text = "第二季度"
sheet.range("b4").text = "第三季度"
sheet.range("b5").text = "第四季度"
sheet.range("b6").text = "第一季度"
sheet.range("b7").text = "第二季度"
sheet.range("b8").text = "第三季度"
sheet.range("b9").text = "第四季度"
sheet.range("c1").text = "季度销量"
sheet.range("c2").value = "1544"
sheet.range("c3").value = "1102"
sheet.range("c4").value = "2558"
sheet.range("c5").value = "1609"
sheet.range("c6").value = "1700"
sheet.range("c7").value = "1304"
sheet.range("c8").value = "1572"
sheet.range("c9").value = "3210"
'纵向合并单元格a2到a5、a6到a9
sheet.range("a2:a5").merge()
sheet.range("a6:a9").merge()
'添加柱状图
dim chart as chart = sheet.charts.add(excelcharttype.columnclustered)
chart.charttitle = "季度销量"
'设置图标标题
chart.plotarea.fill.filltype = shapefilltype.nofill
'不填充绘图区域(默认填充灰色)
chart.legend.delete()
'删除图例
'设置柱状图位置及宽度
chart.leftcolumn = 5
chart.toprow = 1
chart.rightcolumn = 14
'设置系列数据来源
chart.datarange = sheet.range("c2:c9")
chart.seriesdatafromrange = false
'设置系列分类标签数据来源
dim serie as chartserie = chart.series(0)
serie.categorylabels = sheet.range("a2:b9")
'显示多层分类标签
chart.primarycategoryaxis.multilevellable = true
'保存文档
wb.savetofile("output.xlsx", excelversion.version2013)