Creazione di grafici dinamici
I grafici dinamici (o interattivi) hanno la capacità di cambiare quando si cambia l’ambito dei dati. In altre parole, i grafici dinamici possono riflettere automaticamente i cambiamenti quando viene modificata la fonte dati. Per innescare il cambiamento nella fonte dati, è possibile utilizzare l’opzione di filtraggio delle tabelle di Excel o utilizzare un controllo come ComboBox o elenco a discesa.
Questo articolo dimostra l’utilizzo delle API di Aspose.Cells per Python via .NET per creare grafici dinamici usando entrambi gli approcci sopra menzionati.
Utilizzo delle tabelle di Excel
ListObjects fornisce la funzionalità integrata per ordinare e filtrare i dati tramite interazione dell’utente. Entrambe le opzioni di ordinamento e filtro sono fornite attraverso i menu a tendina che vengono aggiunti automaticamente alla riga di intestazione. Grazie a queste funzionalità (ordinamento e filtro), il ListObject sembra essere il candidato ideale per fungere da sorgente dati per un grafico dinamico perché, quando si cambia l’ordinamento o il filtro, la rappresentazione dei dati nel grafico verrà modificata per riflettere lo stato attuale del .
Al fine di mantenere la dimostrazione semplice da comprendere, creeremo il Workbook da zero e procederemo passo dopo passo come indicato di seguito.
- Creare un Workbook vuoto.
- Accedere al Cells del primo Worksheet nel Workbook.
- Inserire alcuni dati nelle celle.
- Creare ListObject basato sui dati inseriti.
- Creare Chart basato sull’intervallo di dati di ListObject.
- Salvare il risultato sul disco.
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create an instance of Workbook | |
book = Workbook() | |
# Access first worksheet from the collection | |
sheet = book.worksheets[0] | |
# Access cells collection of the first worksheet | |
cells = sheet.cells | |
# Insert data column wise | |
cells.get("A1").put_value("Category") | |
cells.get("A2").put_value("Fruit") | |
cells.get("A3").put_value("Fruit") | |
cells.get("A4").put_value("Fruit") | |
cells.get("A5").put_value("Fruit") | |
cells.get("A6").put_value("Vegetables") | |
cells.get("A7").put_value("Vegetables") | |
cells.get("A8").put_value("Vegetables") | |
cells.get("A9").put_value("Vegetables") | |
cells.get("A10").put_value("Beverages") | |
cells.get("A11").put_value("Beverages") | |
cells.get("A12").put_value("Beverages") | |
cells.get("B1").put_value("Food") | |
cells.get("B2").put_value("Apple") | |
cells.get("B3").put_value("Banana") | |
cells.get("B4").put_value("Apricot") | |
cells.get("B5").put_value("Grapes") | |
cells.get("B6").put_value("Carrot") | |
cells.get("B7").put_value("Onion") | |
cells.get("B8").put_value("Cabage") | |
cells.get("B9").put_value("Potatoe") | |
cells.get("B10").put_value("Coke") | |
cells.get("B11").put_value("Coladas") | |
cells.get("B12").put_value("Fizz") | |
cells.get("C1").put_value("Cost") | |
cells.get("C2").put_value(2.2) | |
cells.get("C3").put_value(3.1) | |
cells.get("C4").put_value(4.1) | |
cells.get("C5").put_value(5.1) | |
cells.get("C6").put_value(4.4) | |
cells.get("C7").put_value(5.4) | |
cells.get("C8").put_value(6.5) | |
cells.get("C9").put_value(5.3) | |
cells.get("C10").put_value(3.2) | |
cells.get("C11").put_value(3.6) | |
cells.get("C12").put_value(5.2) | |
cells.get("D1").put_value("Profit") | |
cells.get("D2").put_value(0.1) | |
cells.get("D3").put_value(0.4) | |
cells.get("D4").put_value(0.5) | |
cells.get("D5").put_value(0.6) | |
cells.get("D6").put_value(0.7) | |
cells.get("D7").put_value(1.3) | |
cells.get("D8").put_value(0.8) | |
cells.get("D9").put_value(1.3) | |
cells.get("D10").put_value(2.2) | |
cells.get("D11").put_value(2.4) | |
cells.get("D12").put_value(3.3) | |
# Create ListObject, Get the List objects collection in the first worksheet | |
listObjects = sheet.list_objects | |
# Add a List based on the data source range with headers on | |
index = listObjects.add(0, 0, 11, 3, True) | |
sheet.auto_fit_columns() | |
# Create chart based on ListObject | |
index = sheet.charts.add(ChartType.COLUMN, 21, 1, 35, 18) | |
chart = sheet.charts[index] | |
chart.set_chart_data_range("A1:D12", True) | |
chart.n_series.category_data = "A2:B12" | |
# Save spreadsheet | |
book.save(dataDir + "output_out.xlsx") |
Utilizzo di Formule Dinamiche
Nel caso in cui non si desideri utilizzare il ListObject come fonte dati per il grafico dinamico, l’altra opzione è utilizzare le funzioni di Excel (o formule) per creare un intervallo dinamico di dati e un controllo (come ComboBox) per innescare il cambiamento dei dati. In questo scenario, utilizzeremo la funzione VLOOKUP per recuperare i valori appropriati in base alla selezione di ComboBox. Quando viene cambiata la selezione, la funzione VLOOKUP aggiornerà il valore della cella. Se un intervallo di celle sta utilizzando la funzione VLOOKUP, l’intero intervallo può essere aggiornato all’interazione dell’utente, quindi può essere utilizzato come fonte per il grafico dinamico.
Al fine di mantenere la dimostrazione semplice da comprendere, creeremo il Workbook da zero e procederemo passo dopo passo come indicato di seguito.
- Creare un Workbook vuoto.
- Accedere al Cells del primo Worksheet nel Workbook.
- Inserire alcuni dati nelle celle creando un Named Range. Questi dati fungeranno da serie per il grafico dinamico.
- Creare ComboBox basato sul Named Range creato nel passo precedente.
- Inserire ulteriori dati nelle celle che fungeranno da fonte per la funzione VLOOKUP.
- Inserire la funzione VLOOKUP (con i parametri appropriati) in un intervallo di celle. Questo intervallo servirà come origine per il grafico dinamico.
- Creare Chart basato sull’intervallo creato nel passo precedente.
- Salvare il risultato sul disco.
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType | |
from aspose.pydrawing import Color | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create a workbook object | |
workbook = Workbook() | |
# Get the first worksheet | |
worksheet = workbook.worksheets[0] | |
# Create a range in the second worksheet | |
range = worksheet.cells.create_range("C21", "C24") | |
# Name the range | |
range.name = "MyRange" | |
# Fill different cells with data in the range | |
range.get(0, 0).put_value("North") | |
range.get(1, 0).put_value("South") | |
range.get(2, 0).put_value("East") | |
range.get(3, 0).put_value("West") | |
comboBox = worksheet.shapes.add_combo_box(15, 0, 2, 0, 17, 64) | |
comboBox.input_range = "=MyRange" | |
comboBox.linked_cell = "=B16" | |
comboBox.selected_index = 0 | |
cell = worksheet.cells.get("B16") | |
style = cell.get_style() | |
style.font.color = Color.white | |
cell.set_style(style) | |
worksheet.cells.get("C16").formula = "=INDEX(Sheet1!$C$21:$C$24,$B$16,1)" | |
# Put some data for chart source | |
# Data Headers | |
worksheet.cells.get("D15").put_value("Jan") | |
worksheet.cells.get("D20").put_value("Jan") | |
worksheet.cells.get("E15").put_value("Feb") | |
worksheet.cells.get("E20").put_value("Feb") | |
worksheet.cells.get("F15").put_value("Mar") | |
worksheet.cells.get("F20").put_value("Mar") | |
worksheet.cells.get("G15").put_value("Apr") | |
worksheet.cells.get("G20").put_value("Apr") | |
worksheet.cells.get("H15").put_value("May") | |
worksheet.cells.get("H20").put_value("May") | |
worksheet.cells.get("I15").put_value("Jun") | |
worksheet.cells.get("I20").put_value("Jun") | |
# Data | |
worksheet.cells.get("D21").put_value(304) | |
worksheet.cells.get("D22").put_value(402) | |
worksheet.cells.get("D23").put_value(321) | |
worksheet.cells.get("D24").put_value(123) | |
worksheet.cells.get("E21").put_value(300) | |
worksheet.cells.get("E22").put_value(500) | |
worksheet.cells.get("E23").put_value(219) | |
worksheet.cells.get("E24").put_value(422) | |
worksheet.cells.get("F21").put_value(222) | |
worksheet.cells.get("F22").put_value(331) | |
worksheet.cells.get("F23").put_value(112) | |
worksheet.cells.get("F24").put_value(350) | |
worksheet.cells.get("G21").put_value(100) | |
worksheet.cells.get("G22").put_value(200) | |
worksheet.cells.get("G23").put_value(300) | |
worksheet.cells.get("G24").put_value(400) | |
worksheet.cells.get("H21").put_value(200) | |
worksheet.cells.get("H22").put_value(300) | |
worksheet.cells.get("H23").put_value(400) | |
worksheet.cells.get("H24").put_value(500) | |
worksheet.cells.get("I21").put_value(400) | |
worksheet.cells.get("I22").put_value(200) | |
worksheet.cells.get("I23").put_value(200) | |
worksheet.cells.get("I24").put_value(100) | |
# Dynamically load data on selection of Dropdown value | |
worksheet.cells.get("D16").formula = "=IFERROR(VLOOKUP($C$16,$C$21:$I$24,2,FALSE),0)" | |
worksheet.cells.get("E16").formula = "=IFERROR(VLOOKUP($C$16,$C$21:$I$24,3,FALSE),0)" | |
worksheet.cells.get("F16").formula = "=IFERROR(VLOOKUP($C$16,$C$21:$I$24,4,FALSE),0)" | |
worksheet.cells.get("G16").formula = "=IFERROR(VLOOKUP($C$16,$C$21:$I$24,5,FALSE),0)" | |
worksheet.cells.get("H16").formula = "=IFERROR(VLOOKUP($C$16,$C$21:$I$24,6,FALSE),0)" | |
worksheet.cells.get("I16").formula = "=IFERROR(VLOOKUP($C$16,$C$21:$I$24,7,FALSE),0)" | |
# Create Chart | |
index = worksheet.charts.add(ChartType.COLUMN, 0, 3, 12, 9) | |
chart = worksheet.charts[index] | |
chart.n_series.add("='Sheet1'!$D$16:$I$16", False) | |
chart.n_series[0].name = "=C16" | |
chart.n_series.category_data = "=$D$15:$I$15" | |
# Save result on disc | |
workbook.save(dataDir + "output_out.xlsx") |