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.
How to Register Legend Plugin
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:
constF2=require('@antv/f2/lib/core');constLegend=require('@antv/f2/lib/plugin/legend');// Method 1: Global RegisterationF2.Chart.plugins.register(Legend); // Or Method2: Registeration for a Chart instanceconstchart=newF2.Chart({ id:'canvas', plugins: Legend});
Legend Configuration
Close the Legend
chart.legend(false)
Just close all legends. Sometimes there will be several legends.
chart.legend(field, false)
field: String type, the map field of the legend.
Close the legend corresponding to the field.
Configure Legend
chart.legend(field, config)
field: String, the map field of the legend.
config: Object type, configuration for the axis. The properties included are as follows:
Legend align demos
When position is set to be 'top' or 'bottom', you can set align property:
Legend verticalAlign demos
When position is set to be 'left' or 'right', you can set verticalAlign property:
itemFormatter callback
Use this property to complete the formatted display of the legend item text. See detailed demo.
chart.legend({ position:'right',itemFormatter(val) {return val +' %'; }});
Custom maker
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 marker radius: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(); }});
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 }});
Legend Item Interface
The Legend item implement the following interface,the image below will help you better understand:
{// Label that will be displayed as name name: String,// Label that will be displayed as value value: String / Number,// For marker, see Custom maker marker: String / Object / Function,// For setting the legend click style, if not set, the default is true checked: 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:
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.
The display position of the legend, can be set to 'top', 'right', 'bottom', 'left', defaults to 'top'.
align
String
'left'
It is used to set the alignment of the legend in the horizontal direction, when position is set to be 'top' or 'bottom'.The values that can be set are: 'left', 'center', 'right', and the default is 'left'. See demos.
verticalAlign
String
'middle'
It is used to set the alignment of the legend in the vertical direction, when position is set to 'left' and 'right'. The values that can be set are: 'top', 'middle', 'bottom', and the default is 'middle'. See demos.
itemWidth
Number | 'auto'
'auto'
It is used to set width of the legend item, defaults to 'auto', using the default layout of F2 to calculate the width. If the itemWidth is set to null, it will be calculated based on the width of the legend itself, developers are also allowed to set the value of the itemWidth, such as itemWidth: 50
showTitle
Boolean
false
wether show the title of the legend, default to false.
It is used to set the text style of the legend, see Canvas for more details.Demo.
joinString
String
':'
Set the connection character for name and value in the legend item. The default is ':'.
triggerOn
String
'touchstart'
The trigger event of the legend filter behavior, the default is 'touchstart'.
selectedMode
String
'multiple'
Set the selected mode of the legend, provide two modes: 'multiple' and 'single'
clickable
Boolean
true
wether the legend is clickable, defaults to true.
onClick
Function
null
callback for clicking the legend, invalid when clickable is false. see here for detail.
custom
Boolean
false
when custom is true, users are allowed to customize legends, including the specific legends and corresponding interactions. The default value is false. See here for detail.