在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)