Three methods for filtering Chart Data with C++
1. Filtering out series to render a chart
Steps to filter series from a chart in Excel
In Excel, we can filter out specific series from a chart, causing those filtered series not to be displayed in the chart. The original chart is shown in Figure 1. However, when we filter out Testseries2 and Testseries4, the chart will appear as shown in Figure 2.
In Aspose.Cells, we can perform a similar operation. For a sample file like this, if we want to filter out Testseries2 and Testseries4, we can execute the following code. Additionally, we will maintain two lists: one (GetNSeries()) list to store all the selected series and another (GetFilteredNSeries) to store the filtered series.
Please note that in the code, when we set chart->GetNSeries()->Get(0)->SetIsFiltered(true);, the first series in GetNSeries() will be removed and placed in the appropriate position within GetFilteredNSeries. Subsequently, the previous NSeries[1] will become the new first item in the list, and all the following series will shift forward by one position. This means that if we then run chart->GetNSeries()->Get(1)->SetIsFiltered(true);, we are effectively removing the original third series. This can sometimes lead to confusion, so we recommend following the operation in the code, which deletes series from the end to the beginning.
Sample Code
The following sample code loads the sample Excel file.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
<span class="c1">// Create an instance of existing Workbook
Workbook workbook(u"seriesFiltered.xlsx");
<span class="c1">// Get filtered series list
SeriesCollection nSeriesFiltered = workbook.GetWorksheets().Get(0).GetCharts().Get(u"Chart 1").GetFilteredNSeries();
<span class="c1">// Get selected series list
SeriesCollection nSeries = workbook.GetWorksheets().Get(0).GetCharts().Get(u"Chart 1").GetNSeries();
<span class="c1">// Should be 0
std::cout << "Filtered Series count: " << nSeriesFiltered.GetCount() << std::endl;
<span class="c1">// Should be 6
std::cout << "Visible Series count: " << nSeries.GetCount() << std::endl;
<span class="c1">// Process from the end to the beginning
nSeries.Get(1).SetIsFiltered(true);
nSeries.Get(0).SetIsFiltered(true);
<span class="c1">// Should be 2
std::cout << "Filtered Series count: " << nSeriesFiltered.GetCount() << std::endl;
<span class="c1">// Should be 4
std::cout << "Visible Series count: " << nSeries.GetCount() << std::endl;
<span class="c1">// Save the workbook
workbook.Save(u"seriesFiltered-out.xlsx");
<span class="c1">// Reload the workbook
workbook = Workbook(u"seriesFiltered-out.xlsx");
<span class="c1">// Should be 2
std::cout << "Filtered Series count: " << nSeriesFiltered.GetCount() << std::endl;
<span class="c1">// Should be 4
std::cout << "Visible Series count: " << nSeries.GetCount() << std::endl;
<span class="n">Aspose</span><span class="o">::</span><span class="n">Cells</span><span class="o">::</span><span class="n">Cleanup</span><span class="p">();</span>
}
2. Filter the data and let the chart change
Filtering your data is a great way to handle chart filters with a lot of data. When you filter the data, the chart will change. One issue we’re going to have to address is making sure the chart stays on the screen. When you filter, you get hidden rows, and occasionally, the chart will be in those hidden rows.
Steps to use Data Filters to change the chart in Excel
- Click inside your data range.
- Click the Data tab, and turn on Filters by clicking Filters. Your header row will have drop down arrows.
- Create a chart by going to Insert tab and selecting a columns chart.
- Now filter your data using the drop down arrows in the data. Don’t use the Chart Filters.
Sample Code
The following sample code shows the same feature using Aspose.Cells.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Charts;
int main() {
Aspose::Cells::Startup();
<span class="c1">// Create an instance of Workbook
Workbook workbook;
<span class="c1">// Get the First sheet.
Worksheet sheet = workbook.GetWorksheets().Get(u"Sheet1");
<span class="c1">// Add data into details cells.
sheet.GetCells().Get(0, 0).PutValue(u"Fruits Name");
sheet.GetCells().Get(0, 1).PutValue(u"Fruits Price");
sheet.GetCells().Get(1, 0).PutValue(u"Apples");
sheet.GetCells().Get(2, 0).PutValue(u"Bananas");
sheet.GetCells().Get(3, 0).PutValue(u"Grapes");
sheet.GetCells().Get(4, 0).PutValue(u"Oranges");
sheet.GetCells().Get(1, 1).PutValue(5);
sheet.GetCells().Get(2, 1).PutValue(2);
sheet.GetCells().Get(3, 1).PutValue(1);
sheet.GetCells().Get(4, 1).PutValue(4);
<span class="c1">// Add a chart to the worksheet
int chartIndex = sheet.GetCharts().Add(ChartType::Column, 7, 7, 15, 15);
<span class="c1">// Access the instance of the newly added chart
Chart chart = sheet.GetCharts().Get(chartIndex);
<span class="c1">// Set data range
chart.SetChartDataRange(u"A1:B5", true);
<span class="c1">// Set AutoFilter range
sheet.GetAutoFilter().SetRange(u"A1:B5");
<span class="c1">// Add filters for a filter column.
sheet.GetAutoFilter().AddFilter(0, u"Bananas");
sheet.GetAutoFilter().AddFilter(0, u"Oranges");
<span class="c1">// Apply the filters
sheet.GetAutoFilter().Refresh();
<span class="c1">// Save the chart as an image
chart.ToImage(u"Autofilter.png");
<span class="c1">// Save the workbook
workbook.Save(u"Autofilter.xlsx");
<span class="n">Aspose</span><span class="o">::</span><span class="n">Cells</span><span class="o">::</span><span class="n">Cleanup</span><span class="p">();</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
}
3. Filter the data using a Table and let the chart change
Using a Table is similar to Method 2, using a range, but you have advantages with tables over ranges. When you change your range to a Table and add data, the chart automatically updates. With a range, you will have to change the data source.
Format as table in Excel
Click inside your data and use CTRL + T or use the Home tab, Format as Table
Sample Code
The following sample code loads the sample Excel file shows the same feature using Aspose.Cells.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Charts;
using namespace Aspose::Cells::Tables;
int main()
{
Aspose::Cells::Startup();
<span class="c1">// Create a workbook
Workbook workbook(u"TableFilters.xlsx");
<span class="c1">// Access first worksheet
Worksheet sheet = workbook.GetWorksheets().Get(0);
<span class="c1">// Access the instance of the newly added chart
int chartIndex = sheet.GetCharts().Add(ChartType::Column, 7, 7, 15, 15);
Chart chart = sheet.GetCharts().Get(chartIndex);
<span class="c1">// Set data range
chart.SetChartDataRange(u"A1:B7", true);
<span class="c1">// Convert the chart to image
chart.ToImage(u"TableFilters.before.png");
<span class="c1">// Add a new List Object to the worksheet
ListObject listObject = sheet.GetListObjects().Get(sheet.GetListObjects().Add(u"A1", u"B7", true));
<span class="c1">// Add default style to the table
listObject.SetTableStyleType(TableStyleType::TableStyleMedium10);
<span class="c1">// Show Total
listObject.SetShowTotals(false);
<span class="c1">// Add filters for a filter column
listObject.GetAutoFilter().AddFilter(0, u"James");
<span class="c1">// Apply the filters
listObject.GetAutoFilter().Refresh();
<span class="c1">// After adding new value the chart will change
listObject.PutCellValue(7, 0, Object(u"Me"));
listObject.PutCellValue(7, 1, Object(1000));
<span class="c1">// Check the changed images
chart.ToImage(u"TableFilters.after.png");
<span class="c1">// Saving the Excel file
workbook.Save(u"TableFilter.out.xlsx");
<span class="n">Aspose</span><span class="o">::</span><span class="n">Cells</span><span class="o">::</span><span class="n">Cleanup</span><span class="p">();</span>
}