Asse X vs. Asse delle categorie
Possibili Scenari di Utilizzo
Ci sono diversi tipi di assi X. Mentre l’asse Y è un asse di tipo Valore, l’asse X può essere un asse di tipo Categoria o un asse di tipo Valore. Utilizzando un asse di tipo Valore, i dati sono trattati come dati numerici continuamente variabili, e il marcatore è posizionato in un punto lungo l’asse che varia in base al suo valore numerico. Utilizzando un asse di tipo Categoria, i dati sono trattati come una sequenza di etichette di testo non numeriche, e il marcatore è posizionato in un punto lungo l’asse in base alla sua posizione nella sequenza. Il codice di esempio qui sotto illustra la differenza tra Assi di Valore e di Categoria. I nostri dati di esempio sono mostrati nella tabella di esempio di seguito. La prima colonna contiene i nostri dati asse X, che possono essere trattati come Categorie o come Valori. Nota che i numeri non sono equispaziati, né appaiono in ordine numerico.
Gestire l’asse X e il’asse delle categorie come Microsoft Excel
Mostreremo questi dati su due tipi di grafico, il primo grafico è un grafico XY (disperso) con l’asse X come asse dei valori, il secondo grafico è un grafico a linee con l’asse X come asse delle categorie.
Codice di Esempio
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType, LegendPositionType | |
from aspose.cells.drawing import FillType | |
# Create an instance of Workbook | |
workbook = Workbook() | |
# Access the first worksheet. | |
worksheet = workbook.worksheets[0] | |
# Put the sample values used in charts | |
worksheet.cells.get("A2").put_value(1) | |
worksheet.cells.get("A3").put_value(3) | |
worksheet.cells.get("A4").put_value(2.5) | |
worksheet.cells.get("A5").put_value(3.5) | |
worksheet.cells.get("B1").put_value("Cats") | |
worksheet.cells.get("C1").put_value("Dogs") | |
worksheet.cells.get("D1").put_value("Fishes") | |
worksheet.cells.get("B2").put_value(7) | |
worksheet.cells.get("B3").put_value(6) | |
worksheet.cells.get("B4").put_value(5) | |
worksheet.cells.get("B5").put_value(4) | |
worksheet.cells.get("C2").put_value(7) | |
worksheet.cells.get("C3").put_value(5) | |
worksheet.cells.get("C4").put_value(4) | |
worksheet.cells.get("C5").put_value(3) | |
worksheet.cells.get("D2").put_value(8) | |
worksheet.cells.get("D3").put_value(7) | |
worksheet.cells.get("D4").put_value(3) | |
worksheet.cells.get("D5").put_value(2) | |
# Create Line Chart: X as Category Axis | |
pieIdx = worksheet.charts.add(ChartType.LINE_WITH_DATA_MARKERS, 6, 15, 20, 21) | |
# Retrieve the Chart object | |
chart = worksheet.charts[pieIdx] | |
# Add Series | |
chart.n_series.add("B2:D5", True) | |
# Set the category data | |
chart.n_series.category_data = "=Sheet1!$A$2:$A$5" | |
# Set the first series mame | |
chart.n_series[0].name = "Cats" | |
# Set the second series mame | |
chart.n_series[1].name = "Dogs" | |
# Set the third series mame | |
chart.n_series[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.plot_area.area.fill_format.fill_type = FillType.NONE | |
# Create XY (Scatter) Chart: X as Value Axis | |
pieIdx = worksheet.charts.add(ChartType.SCATTER_CONNECTED_BY_LINES_WITH_DATA_MARKER, 6, 6, 20, 12) | |
# Retrieve the Chart object | |
chart = worksheet.charts[pieIdx] | |
# Add Series | |
chart.n_series.add("B2:D5", True) | |
# Set X values for series | |
chart.n_series[0].x_values = "{1,3,2.5,3.5}" | |
chart.n_series[1].x_values = "{1,3,2.5,3.5}" | |
chart.n_series[2].x_values = "{1,3,2.5,3.5}" | |
# Set the first series mame | |
chart.n_series[0].name = "Cats" | |
# Set the second series mame | |
chart.n_series[1].name = "Dogs" | |
# Set the third series mame | |
chart.n_series[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.plot_area.area.fill_format.fill_type = FillType.NONE | |
# Save the Excel file | |
workbook.save("XAxis.xlsx") |