Tre metodi per filtrare i dati del grafico
1. Filtrare le serie per visualizzare un grafico
Passaggi per filtrare le serie da un grafico in Excel
In Excel, possiamo filtrare serie specifiche da un grafico, causando la non visualizzazione di tali serie filtrate nel grafico. Il grafico originale è mostrato nella Figura 1. Tuttavia, quando filtriamo Testseries2 e Testseries4, il grafico apparirà come mostrato nella Figura 2.
In Aspose.Cells, possiamo eseguire un’operazione simile. Per un esempio di file come questo, se vogliamo filtrare Testseries2 e Testseries4, possiamo eseguire il seguente codice. Inoltre, manterremo due liste: una (NSeries) lista per memorizzare tutte le serie selezionate e un’altra (FilteredNSeries) per memorizzare le serie filtrate.
Si prega di notare che nel codice, quando impostiamo chart.NSeries[0].IsFiltered = true;, la prima serie in NSeries verrà rimossa e posizionata nella posizione appropriata all’interno di FilteredNSeries. Successivamente, il precedente NSeries[1] diventerà il nuovo primo elemento nella lista, e tutte le serie successive si sposteranno in avanti di una posizione. Questo significa che se eseguiamo poi chart.NSeries[1].IsFiltered = true;, stiamo effettivamente rimuovendo la serie originale terza. Questo può a volte portare a confusione, quindi raccomandiamo di seguire l’operazione nel codice, che elimina le serie dalla fine all’inizio.
Codice di Esempio
Il seguente codice di esempio carica il file Excel di esempio.
// Create an instance of existing Workbook | |
Workbook workbook = new Workbook("seriesFiltered.xlsx"); | |
// Get filtered series list | |
SeriesCollection nSeriesFiltered = workbook.Worksheets[0].Charts["Chart 1"].FilteredNSeries; | |
// Get selected series list | |
SeriesCollection nSeries = workbook.Worksheets[0].Charts["Chart 1"].NSeries; | |
// Should be 0 | |
Console.WriteLine("Filtered Series count" + nSeriesFiltered.Count); | |
// Should be 6 | |
Console.WriteLine("Visiable Series count" + nSeries.Count); | |
// Process from the end to the beginning | |
nSeries[1].IsFiltered = true; | |
nSeries[0].IsFiltered = true; | |
// Should be 2 | |
Console.WriteLine("Filtered Series count" + nSeriesFiltered.Count); | |
// Should be 4 | |
Console.WriteLine("Visiable Series count" + nSeries.Count); | |
workbook.Save("seriesFiltered-out.xlsx"); | |
workbook = new Workbook("seriesFiltered-out.xlsx"); | |
// Should be 2 | |
Console.WriteLine("Filtered Series count" + nSeriesFiltered.Count); | |
// Should be 4 | |
Console.WriteLine("Visiable Series count" + nSeries.Count); |
2. Filtrare i dati e far cambiare il grafico
Filtrare i tuoi dati è un ottimo modo per gestire i filtri del grafico con molti dati. Quando filtri i dati, il grafico cambierà. Un problema che dovremo affrontare è assicurarci che il grafico rimanga sullo schermo. Quando filtri, ottieni righe nascoste, e occasionalmente, il grafico sarà in quelle righe nascoste.
Passaggi per utilizzare i filtri dei dati per modificare il grafico in Excel
- Fare clic all’interno del proprio intervallo di dati.
- Fare clic sulla scheda Dati, e attivare i filtri cliccando su Filtri. La riga di intestazione avrà frecce a discesa.
- Creare un grafico andando alla scheda Inserisci e selezionando un grafico a colonne.
- Ora filtra i tuoi dati utilizzando le frecce a discesa nei dati. Non utilizzare i filtri del grafico.
Codice di Esempio
Il seguente codice di esempio mostra la stessa funzionalità utilizzando Aspsoe.Cells.
// Create an instance of Workbook | |
Workbook workbook = new Workbook(); | |
// Get the First sheet. | |
Worksheet sheet = workbook.Worksheets["Sheet1"]; | |
// Add data into details cells. | |
sheet.Cells[0, 0].PutValue("Fruits Name"); | |
sheet.Cells[0, 1].PutValue("Fruits Price"); | |
sheet.Cells[1, 0].PutValue("Apples"); | |
sheet.Cells[2, 0].PutValue("Bananas"); | |
sheet.Cells[3, 0].PutValue("Grapes"); | |
sheet.Cells[4, 0].PutValue("Oranges"); | |
sheet.Cells[1, 1].PutValue(5); | |
sheet.Cells[2, 1].PutValue(2); | |
sheet.Cells[3, 1].PutValue(1); | |
sheet.Cells[4, 1].PutValue(4); | |
// Add a chart to the worksheet | |
int chartIndex = sheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 7, 7, 15, 15); | |
// Access the instance of the newly added chart | |
Aspose.Cells.Charts.Chart chart = sheet.Charts[chartIndex]; | |
// Set data range | |
chart.SetChartDataRange("A1:B5", true); | |
// Set AutoFilter range | |
sheet.AutoFilter.Range = "A1:B5"; | |
// Add filters for a filter column. | |
sheet.AutoFilter.AddFilter(0, "Bananas"); | |
sheet.AutoFilter.AddFilter(0, "Oranges"); | |
// Apply the filters | |
sheet.AutoFilter.Refresh(); | |
chart.ToImage("Autofilter.png"); | |
workbook.Save("Autofilter.xlsx"); |
3. Filtra i dati utilizzando una Tabella e fai cambiare il grafico
Utilizzare una Tabella è simile al Metodo 2, utilizzando un intervallo, ma hai vantaggi con le tabelle rispetto agli intervalli. Quando cambia il tuo intervallo in una Tabella e aggiungi dati, il grafico si aggiorna automaticamente. Con un intervallo, dovrai modificare la fonte dati.
Formatta come tabella in Excel
Fare clic all’interno dei dati e utilizzare CTRL + T oppure utilizzare la scheda Home, Formatta come Tabella
Codice di Esempio
Il seguente codice di esempio carica il file Excel di esempio mostra la stessa funzionalità utilizzando Aspsoe.Cells.
// Create a workbook. | |
Workbook workbook = new Workbook("TableFilters.xlsx"); | |
// Access first worksheet | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Access the instance of the newly added chart | |
int chartIndex = sheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 7, 7, 15, 15); | |
Aspose.Cells.Charts.Chart chart = sheet.Charts[chartIndex]; | |
// Set data range | |
chart.SetChartDataRange("A1:B7", true); | |
// Convert the chart to image | |
chart.ToImage("TableFilters.before.png"); | |
// Add a new List Object to the worksheet | |
Aspose.Cells.Tables.ListObject listObject = sheet.ListObjects[sheet.ListObjects.Add("A1", "B7", true)]; | |
// Add default style to the table | |
listObject.TableStyleType = Aspose.Cells.Tables.TableStyleType.TableStyleMedium10; | |
// Show Total | |
listObject.ShowTotals = false; | |
// Add filters for a filter column. | |
listObject.AutoFilter.AddFilter(0, "James"); | |
// Apply the filters | |
listObject.AutoFilter.Refresh(); | |
//After adding new value the chart will change | |
listObject.PutCellValue(7, 0, "Me"); | |
listObject.PutCellValue(7, 1, 1000); | |
// Check the changed images | |
chart.ToImage("TableFilters.after.png"); | |
// Saving the Excel file | |
workbook.Save("TableFilter.out.xlsx"); |