AntV
Search…
Axis
The picture below reveals the composition of our axes:

The Axis Configuration

Close the axis

chart.axis(false)

Close all axes.

chart.axis(field, false)

    field: String type, the data field name corresponding to the axis
Do not render the axis corresponding to the field.

Configure Axis

chart.axis(field, config)

    field: String type, the data field name corresponding to the axis
    config: Object type, configuration for the axis. It can be used to configure each component of the axis: include label, line, tick, tickLine and grid.The properties included are as follows:
Name
Type
Default
Description
line
Object / null
{ stroke: '#e8e8e8', lineWidth: 1 }
The axis line's configuration. The axis line is not displayed when line set to null. It supports all html5 canvas's line attributes, see canvas for more details.
labelOffset
Number
7.5
The distance between the axis label and axis line
grid
Object/Function/null
{ type: 'line', stroke: '#e8e8e8', lineWidth: 1, lineDash: [ 2 ] }
The axis grid's configuration. The grid is not displayed when grid is set to null. It supports all html5 canvas's line attributes, see canvas for more details.
If gird is a function type, see callback
tickLine
Object/null
null
The configuration for style of the tickLine in axis. The tickLine of axis is not displayed when tickLine is set to null. It supports all html5 canvas's line attributes, see canvas for more details.
label
Object/Function/null
{ fill: '#808080', fontSize: 10 }
Configuration for label of the axis. The labek of the axis is not displayed when label is set to null. It supports all html5 canvas's attributes, see canvas for more details.
If label is a function type, see callback
position
String
'left'
Configuration for the position of the Y-axis. X-axis defaults to 'bottom', the position of y-axis can be set to 'left' or 'right'.
top
Boolean
false
Used to adjust the layer level. true represents the top layer level, false means the bottom layer level.
line, grid, label, tickLine all have a property top which is used to adjust the display layer, true represents the top layer, false means the bottom layer level.
1
chart.axis('xFieldName', {
2
line: {
3
top: true
4
}
5
});
Copied!
Note: When grid and label are callback functions, the return values must be an object.
1
chart.axis('your data field name', {
2
line: {
3
lineWidth: 1,
4
stroke: '#ccc'
5
},
6
labelOffset: 20,
7
tickLine: {
8
lineWidth: 1,
9
stroke: '#ccc',
10
length: 5
11
},
12
// highlight for grid line at 0%
13
grid: (text, index, total) => {
14
if(text === '0%') {
15
return {
16
stroke: '#efefef'
17
};
18
}
19
return {
20
stroke: '#f7f7f7'
21
}
22
},
23
label: (text, index, total) => {
24
const cfg = {
25
textAlign: 'center'
26
};
27
if (index === 0) {
28
cfg.textAlign = 'start';
29
}
30
if (index > 0 && index === total - 1) {
31
cfg.textAlign = 'end';
32
}
33
cfg.text = text + '%'; // cfg.text supports text formatting
34
return cfg;
35
}
36
});
Copied!

Creating Custom Label

If you want to change the axis label to include information about the data type, you can define a callback function for label. In the following example, every label of the Y axis would be formatted as a percentage, and if the value is greater than zero, the font color will be red, equal to zero is black, and less than zero is green.
If the callback returns null or undefined the label and the associated grid line will be hidden.
See detailed demo: here.
1
chart.axis('value', {
2
/**
3
* @param {String} text the text string of label
4
* @param {number} index index order
5
* @param {number} total the total numbers of label
6
* @return {Object} cfg return the configuration object
7
*/
8
label: function label(text, index, total) {
9
var number = parseInt(text);
10
var cfg = {};
11
if (number > 0) {
12
cfg.text = '+' + text;
13
cfg.fill = '#F5222D';
14
} else if (number === 0) {
15
cfg.fill = '#000';
16
cfg.fontWeight = 'bold';
17
} else {
18
cfg.fill = '#52C41A';
19
}
20
return cfg;
21
}
22
});
Copied!

Creating Custom Grid

Same as label. The following example will highlight the grid line which the value is zero.
See detailed demo: here.
1
chart.axis('value', {
2
/**
3
* @param {String} text the text string of grid
4
* @param {number} index index order
5
* @param {number} total the total numbers of grid
6
* @return {Object} cfg return the configuration object
7
*/
8
grid: function grid(text, index, total) {
9
if (text === '0') {
10
return {
11
lineDash: null,
12
lineWidth: 1
13
};
14
}
15
}
16
});
Copied!

More Axis Configuration DemosD

Demo
Last modified 3yr ago