AntV
Search…
Scale
Scales are a convenient abstraction for a fundamental task in visualization: mapping a dimension of abstract data to a visual representation. Different data types have different type of scales.
    For continuous quantitative data, you typically want a linear scale.
    For time series data, a timeCat scale
    For discrete ordinal (ordered) or categorical (unordered) data, a cat scale specifies an explicit mapping from a set of data values to a corresponding set of visual attributes (such as colors, shapes).
    A identity scale is for constant.
Scales have no intrinsic visual representation. However, most scales can generate and format ticks for reference marks to aid in the construction of axes.
F2 will generate scales for the data automatically. But still offers two methods to configure.Here is an example:
1
const data = [
2
{ State: 'WY', Age: 'Under 5 Years', Population: 25635 },
3
{ State: 'WY', Age: '5 to 13 Years', Population: 1890 },
4
{ State: 'WY', Age: '14 to 17 Years', Population: 9314 },
5
{ State: 'DC', Age: 'Under 5 Years', Population: 30352 },
6
{ State: 'DC', Age: '5 to 13 Years', Population: 20439 },
7
{ State: 'DC', Age: '14 to 17 Years', Population: 10225 },
8
{ State: 'VT', Age: 'Under 5 Years', Population: 38253 },
9
{ State: 'VT', Age: '5 to 13 Years', Population: 42538 },
10
{ State: 'VT', Age: '14 to 17 Years', Population: 15757 },
11
{ State: 'ND', Age: 'Under 5 Years', Population: 51896 },
12
{ State: 'ND', Age: '5 to 13 Years', Population: 67358 },
13
{ State: 'ND', Age: '14 to 17 Years', Population: 18794 },
14
{ State: 'AK', Age: 'Under 5 Years', Population: 72083 },
15
{ State: 'AK', Age: '5 to 13 Years', Population: 85640 },
16
{ State: 'AK', Age: '14 to 17 Years', Population: 22153 }
17
];
Copied!
In the data, 'State' is a cat scale, 'Age' is a cat scale and 'Population' is a linear scale. Now we can set the maximum and minimum values ​​of Population
1
chart.source(data, {
2
Population: {
3
min: 1000,
4
max: 100000
5
}
6
});
Copied!
Or
1
chart.scale('Population', {
2
min: 1000,
3
max: 100000
4
});
Copied!

Common Configuration

1
chart.scale('fieldName', {
2
// configurations
3
})
Copied!
The common configuration properties of scale are described below:
Name
Type
Default
Description
type
String
null
Specify the type of the scale, supported scale types are: identify, linear, cat, timeCat.
formatter
Function
null
It is used to format the text of the scale point in the axis and will affect the display of the data on axis, legend, and tooltip.
range
Array
[ 0, 1 ]
Used to control the drawing range of data on the coordinate, in the format of [ min, max ], and both min and max are data in the range 0 to 1.
alias
String
null
Alias of the data field, if it is set, we will display the alias value in the chart.
tickCount
Number
Number of tick points in the axis, different scale types have different default values.
ticks
Array
null
Used to specify the scale text on the axis. When the user sets ticks, it will display according to ticks.
values
Array
null
Set the data collection of the scale, the default will automatically read from the data source, see Cat scale for detail usage.
1
chart.source(data, {
2
value: {
3
min: 0,
4
ticks: [ 0, 500, 1000, 1300, 1600 ]
5
}
6
});
Copied!
Except for the above general configuration properties, different scale types have different configuration items.

Linear

