默认情况下,创建图表时,excel会自动设置其坐标轴属性。这些属性包括坐标轴选项,例如边界最大值,边界最小值,主要单位,次要单位;刻度线标记的主要类型,次要类型;标签位置;横坐标轴交叉设置;是否逆序刻度值等。但是有时我们需要手动设置这些属性来完善和美化图表。本文将介绍如何使用spire.xls自定义设置excel图表坐标轴格式。
c#
//创建workbook对象,获取第一个工作表
workbook workbook = new workbook();
workbook.createemptysheets(1);
worksheet sheet = workbook.worksheets[0];
//添加示例数据
sheet.name = "demo";
sheet.range["a1"].value = "month";
sheet.range["a2"].value = "jan";
sheet.range["a3"].value = "feb";
sheet.range["a4"].value = "mar";
sheet.range["a5"].value = "apr";
sheet.range["a6"].value = "may";
sheet.range["a7"].value = "jun";
sheet.range["a8"].value = "jul";
sheet.range["a9"].value = "aug";
sheet.range["b1"].value = "planned";
sheet.range["b2"].numbervalue = 38;
sheet.range["b3"].numbervalue = 47;
sheet.range["b4"].numbervalue = 39;
sheet.range["b5"].numbervalue = 36;
sheet.range["b6"].numbervalue = 27;
sheet.range["b7"].numbervalue = 25;
sheet.range["b8"].numbervalue = 36;
sheet.range["b9"].numbervalue = 48;
//根据示例数据创建柱形图
chart chart = sheet.charts.add(excelcharttype.columnclustered);
chart.datarange = sheet.range["b1:b9"];
chart.seriesdatafromrange = false;
chart.plotarea.visible = false;
chart.toprow = 6;
chart.bottomrow = 25;
chart.leftcolumn = 2;
chart.rightcolumn = 9;
chart.charttitle = "chart with customized axis";
chart.charttitlearea.isbold = true;
chart.charttitlearea.size = 12;
spire.xls.charts.chartserie cs1 = chart.series[0];
cs1.categorylabels = sheet.range["a2:a9"];
//自定义设置图表坐标轴属性
chart.primaryvalueaxis.majorunit = 8;
chart.primaryvalueaxis.minorunit = 2;
chart.primaryvalueaxis.maxvalue = 50;
chart.primaryvalueaxis.minvalue = 0;
chart.primaryvalueaxis.isreverseorder = false;
chart.primaryvalueaxis.majortickmark = tickmarktype.tickmarkoutside;
chart.primaryvalueaxis.minortickmark = tickmarktype.tickmarkinside;
chart.primaryvalueaxis.ticklabelposition = ticklabelpositiontype.ticklabelpositionnexttoaxis;
chart.primaryvalueaxis.crossesat = 0;
//保存文档
workbook.savetofile("result.xlsx", excelversion.version2010);
vb.net
'创建workbook对象,获取第一个工作表
dim workbook as new workbook()
workbook.createemptysheets(1)
dim sheet as worksheet = workbook.worksheets(0)
'添加示例数据
sheet.name = "demo"
sheet.range("a1").value = "month"
sheet.range("a2").value = "jan"
sheet.range("a3").value = "feb"
sheet.range("a4").value = "mar"
sheet.range("a5").value = "apr"
sheet.range("a6").value = "may"
sheet.range("a7").value = "jun"
sheet.range("a8").value = "jul"
sheet.range("a9").value = "aug"
sheet.range("b1").value = "planned"
sheet.range("b2").numbervalue = 38
sheet.range("b3").numbervalue = 47
sheet.range("b4").numbervalue = 39
sheet.range("b5").numbervalue = 36
sheet.range("b6").numbervalue = 27
sheet.range("b7").numbervalue = 25
sheet.range("b8").numbervalue = 36
sheet.range("b9").numbervalue = 48
'根据示例数据创建柱形图
dim chart as chart = sheet.charts.add(excelcharttype.columnclustered)
chart.datarange = sheet.range("b1:b9")
chart.seriesdatafromrange = false
chart.plotarea.visible = false
chart.toprow = 6
chart.bottomrow = 25
chart.leftcolumn = 2
chart.rightcolumn = 9
chart.charttitle = "chart with customized axis"
chart.charttitlearea.isbold = true
chart.charttitlearea.size = 12
dim cs1 as spire.xls.charts.chartserie = chart.series(0)
cs1.categorylabels = sheet.range("a2:a9")
'自定义设置图表坐标轴属性
chart.primaryvalueaxis.majorunit = 8
chart.primaryvalueaxis.minorunit = 2
chart.primaryvalueaxis.maxvalue = 50
chart.primaryvalueaxis.minvalue = 0
chart.primaryvalueaxis.isreverseorder = false
chart.primaryvalueaxis.majortickmark = tickmarktype.tickmarkoutside
chart.primaryvalueaxis.minortickmark = tickmarktype.tickmarkinside
chart.primaryvalueaxis.ticklabelposition = ticklabelpositiontype.ticklabelpositionnexttoaxis
chart.primaryvalueaxis.crossesat = 0
'保存文档
workbook.savetofile("result.xlsx", excelversion.version2010)
截图: