文檔首頁>>Spire.XLS 系列教程>>Spire.XLS 圖表系列教程:C# 設(shè)置 Excel 圖表坐標(biāo)軸格式
Spire.XLS 圖表系列教程:C# 設(shè)置 Excel 圖表坐標(biāo)軸格式
更多資源查看:Spire.XLS工作表教程 | Spire.Doc系列教程 | Spire.PDF系列教程
默認(rèn)情況下,創(chuàng)建圖表時(shí),Excel會(huì)自動(dòng)設(shè)置其坐標(biāo)軸屬性。這些屬性包括坐標(biāo)軸選項(xiàng),例如邊界最大值,邊界最小值,主要單位,次要單位;刻度線標(biāo)記的主要類型,次要類型;標(biāo)簽位置;橫坐標(biāo)軸交叉設(shè)置;是否逆序刻度值等。但是有時(shí)我們需要手動(dòng)設(shè)置這些屬性來完善和美化圖表。本文將介紹如何使用Spire.XLS自定義設(shè)置Excel圖表坐標(biāo)軸格式。
代碼段:
//創(chuàng)建Workbook對(duì)象,獲取第一個(gè)工作表 Workbook workbook = new Workbook(); workbook.CreateEmptySheets(1); Worksheet sheet = workbook.Worksheets[0]; //添加示例數(shù)據(jù) 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; //根據(jù)示例數(shù)據(jù)創(chuàng)建柱形圖 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"]; //自定義設(shè)置圖表坐標(biāo)軸屬性 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);
效果圖:
如果你有任何問題或意見,請?jiān)谙路皆u(píng)論區(qū)留言~