config
Description
Optional. Defines the structure of the Pivot table and how data is aggregated
Usage
config?: {
rows?: string | {field: string, method?: string}[],
columns?: string | {field: string, method?: string}[],
values?: string | {field: string, method?: string}[],
filters?: {}
};
Parameters
The config
parameters are used to define which fields will be applied as rows and columns, and what additional data aggregation methods should be applied to rows/columns.
rows
- (optional) defines the rows of the Pivot table. The default value is an empty array. It can be a string which represents a single field ID or an object with the field ID and a method for data extraction; the object parameters are the following:field
- (required) the ID of a fieldmethod
- (optional) defines a method for data aggregation in the field; methods for the time-based data fields are available by default: year, month, day, hour, minute which group data by year/month/day/hour; here you can also add the name of a custom method (seepredicates
) for the field of any data type
columns
- (optional) defines columns for the Pivot table. It's an empty array by default. It can be a single field ID or an object with the field ID and a method for data extraction; the object parameters are the following:field
- (required) the ID of a fieldmethod
- (optional) defines a method for data processing (for time-based data fields). By default, methods are available for the time-based fields (the date type) with the next values: "year", "quarter", "month", "week", "day", "hour", "minute". Here you can also add the name of a custom method (seepredicates
) for the field of any data type
values
- (optional) defines the data aggregation for the cells of the Pivot table. It's an empty array by default. Each element can be a string representing a data field ID and aggregation method or an object containing the field ID and the method for data aggregation. The object parameters are the following:field
- (required) the ID of a fieldmethod
- (required) defines a method for data extraction; for methods types and their description refer to Applying methods
Options for defining values
You can define values
in either of the two equally valid ways:
- option one is a string representing the field ID
- option two is an object containing the field ID and the method for data aggregation
Example
values: [
"sum(sales)", // option one
{ id: "sales", method: "sum" }, // option two
]
filters
- (optional) defines how data is filtered in the table; it's an object with field IDs and a filtering rule. The default value is an empty object. The object parameters are the following:field
- (optional) the filter key which is the ID of a field or an array of IDs with the filtering criteria:equal
- (optional) accepts numbers, string and Date valuesnotEqual
- (optional) accepts numbers, string and Date valuesgreater
- (optional) accepts numbers and Date valuesgreaterOrEqual
- (optional) accepts numbers and Date valuesless
- (optional) accepts numbers and Date valueslessOrEqual
- accepts numbers and Date valuesbetween
- an object with the next parameters:start
- Dateend
- Date
notBetween
- an object with the next parameters:start
- Dateend
- Date
contains
- accepts string values and numbersnotContains
- accepts string values and numbersbeginsWith
- accepts string values and numbersnotBeginsWith
- accepts string values and numbersendsWith
- accepts string values and numbersnotEndsWith
- accepts string values and numbersincludes
- (optional) an array of values to be displayed from those that are already filtered; available for text and dates values
info
When config is processed by Pivot, its properties receive extra data and if you try to return the config state via the api.getState()
method, the full object will look like this:
interface IParsedField {
id: string,
field: string,
method: string | null,
area: 'rows'|'columns'|'values',
base?: string,
label: string,
type: 'number'|'date'|'text'
}
interface IParsedConfig {
rows: IParsedField[],
columns: IParsedField[],
values: IParsedField[],
filters: {
[field: string]: number | string | [] |
{ [operation: string]: number | string | [] | { start:Date, end: Date} }
}
}
Parameters:
id
- a unique id of the processed fieldfield
- a field namemethod
- the operation name used for aggregation. A method is optional in case of rows and columns, and if provided, acts as a predicate and defines the way field data is pre-processed before aggregation. For values, method is a mandatory parameter.area
- the area to which the field is addedbase
- used in columns and rows for fields with a predicate. Defines original field name , while field name is formed according to the "field_by_predicate" patternlabel
- text labeltype
- data type
Example
const table = new pivot.Pivot("#root", {
fields,
data: dataset,
config: {
rows: ["studio", "genre"],
values: [
{
field: "title",
method: "count"
},
{
field: "score",
method: "max"
}
],
filters: {
genre: {
contains: "D",
includes: ["Drama"]
},
title: {
// filter for another field ("title")
contains: "A"
}
}
}
});