Name
Type
Default
Description
nice
Boolean
true
used to optimize the range of values so that ticks on axes are evenly distributed. For example, the range [ 3, 97 ] will be optimized to [ 0, 100 ], if nice is set to be true.
min
Number
the minimum value of the data set
max
Number
the maximum value of the data set
tickInterval
Number
Used to specify the distance between the scale points of the axis, which is the difference between the original data. Note thattickCountandtickInterval cannot both be defined.
tickInterval explanation
1
const data = [
2
{ year: '1951 年', sales: 38 },
3
{ year: '1952 年', sales: 52 },
4
{ year: '1956 年', sales: 61 },
5
{ year: '1957 年', sales: 145 },
6
{ year: '1958 年', sales: 48 },
7
{ year: '1959 年', sales: 38 },
8
{ year: '1960 年', sales: 38 },
9
{ year: '1962 年', sales: 38 },
10
];
11
const chart = new F2.Chart({
12
id: 'mountNode',
13
pixelRatio: window.devicePixelRatio
14
});
15
16
chart.source(data, {
17
sales: {
18
tickInterval: 80 // set tickInterval
19
}
20
});
21
chart.interval().position('year*sales');
22
chart.render();
Copied!

Cat

Cat scale has no other configuration items. But we will introduce the usage of values ​​here.
1. When you need to specify the display order of the categorical data.
We use the following data to draw a bar chart:
1
const data = [
2
{ name: 'A', value: 38 },
3
{ name: 'B', value: 52 },
4
{ name: 'C', value: 61 },
5
];
6
const chart = new F2.Chart({
7
id: 'mountNode',
8
pixelRatio: window.devicePixelRatio
9
});
10
11
chart.source(data);
12
chart.interval().position('name*value').color('name');
13
chart.render();
Copied!
Now we can change the display order of the bar by setting values, including legend:
1
chart.source(data, {
2
name: {
3
type: 'cat', // In fact, you can also do not need to set, F2 will automatically recognize
4
values: [ 'C', 'B', 'A' ]
5
}
6
});
Copied!
After set this, the bar chart will be rendered as follow:
2. Convert index values ​​to corresponding data. But the original value of the field must be indexed value, start from 0.
1
const data = [
2
{ month: 0, tem: 7, city: 'Tokyo' },
3
{ month: 1, tem: 6.9, city: 'Tokyo' },
4
{ month: 2, tem: 9.5, city: 'Tokyo' },
5
{ month: 3, tem: 14.5, city: 'Tokyo' },
6
{ month: 4, tem: 18.2, city: 'Tokyo' },
7
{ month: 5, tem: 21.5, city: 'Tokyo' },
8
{ month: 6, tem: 25.2, city: 'Tokyo' }
9
];
10
const chart = new F2.Chart({
11
id: 'mountNode',
12
width: 375,
13
height: 260,
14
pixelRatio: window.devicePixelRatio
15
});
16
chart.source(data, {
17
month: {
18
type: 'cat',
19
values: ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'Jun.', 'Jul.']
20
}
21
});
22
chart.interval().position('month*tem');
23
chart.render();
Copied!
3. Control the display of data.
Case 1: we just have 3 items in data, but we can set 10 ticks on the axis:
1
const data = [
2
{ name: 'A', value: 38 },
3
{ name: 'B', value: 52 },
4
{ name: 'C', value: 61 },
5
];
6
const chart = new F2.Chart({
7
id: 'mountNode',
8
pixelRatio: window.devicePixelRatio
9
});
10
11
chart.source(data, {
12
name: {
13
values: [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K' ]
14
}
15
});
16
chart.interval().position('name*value');
17
chart.render();
Copied!
Case 2: we just have 3 items in data, but we can just want to display one:
1
const data = [
2
{ name: 'A', value: 38 },
3
{ name: 'B', value: 52 },
4
{ name: 'C', value: 61 },
5
];
6
const chart = new F2.Chart({
7
id: 'mountNode',
8
pixelRatio: window.devicePixelRatio
9
});
10
11
chart.source(data, {
12
name: {
13
values: [ 'B' ]
14
}
15
});
16
chart.interval().position('name*value');
17
chart.render();
Copied!

TimeCat

For time series data, we will sort the data set by default.
Name
Type
Default
Description
mask
String
'YYYY-MM-DD'
To Format the display of date, we use fecha lib, the format tokens can see here
NOTE: mask and formmater cannot both be specified, if both are defined, the formatter attribute will be used in priority.
Last modified 3yr ago