SpreadJS allows you to use group and collapse operations in a pivot table which makes it easier to get a clear view and display only the required data. This is particularly helpful when the pivot table contains lots of data which can be summarized further. You can group the data by date, text and numbers as explained in the below section.
You can group data by date based on seconds, minutes, hours, days, months, quarters or years and even set the starting and ending timelines. The dateGroups method can be used to achieve the same.
The following image shows the data grouped quarterly for 2013 and 2014 years.
The following sample code show how to group date data by quarters.
Javascript |
Copy Code
|
---|---|
// group by dates let groupInfo = { originFieldName: "OrderDate", dateGroups: [{ by: GC.Pivot.DateGroupType.quarters }, { by: GC.Pivot.DateGroupType.years }] }; myPivotTable.group(groupInfo); myPivotTable.add("OrderDate", "Qtr", GC.Spread.Pivot.PivotTableFieldType.columnField); myPivotTable.add("Years", "Years", GC.Spread.Pivot.PivotTableFieldType.columnField); |
In a pivot table, you can also group the Row label or Column Label items by using the textGroup method. It uses the add method to add a new group to the pivot table. When a text group is added, a new field is also created.
The following image shows the new group created by row label items.
The following example code shows how to group text of row label items.
Javascript |
Copy Code
|
---|---|
// group by items myPivotTable.views.apply("empty_pt"); myPivotTable.add("Region", "Region", GC.Spread.Pivot.PivotTableFieldType.rowField); myPivotTable.add("Quantity", "Sum of quantity", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum); let groupInfo = { originFieldName: "Product", textGroup: { fieldName: "NewProduct", groupItems: { Drinks: ['Coffee', 'Juice'], Chocolate: ['Dark', 'White', 'Milk'], Others: ['Breads', 'Cheese puffs', 'Cookie'] } } }; myPivotTable.group(groupInfo); myPivotTable.add("NewProduct", "TestProduct", GC.Spread.Pivot.PivotTableFieldType.columnField); |
You can group the numerical values instead of listing them individually in a Row field while creating frequency distribution tables. This helps in analyzing numerical values by grouping them into ranges. You can use the numberGroup method to group numerical values. The following image shows the frequency distribution of quantity within the group.
The following example code shows how to group numbers.
Javascript |
Copy Code
|
---|---|
// group by numbers myPivotTable.views.apply("empty_pt"); myPivotTable.add("Quantity", "Quantity", GC.Spread.Pivot.PivotTableFieldType.rowField); myPivotTable.add("Region", "Region", GC.Spread.Pivot.PivotTableFieldType.colField); myPivotTable.add("Product", "Count of quantity", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.count); let groupInfo = { originFieldName: "Quantity", numberGroup: { by: 30 } }; myPivotTable.group(groupInfo); |
You can expand or collapse specific groups or entire pivot field in a pivot table by using collapse option. The following image shows the East region, Bakery and Chocolates category fields as collapsed.
The following example code shows how to collapse and expand groups.
Javascript |
Copy Code
|
---|---|
// collapse some groups myPivotTable.collapse("Region", "East", true); myPivotTable.collapse("Category", "Bakery", true); myPivotTable.collapse("Category", "Chocolates", true); // expand region group myPivotTable.collapse("Region", "East", false); |
You can show or hide the expand and collapse buttons (+ or - sign buttons on the left of pivot item heading) in a pivot table using the showDrill option.
The following sample code shows how to hide expand and collapse buttons in the pivot table.
Javascript |
Copy Code
|
---|---|
myPivotTable.options.showDrill = false;
|