Eje X vs. Eje de categoría
Posibles escenarios de uso
Hay diferentes tipos de ejes X. Mientras que el eje Y es un eje de tipo Valor, el eje X puede ser un eje de tipo Categoría o un eje de tipo Valor. Con un eje de valor, los datos se tratan como datos numéricos que varían continuamente y el marcador se coloca en un punto a lo largo del eje que varía según su valor numérico. Con un eje de categoría, los datos se tratan como una secuencia de etiquetas de texto no numéricos y el marcador se coloca en un punto a lo largo del eje según su posición en la secuencia. El siguiente ejemplo ilustra la diferencia entre los ejes de valor y categoría. Nuestros datos de muestra se muestran en laarchivo de tabla de muestra abajo. La primera columna contiene nuestros datos del eje X, que se pueden tratar como Categorías o como Valores. Tenga en cuenta que los números no están espaciados por igual, ni siquiera aparecen en orden numérico.
Manejar el eje X y Categoría como Microsoft Excel
Mostraremos estos datos en dos tipos de gráficos, el primer gráfico es el gráfico XY (Dispersión) X como eje de valores, el segundo gráfico es el gráfico de líneas X como eje de categorías.
Código de muestra
// 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"); |