X轴与分类轴的区别
Contents
[
Hide
]
可能的使用场景
X轴有不同类型。而Y轴是一个值类型轴,X轴可以是分类类型轴或值类型轴。使用值轴,数据被视为连续变化的数值数据,并且标记位于沿轴的变动点,其位置根据其数值而变化。使用分类轴,数据被视为一系列非数值文本标签,并且标记位于沿轴的一个位置,其位置根据其在序列中的位置而变化。下面的示例说明了值轴和分类轴之间的区别。 我们的示例数据如下图中的示例表文件所示。第一列包含我们的X轴数据,可以视为分类或值。请注意,数字不是等间距的,也不按照数字顺序出现。
处理X轴和分类轴,就像处理Microsoft Excel一样
我们将在两种类型的图表上显示这些数据,第一个图表是XY(散点)图,X作为值轴,第二个图表是折线图,X作为分类轴。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create an instance of Workbook | |
Workbook workbook = new Workbook(); | |
// Access the first worksheet. | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Put the sample values used in charts | |
worksheet.Cells["A2"].PutValue(1); | |
worksheet.Cells["A3"].PutValue(3); | |
worksheet.Cells["A4"].PutValue(2.5); | |
worksheet.Cells["A5"].PutValue(3.5); | |
worksheet.Cells["B1"].PutValue("Cats"); | |
worksheet.Cells["C1"].PutValue("Dogs"); | |
worksheet.Cells["D1"].PutValue("Fishes"); | |
worksheet.Cells["B2"].PutValue(7); | |
worksheet.Cells["B3"].PutValue(6); | |
worksheet.Cells["B4"].PutValue(5); | |
worksheet.Cells["B5"].PutValue(4); | |
worksheet.Cells["C2"].PutValue(7); | |
worksheet.Cells["C3"].PutValue(5); | |
worksheet.Cells["C4"].PutValue(4); | |
worksheet.Cells["C5"].PutValue(3); | |
worksheet.Cells["D2"].PutValue(8); | |
worksheet.Cells["D3"].PutValue(7); | |
worksheet.Cells["D4"].PutValue(3); | |
worksheet.Cells["D5"].PutValue(2); | |
//Create Line Chart: X as Category Axis | |
int pieIdx = worksheet.Charts.Add(ChartType.LineWithDataMarkers, 6, 15, 20, 21); | |
// Retrieve the Chart object | |
Chart chart = worksheet.Charts[pieIdx]; | |
// Add Series | |
chart.NSeries.Add("B2:D5", true); | |
// Set the category data | |
chart.NSeries.CategoryData = "=Sheet1!$A$2:$A$5"; | |
// Set the first series mame | |
chart.NSeries[0].Name = "Cats"; | |
// Set the second series mame | |
chart.NSeries[1].Name = "Dogs"; | |
// Set the third series mame | |
chart.NSeries[2].Name = "Fishes"; | |
// Set the Legend at the bottom of the chart area | |
chart.Legend.Position = LegendPositionType.Bottom; | |
// Fill the PlotArea area with nothing | |
chart.PlotArea.Area.FillFormat.FillType = FillType.None; | |
// Create XY (Scatter) Chart: X as Value Axis | |
pieIdx = worksheet.Charts.Add(ChartType.ScatterConnectedByLinesWithDataMarker, 6, 6, 20, 12); | |
// Retrieve the Chart object | |
chart = worksheet.Charts[pieIdx]; | |
// Add Series | |
chart.NSeries.Add("B2:D5", true); | |
// Set X values for series | |
chart.NSeries[0].XValues = "{1,3,2.5,3.5}"; | |
chart.NSeries[1].XValues = "{1,3,2.5,3.5}"; | |
chart.NSeries[2].XValues = "{1,3,2.5,3.5}"; | |
// Set the first series mame | |
chart.NSeries[0].Name = "Cats"; | |
// Set the second series mame | |
chart.NSeries[1].Name = "Dogs"; | |
// Set the third series mame | |
chart.NSeries[2].Name = "Fishes"; | |
// Set the Legend at the bottom of the chart area | |
chart.Legend.Position = LegendPositionType.Bottom; | |
// Fill the PlotArea area with nothing | |
chart.PlotArea.Area.FillFormat.FillType = FillType.None; | |
// Save the Excel file | |
workbook.Save("XAxis.xlsx"); |