Aggregation in Blazor Pivot Table Component
29 Dec 20219 minutes to read
This feature is applicable only for relational data source.
End user can perform calculations over a group of values (exclusively for value fields bound in value axis) using the aggregation option. By default, values are added (summed) together. The other aggregation types are explained below.
The fields with data type such as number supports all aggregation types mentioned below except for “CalculatedField”. The fields with data type such as string, date, datetime, boolean, etc., supports “Count” and “DistinctCount” aggregation types alone.
Operator | Description |
---|---|
Sum | Displays the pivot table values with sum. |
Product | Displays the pivot table values with product. |
Count | Displays the pivot table values with count. |
DistinctCount | Displays the pivot table values with distinct count. |
Min | Displays the pivot table with minimum value. |
Max | Displays the pivot table with maximum value. |
Avg | Displays the pivot table values with average. |
Index | Displays the pivot table values with index. |
PopulationStDev | Displays the pivot table values with standard deviation of population. |
SampleStDev | Displays the pivot table values with sample standard deviation. |
PopulationVar | Displays the pivot table values with variance of population. |
SampleVar | Displays the pivot table values with sample variance. |
RunningTotals | Displays the pivot table values with running totals. |
DifferenceFrom | Displays the pivot table values with difference from the value of the base item in the base field. |
PercentageOfDifferenceFrom | Displays the pivot table values with percentage difference from the value of the base item in the base field. |
PercentageOfGrandTotal | Displays the pivot table values with percentage of grand total of all values. |
PercentageOfColumnTotal | Displays the pivot table values in each column with percentage of total values for the column. |
PercentageOfRowTotal | Displays the pivot table values in each row with percentage of total values for the row. |
PercentageOfParentTotal | Displays the pivot table values with percentage of total of all values based on selected field. |
PercentageOfParentColumnTotal | Displays the pivot table values with percentage of its parent total in each column. |
PercentageOfParentRowTotal | Displays the pivot table values with percentage of its parent total in each row. |
CalculatedField | Displays the pivot table with calculated field values. It allows user to create a new calculated field alone. |
Assigning aggregation type for value fields through API
For each value field, the aggregation type can be set using the property Type in PivotViewValue class. Meanwhile, aggregation types like SummaryTypes.DifferenceFrom and SummaryTypes.PercentageOfDifferenceFrom can check for specific field of specific item using BaseField and BaseItem properties. Likewise, SummaryTypes.PercentageOfParentTotal type can check for specific field using BaseField property. For instance, the aggregation type SummaryTypes.DifferenceFrom would intake the specified field and its corresponding member as input and its value is compared across other members in the same field and also across different fields to formulate an appropriate output value.
- Type: It allows to set the aggregate type of the field.
- BaseField: It allows to set the specific field to aggregate the values.
- BaseItem: It allows to set the specific member to aggregate the values.
@using Syncfusion.Blazor.PivotView
<SfPivotView TValue="ProductDetails">
<PivotViewDataSourceSettings DataSource="@data">
<PivotViewColumns>
<PivotViewColumn Name="Year"></PivotViewColumn>
<PivotViewColumn Name="Quarter"></PivotViewColumn>
</PivotViewColumns>
<PivotViewRows>
<PivotViewRow Name="Country"></PivotViewRow>
<PivotViewRow Name="Products"></PivotViewRow>
</PivotViewRows>
<PivotViewValues>
<PivotViewValue Name="Sold" Caption="Unit Sold" Type="SummaryTypes.DifferenceFrom" BaseField="Country" BaseItem="France"></PivotViewValue>
<PivotViewValue Name="Amount" Caption="Sold Amount" Type=SummaryTypes.Min></PivotViewValue>
</PivotViewValues>
<PivotViewFormatSettings>
<PivotViewFormatSetting Name="Amount" Format="C"></PivotViewFormatSetting>
</PivotViewFormatSettings>
</PivotViewDataSourceSettings>
</SfPivotView>
@code{
public List<ProductDetails> data { get; set; }
protected override void OnInitialized()
{
this.data = ProductDetails.GetProductData().ToList();
//Bind the data source collection here. Refer "Assigning sample data to the pivot table" section in getting started for more details.
}
}
By default, the aggregation will be considered as SummaryTypes.Sum to the value fields which had number type and for the value fields which had non-number type such as string, date, datetime, boolean, etc., the aggregation type will be considered as SummaryTypes.Count.
Show desired aggregation types in its dropdown menu
By default, all the aggregation types are displayed in the dropdown menu available in buttons. However, based on the request for an application, there may be a need to show selective aggregation types on our own. This can be achieved using the AggregateTypes property.
@using Syncfusion.Blazor.PivotView
<SfPivotView TValue="ProductDetails" ShowGroupingBar="true" AggregateTypes="@aggregateType">
<PivotViewDataSourceSettings DataSource="@dataSource">
<PivotViewColumns>
<PivotViewColumn Name="Year"></PivotViewColumn>
<PivotViewColumn Name="Quarter"></PivotViewColumn>
</PivotViewColumns>
<PivotViewRows>
<PivotViewRow Name="Country"></PivotViewRow>
<PivotViewRow Name="Products"></PivotViewRow>
</PivotViewRows>
<PivotViewValues>
<PivotViewValue Name="Sold" Caption="Units Sold"></PivotViewValue>
<PivotViewValue Name="Amount" Caption="Sold Amount"></PivotViewValue>
</PivotViewValues>
<PivotViewFormatSettings>
<PivotViewFormatSetting Name="Amount" Format="C"></PivotViewFormatSetting>
</PivotViewFormatSettings>
</PivotViewDataSourceSettings>
</SfPivotView>
@code{
public List<AggregateTypes> aggregateType = new List<AggregateTypes> {
AggregateTypes.DistinctCount,
AggregateTypes.Avg,
AggregateTypes.Product
};
public List<ProductDetails> dataSource { get; set; }
protected override void OnInitialized()
{
this.dataSource = ProductDetails.GetProductData().ToList();
//Bind the data source collection here. Refer "Assigning sample data to the pivot table" section in getting started for more details.
}
}
Modifying aggregation type for value fields at runtime
Aggregation types can be changed easily through UI at runtime. The value fields bound to grouping bar and field list appears with a dropdown icon which helps to select an appropriate aggregation type for the respective value field. On selection, the values in the pivot table will be changed dynamically.
Hiding aggregation type from button text
By default, in value axis each field would be displayed by its name and aggregation type together. To hide aggregation type and display field name alone, set the property ShowAggregationOnValueField in PivotViewDataSourceSettings class to false.
@using Syncfusion.Blazor.PivotView
<SfPivotView TValue="ProductDetails">
<PivotViewDataSourceSettings DataSource="@data" ShowAggregationOnValueField="false">
<PivotViewColumns>
<PivotViewColumn Name="Year"></PivotViewColumn>
<PivotViewColumn Name="Quarter"></PivotViewColumn>
</PivotViewColumns>
<PivotViewRows>
<PivotViewRow Name="Country"></PivotViewRow>
<PivotViewRow Name="Products"></PivotViewRow>
</PivotViewRows>
<PivotViewValues>
<PivotViewValue Name="Sold" Caption="Unit Sold"></PivotViewValue>
<PivotViewValue Name="Amount" Caption="Sold Amount"></PivotViewValue>
</PivotViewValues>
<PivotViewFormatSettings>
<PivotViewFormatSetting Name="Amount" Format="C"></PivotViewFormatSetting>
</PivotViewFormatSettings>
</PivotViewDataSourceSettings>
</SfPivotView>
@code{
public List<ProductDetails> data { get; set; }
protected override void OnInitialized()
{
this.data = ProductDetails.GetProductData().ToList();
//Bind the data source collection here. Refer "Assigning sample data to the pivot table" section in getting started for more details.
}
}
Hiding aggregation type icon from UI
By default, the icon to set aggregation type is enabled in the grouping bar. To disable this icon, set the property ShowValueTypeIcon in PivotViewGroupingBarSettings class to false.
Icon to change the aggregation type can be hidden only in Grouping Bar but not in Field List at the moment.
@using Syncfusion.Blazor.PivotView
<SfPivotView TValue="ProductDetails" ShowGroupingBar="true">
<PivotViewDataSourceSettings DataSource="@data">
<PivotViewColumns>
<PivotViewColumn Name="Year"></PivotViewColumn>
<PivotViewColumn Name="Quarter"></PivotViewColumn>
</PivotViewColumns>
<PivotViewRows>
<PivotViewRow Name="Country"></PivotViewRow>
<PivotViewRow Name="Products"></PivotViewRow>
</PivotViewRows>
<PivotViewValues>
<PivotViewValue Name="Sold" Caption="Unit Sold"></PivotViewValue>
<PivotViewValue Name="Amount" Caption="Sold Amount"></PivotViewValue>
</PivotViewValues>
<PivotViewFormatSettings>
<PivotViewFormatSetting Name="Amount" Format="C"></PivotViewFormatSetting>
</PivotViewFormatSettings>
</PivotViewDataSourceSettings>
<PivotViewGroupingBarSettings ShowValueTypeIcon="false"></PivotViewGroupingBarSettings>
</SfPivotView>
@code{
public List<ProductDetails> data { get; set; }
protected override void OnInitialized()
{
this.data = ProductDetails.GetDefaultData().ToList();
//Bind the data source collection here. Refer "Assigning sample data to the pivot table" section in getting started for more details.
}
}
You can refer to the Blazor Pivot Table feature tour page for its groundbreaking feature representations. You can also explore the Blazor Pivot Table example to know how to render and configure the pivot table.