Loading...
The ordinal scale is a categorical scale that maps discrete domains to discrete ranges. Unlike continuous scales, ordinal scales handle discrete, categorical data such as product categories, classes, gender, etc.
The core characteristics of ordinal scales are:
In G2, ordinal scales are most commonly used to map categorical data to visual attributes such as colors and shapes. When no scale type is explicitly declared, G2 applies ordinal scales by default for categorical data.
The working principle of ordinal scales is:
For example, with domain ['A', 'B', 'C'] and range ['red', 'green', 'blue']:
Ordinal scales are suitable for the following scenarios:
Here's a basic usage example that maps categorical data to the color channel:
chart.interval().encode('x', 'category').encode('y', 'value').encode('color', 'category').scale('color', {type: 'ordinal',range: ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#c564be'],});
In this example, we map the 'category' field to the color channel and use an ordinal scale to specify a set of custom colors.
The ordinal scale provides the following configuration options:
Parameter | Description | Type | Default Value | Required |
---|---|---|---|---|
domain | Set the domain range of the data | any[] | [] | |
range | Set the range of data mapping values | any[] | [] | |
unknown | Return value for undefined , NaN , null empty values | any | undefined | |
compare | Comparator for comparing two values, used for sorting | (a: number | string, b: number | string) => number | undefined |
The domain
parameter defines the input domain of the scale, i.e., the set of possible values in the original data. For ordinal scales, the domain is usually an array of strings representing all possible categorical values.
If domain is not set, G2 will automatically infer it from the data. However, in some cases, explicitly setting the domain can:
chart.scale('color', {type: 'ordinal',domain: ['Category A', 'Category B', 'Category C'], // Explicitly specify categories and their order});
The range
parameter defines the output domain of the scale, i.e., the set of mapped values. For ordinal scales, the range can be an array of any type, most commonly an array of colors.
chart.scale('color', {type: 'ordinal',range: ['#1f77b4', '#ff7f0e', '#2ca02c'], // Custom colors});
If range is not set, G2 will use default values based on the channel type:
The compare
parameter is a comparison function used to sort values in the domain. This is very useful for controlling the display order of categorical data.
chart.scale('color', {type: 'ordinal',// Sort alphabeticallycompare: (a, b) => a.localeCompare(b),});
If no compare function is provided, values in the domain will maintain their original order.
Below is a basic example using ordinal scales to map categorical data to colors:
import { Chart } from '@antv/g2';const chart = new Chart({container: 'container',autoFit: true,});chart.options({type: 'interval',data: [{ genre: 'Sports', sold: 275 },{ genre: 'Strategy', sold: 115 },{ genre: 'Action', sold: 120 },{ genre: 'Shooter', sold: 350 },{ genre: 'Other', sold: 150 },],encode: {x: 'genre',y: 'sold',color: 'genre', // Map genre to color channel},scale: {color: {type: 'ordinal',// Custom color rangerange: ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#c564be'],},},});chart.render();
In this example, we use an ordinal scale to map game genres to different colors. Each category corresponds to a color in the range array.
The following example shows basic categorical data visualization:
import { Chart } from '@antv/g2';// Create chart instanceconst chart = new Chart({container: 'container',autoFit: true,});// Prepare data - intentionally using unordered categoriesconst data = [{ category: 'C', value: 20 },{ category: 'A', value: 40 },{ category: 'B', value: 30 },{ category: 'E', value: 10 },{ category: 'D', value: 25 },];// Configure chartchart.options({type: 'interval',data,encode: {x: 'category',y: 'value',color: 'category',},});// Render chartchart.render();
In this example, we can see that G2 uses ordinal scales by default to handle categorical data. By default, categories are displayed in their original order from the data (C, A, B, E, D).
If you need custom sorting, you can add the following configuration:
scale: {x: {type: 'ordinal',compare: (a, b) => a.localeCompare(b), // Sort alphabetically},color: {type: 'ordinal',compare: (a, b) => a.localeCompare(b), // Keep color mapping consistent},}
This ensures categories are arranged alphabetically (A, B, C, D, E) rather than in the original order from the data.
Here's a complete example using G2 declarative syntax (G2Spec) to configure ordinal scales:
import { Chart } from '@antv/g2';const spec = {type: 'interval',data: [{ category: 'A', value: 40 },{ category: 'B', value: 30 },{ category: 'C', value: 20 },{ category: 'D', value: 10 },{ category: 'E', value: 25 },],scale: {color: {type: 'ordinal',domain: ['A', 'B', 'C', 'D', 'E'], // Explicitly specify category orderrange: ['#5B8FF9', '#5AD8A6', '#5D7092', '#F6BD16', '#E8684A'], // Custom colors},},encode: {x: 'category',y: 'value',color: 'category',},};// Render using Chartconst chart = new Chart({container: 'container',autoFit: true,});chart.options(spec);chart.render();
This example demonstrates how to create a bar chart using ordinal scales with G2 declarative syntax, including the following features:
When using ordinal scales, pay attention to the following:
Number of categories vs. number of colors: If the number of categories exceeds the number of colors provided in the range, G2 will cycle through these colors. For optimal visual effect, it's recommended that the range length be at least equal to the number of different categories.
Color selection: Choose colors with good contrast to ensure different categories can be clearly distinguished. For large numbers of categories, consider using professional color schemes like ColorBrewer.
Coordination with other scales: In the same chart, ordinal scales are usually used together with band or point scales, where the former is used for color encoding and the latter for position encoding.
Sorting considerations: Choose appropriate sorting methods based on the semantics of the data. Sometimes sorting by numeric values is more meaningful than sorting by category names.