A pivot table puts together raw data from various data sources that are normally flat, resulting in many individual and detailed records that are unorganized. When raw data lacks structure, it isn’t easy to understand for any user.
This is where the Syncfusion JavaScript Pivot Table’s data aggregation feature comes in. This feature converts raw data into summarized and organized data using a pivot engine. The data is then bound to a pivot table UI for viewing the well-structured and understandable result.
Let’s explore this powerful and user-friendly feature with code examples.
The most popular aggregation types supported by the JavaScript Pivot Table are:
Other types of aggregations are:
Let’s look at a real-time scenario in a supermarket. Many sales occur there throughout the day. Every sale will be treated as a separate record added to the database, which is our raw data. It would be difficult to analyze product sales using raw data alone, and this raw data must be converted to a structured, organized format.
For instance, take the product Polo T-Shirt. If an analyst wants to know the minimum or maximum number of polo t-shirt purchases per month, quarter, or year, they can use the Min and Max aggregates. If the analyst wants to know the average sales of t-shirts per month, quarter, or year, they can use the Avg aggregate.
Before diving deep into data aggregation in our JavaScript Pivot Table, remember the following points:
The aggregation type can be set to a field in the Pivot Table via:
To do the data aggregation via UI, use the dropdown arranged inside the Values section.
A list of basic aggregation types will appear, which can be selected from and changed at runtime.
Click the More option at the bottom of the context menu. It opens a dialogue with advanced aggregation settings to handle a field based on your requirements.
So, these are the primary steps for dynamically changing an aggregation type for any field at runtime via UI.
To do the data aggregation via code-behind, in the Values section, first define the field with its name and caption.
The type property then accepts the assigned aggregate type and processes it in the pivot engine before providing the result data to render the pivot table UI.
Refer to the following code example. In it, we have used the aggregation type Avg.
import { PivotView, IDataSet } from '@syncfusion/ej2-pivotview'; import { pivotData } from './datasource.ts'; let pivotTableObj: PivotView = new PivotView({ dataSourceSettings: { dataSource: pivotData as IDataSet[], columns: [{ name: 'EnerType', caption: 'Energy Type' }, { name: 'EneSource', caption: 'Energy Source' }], values: [{ name: 'PowUnits', caption: 'Units (GWh)', type: 'Avg' }, { name: 'ProCost', caption: 'Cost (MM)' }], rows: [{ name: 'Year', caption: 'Production Year' }, { name: 'Quarter', caption: ' Quarter Year' }], filters: [] } }); pivotTableObj.appendTo('#PivotTable');
Refer to the following images.
The JavaScript Pivot Table’s data aggregation feature comes with more options:
For more details, refer to the Aggregation in JavaScript Pivot Table demos and documentation.
Thanks for reading! This blog explored the data aggregation feature in the Syncfusion JavaScript Pivot Table. With this feature, we can convert raw data into summarized and organized data to get clear insights from it.
We strongly urge you to explore our Syncfusion Essential JS 2 suite, which boasts over 80 user-friendly JavaScript UI controls. Your feedback is essential to us, and we encourage you to share your thoughts in the comments section below.
Existing customers can access the newest version of Essential Studio® from the License and Downloads page. If you’re not a customer yet, we offer a 30-day free trial to let you experience the full range of available features.
If you have questions, you can contact us through our support forum, support portal, or feedback portal. Our team is always eager to help you.