The legend for F2 is determined by the color mapping. When the data mapped to color is a cat scale(for discrete ordinal (ordered) or categorical (unordered) data), a legend is generated.
F2 has modular structure provides best tree-shaking results and package size optimization.
If you just import F2 from '@antv/f2'
, then it has included legend by default. But if you want a better package size optimization, you can register manually:
const F2 = require('@antv/f2/lib/core');const Legend = require('@antv/f2/lib/plugin/legend');// Method 1: Global RegisterationF2.Chart.plugins.register(Legend);// Or Method2: Registeration for a Chart instanceconst chart = new F2.Chart({id: 'canvas',plugins: Legend});
Just close all legends. Sometimes there will be several legends.
field
: String type, the map field of the legend.
Close the legend corresponding to the field.
field
: String, the map field of the legend.
config
: Object type, configuration for the axis. The properties included are as follows:
Name | Type | Default | Description |
| String | 'top' | The display position of the legend, can be set to 'top', 'right', 'bottom', 'left', defaults to 'top'. |
| String | 'left' | It is used to set the alignment of the legend in the horizontal direction, when |
| String | 'middle' | It is used to set the alignment of the legend in the vertical direction, when |
| Number | 'auto' | 'auto' | It is used to set width of the legend item, defaults to |
| Boolean | false | wether show the title of the legend, default to false. |
|
|
| Configure the style of legend title. see Canvas for more details. |
| Number | 0 | The offset of the legend in x direction. The value unit is 'px' and the default value is 0. |
| Number | 0 | The offset of the legend in y direction. The value unit is 'px' and the default value is 0. |
| Number | 12 | The distance between the title and the legend items, defaults to 12(px). If the title is not displayed, it is invalid. |
| Number | 12 | The distance between legend's items in horizontal direction, defaults to 12(px). |
| Number | 12 | The distance in the vertical direction from the next legend item, defaults to 12(px). |
| Number | 6 | The distance between the marker and legend label, defaults to 6(px). |
| Object |
| Used to set the style of the marker and text in the legend item that is not selected. |
| Function | null | Callback for formatting the text in legend. See callback. |
| String/Function/Object | 'circle' | It is used to set the marker style of the legend, defaults to 'circle'. See custom marker for detail. |
| Object | see source | It is used to set the text style of the legend, see Canvas for more details. Demo. |
| Object | see source | It is used to set the text style of the legend, see Canvas for more details.Demo. |
| String | ':' | Set the connection character for name and value in the legend item. The default is ':'. |
| String | 'touchstart' | The trigger event of the legend filter behavior, the default is 'touchstart'. |
| String | 'multiple' | Set the selected mode of the legend, provide two modes: 'multiple' and 'single' |
| Boolean | true | wether the legend is clickable, defaults to |
| Function | null | callback for clicking the legend, invalid when |
| Boolean | false | when |
When position
is set to be 'top' or 'bottom', you can set align
property:
When position
is set to be 'left' or 'right', you can set verticalAlign
property:
Use this property to complete the formatted display of the legend item text. See detailed demo.
chart.legend({position: 'right',itemFormatter(val) {return val + ' %';}});
The following describes the specific use of the marker
:
When marker
is a String, F2 provides the following types:marker: 'circle'
or marker: 'square'
when marker
is an Object, you can configure the type of marker and its style.
chart.legend({marker: {symbol: 'circle', // shape of the markerradius: 5 // radius of the circle}});
When marker is a Function, you can customize the shape you need
chart.legend({/*** customize the shape of marker* @param {number} x x-axis of the marker* @param {number} y y-axis of the marker* @param {number} r radius of the marker* @param {object} ctx context object of the canvas* @return {null}*/marker(x, y, r, ctx) {}});
The following code draws a marker shown below:
chart.legend('city', {marker(x, y, r, ctx) {ctx.lineWidth = 2;ctx.strokeStyle = ctx.fillStyle;ctx.moveTo(x - r - 3, y);ctx.lineTo(x + r + 3, y);ctx.stroke();ctx.arc(x, y, r, 0, Math.PI * 2, false);ctx.fill();}});
See demo.
chart.legend({/*** callback for clicking the legend, invalid when clickable is false.* @param {object} event obejct* @return {null}*/onClick: ev => {const { clickedItem } = ev; // get the clicked legend item}});
The Legend item implement the following interface,the image below will help you better understand:
{// Label that will be displayed as namename: String,// Label that will be displayed as valuevalue: String / Number,// For marker, see Custom makermarker: String / Object / Function,// For setting the legend click style, if not set, the default is truechecked: Boolean,// Fill style of the legend marker, you also can set in marker(when it is an object)fill: String}
For example, use the custom legend feature to define legend items as follows:
chart.legend({position: 'right',custom: true,items: [{ name: 'TYPE A', value: '73.76%', marker: { symbol: 'square', fill: '#1890FF', radius: 4 } },{ name: 'TYPE B', value: '22.11%', marker: { symbol: 'square', fill: '#2FC25B', radius: 4 } },{ name: 'TYPE C', value: '2.2%', marker: { symbol: 'square', fill: '#FACC14', radius: 4 } },{ name: 'TYPE D', value: '1.93%', marker: { symbol: 'square', fill: '#F04864', radius: 4 } }],nameStyle: {fill: '#808080'}, // For styling the name labelvalueStyle: {fill: '#333',fontWeight: 'bold' // For styling the value label}});
You can use the custom legend feature to customize the display content and style of the legend, and you can also use the onClick
callback to define the interaction behavior of the legend.
Below is a demo, you can click on the legend to control the display and hide of the corresponding geometry,Complete code.
chart.legend({custom: true,items: [{ name: '个人分数', marker: 'square', fill: '#FACC14', checked: true },{ name: '平均分数',marker: function marker(x, y, r, ctx) {ctx.lineWidth = 1;ctx.strokeStyle = ctx.fillStyle;ctx.moveTo(x - r - 3, y);ctx.lineTo(x + r + 3, y);ctx.stroke();ctx.arc(x, y, r, 0, Math.PI * 2, false);ctx.fill();},fill: '#000',checked: true}],onClick(ev) {const item = ev.clickedItem;const name = item.get('name');const checked = item.get('checked');const children = item.get('children');if (checked) {const markerFill = children[0].attr('fill');const textFill = children[1].attr('fill');children[0].set('_originColor', markerFill); // 缓存 marker 原来的颜色children[1].set('_originColor', textFill); // 缓存文本原来的颜色}const geoms = chart.get('geoms');const canvas = chart.get('canvas');for (let i = 0; i < geoms.length; i++) {const geom = geoms[i];const container = geom.get('container');if (geom.getYScale().alias === name)if (!checked) {container.show();children[0].attr('fill', children[0].get('_originColor'));children[1].attr('fill', children[1].get('_originColor'));} else {container.hide();children[0].attr('fill', '#bfbfbf'); // marker 置灰children[1].attr('fill', '#bfbfbf'); // 文本置灰 置灰}item.set('checked', !checked);legendItems[findLegendItem(name)].checked = !checked;}canvas.draw();}});
Demos | |
| |
| |
| |
marker | |
| |
|