groupX
groupX is a variant of the group function family, specifically designed for grouping discrete x channels and aggregating channels according to specified Reducers. It is equivalent to group with channels = ['x']. The usage and configuration options are the same as the group function. Below, we explain the use cases and configuration options specific to the groupX function.
Options
| Property | Description | Type | Default |
|---|---|---|---|
| [channel] | Aggregation method for channel data output to specific mark | Reducer |
For detailed information about Reducer, please refer to the configuration options of the group function.
Examples
Below, we demonstrate the age distribution of different population groups. We use the groupX function to group the data and aggregate the x channel, calculating the line length and distribution of population for each state. Note that the corresponding mark has transform methods available for data transformation.
import * as G2 from '@antv/g2';
const { Chart } = G2;
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'view',
coordinate: { transform: [{ type: 'transpose' }] },
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/b6f2ff26-b232-447d-a613-0df5e30104a0.csv',
},
children: [
{
type: 'link',
transform: [{ type: 'groupX', y: 'min', y1: 'max' }],
encode: { x: 'state', y: 'population' },
style: { stroke: '#000' },
scale: { y: { formatter: '.0%' } },
},
{
type: 'point',
encode: { x: 'state', y: 'population', shape: 'point', color: 'age' },
scale: { color: { palette: 'spectral' } },
},
],
});
chart.render();
Explanation:
- In this example, we first define a set of sales data
data, including region, age, and population; - In the above code, the
transformmethod uses agroupXtype data transformation, grouping data by thexchannel, with ‘y’ and ‘y1’ taking the minimum and maximum values respectively; - After grouping, the data is aggregated according to the values of the
xchannel, calculating the line length ofpopulationfor eachstate; - Finally, the
encodemethod maps the grouped data to the chart’sxandyaxis for point rendering.