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 tooltip by default. But if you want a better package size optimization, you can register manually:
constF2=require('@antv/f2/lib/core');constTooltip=require('@antv/f2/lib/plugin/tooltip');// Method 1: Global RegisterationF2.Chart.plugins.register(Tooltip); // Or Method2: Registeration for a Chart instanceconstchart=newChart({ id:'canvas', plugins: Tooltip});
Tooltip Configuration
Close Tooltip
If you call chart.tooltip(false), tooltip will not be rendered.
Configure Tooltip
Below is the method for configuring tooltip, the detail properties can be found in table.
chart.tooltip({// see detail properties in table});
Name
Type
Default
Description
alwaysShow
Boolean
false
Whether the tooltip is still show when the touch area is not in the chart.It can be set to be true to preserve displaying.
offsetX
Number
0
Tooltip offset in x direction.
offsetY
Number
0
Tooltip offset in y direction.
triggerOn
String / Array
[ 'touchstart', 'touchmove' ]
The event that will trigger tooltip.
triggerOff
String / Array
'touchend'
The event that will hide tooltip.
showTitle
Boolean
false
Wether to show the title.
showCrosshairs
Boolean
Wether to show the tooltip crosshairs. The default value of different charts is different, point, line, area and path will show by default.
crosshairsStyle
Object
Style of the crosshairs line, default setting is:
{
stroke: 'rgba(0, 0, 0, 0.25)',
lineWidth: 2
}
showTooltipMarker
Boolean
true
Wether to display tooltipMarker.
tooltipMarkerStyle
Object
Style of the tooltipMarker.
background
Object
The background style of tooltip content box.
titleStyle
Object
Style for tooltip's title, invalid if showTitle is false.
Callback when the tooltip content change. See callback.
Tooltip Item Interface
The picture below shows the structure of tooltip content, it includes title and a group of items, each item consists of marker, name and value.
The tooltip items passed to the tooltip onShow , onChange, onHide callbacks implement the following interface:
{// X position of matching tooltip item x: Number,// Y position of matching tooltip item y: Number,// Color of matching tooltip item color: String,// The data of matching tooltip item origin: Object,// The value of the `name` attribute in tooltip item name: String,// The value of the `value` attribute in tooltip item value: String / Number,// The title of current tooltip title: String// The marker of matching tooltip item marker: Object}
onShow callback
We can use onShow callback to change the tooltip's show content. It will trigger when the tooltip is shown.
chart.tooltip({onShow(obj) {}});// The following is the parameter `Obj` interface{// X position of current event x: Number,// Y position of current event y: Number,// The tootlip instance tooltip: Object// Tooltip content items items: Array,// The style of tooltipMarker tooltipMarkerCfg: Object}
As following example, we will change the value of name to the value of title, see demo and code here.
It will trigger when the tooltip's content changed. Usually combined with the custom attribute for custom tooltip.
chart.tooltip({onChange(obj) {}});// The following is the parameter `Obj` interface{// X position of current event x: Number,// Y position of current event y: Number,// The tootlip instance tooltip: Object// Tooltip content items items: Array,// The style of tooltipMarker tooltipMarkerCfg: Object}
onHide callback
It will trigger when the tooltip is disappeared.
chart.tooltip({onHide(obj) {}});// The following is the parameter `Obj` interface{// The tootlip instance tooltip: Object}
Custom Tooltip
Custom tooltips allow you to hook into the tooltip rendering process so that you can render the tooltip in your own custom way. Generally this is used to create an HTML tooltip instead of an canvas one or combined with legend.
Combine Legend and Tooltip
On mobile, in order to make better use of the screen size, the tooltip and legend are often combined in the chart design. Like this:
In F2, we can use a custom tooltip to implement, see demo and code here:
chart.tooltip({ custom:true,// requiredonChange(obj) {var legend =chart.get('legendController').legends.top[0]; // get the top legend instancevar tooltipItems =obj.items;var legendItems =legend.items;var map = {};legendItems.map(function(item) { map[item.name] =_.clone(item); });tooltipItems.map(function(item) {var name =item.name;var value =item.value;if (map[name]) { map[name].value = value; } });legend.setItems(_.values(map)); // update legend items },onHide:functiononHide() {var legend =chart.get('legendController').legends.top[0];legend.setItems(chart.getLegendItems().country); // revert the original items of the legend } });
External(HTML) Tooltip
We also can define a html tooltip, like this, see demo and code here:
chart.tooltip({ custom:true, showCrosshairs:false,onChange:functiononChange(ev) {var tooltipEl =$('.f2-tooltip'); // get the tooltip html element var currentData =ev.items[0];var text =currentData.value;tooltipEl.html(['<span>'+currentData.origin.date +'</span>','<span>'+'Web Visits: <b>'+ text +'</b></span>'].join(''));tooltipEl.css({ opacity:1, left: canvasOffsetLeft +currentData.x -tooltipEl.outerWidth() /2+'px', top: canvasOffsetTop +currentData.y -tooltipEl.outerHeight() -15+'px' }); },onHide:functiononHide() {var tooltipEl =$('.f2-tooltip');tooltipEl.css({ opacity:0 }); }});
Or Like this, combine with Guide.Html and Tooltip, see demo and code here: