In most map setups, a polygon (country) will change color and show a tooltip with its name when hovered. This tutorial will show how you can “simulate” hover via API, without user actually interacting with the map.
Here’s what we are going to start with. The map below has already set up states to change color on hover, as well as tooltipText to display a tooltip.
Run * ---------------------------------------
* This demo was created using amCharts 4.
* For more information visit:
* https://www.amcharts.com/
* Documentation is available at:
* https://www.amcharts.com/docs/v4/
* ---------------------------------------
var chart = am4core . create ( " chartdiv " , am4maps . MapChart );
chart . geodata = am4geodata_worldLow ;
chart . projection = new am4maps . projections . Miller ();
// Create map polygon series
var polygonSeries = chart . series . push ( new am4maps . MapPolygonSeries ());
polygonSeries . exclude = [ " AQ " ];
// Make map load polygon (like country names) data from GeoJSON
polygonSeries . useGeodata = true ;
var polygonTemplate = polygonSeries . mapPolygons . template ;
polygonTemplate . tooltipText = " {name} " ;
polygonTemplate . fill = am4core . color ( " #74B266 " );
// Create hover state and set alternative fill color
var hs = polygonTemplate . states . create ( " hover " );
hs . properties . fill = am4core . color ( " #367B25 " );
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
text-decoration : underline ;
triggering map polygon tooltip via API triggering-map-polygon-tooltip-via-api
Basically, triggering hover is as easy as setting isHover = true on a target element.
That said, there are a few things we can do for it to work correctly, though.
Normally, a tooltip would show up at whatever position your mouse cursor is, or location of tap on a touch screen.
However, in case of a trigger via API, there’s no such position, so we want the tooltip to pop up in the middle of the target polygon.
For that we need to set polygon template’s tooltipPosition to "fixed".
We also want it appearing in a “visual center” of the polygon, not geometrical one, so we’ll need to enable visual center calculation for our Polygon series by setting its calculateVisualCenter to true.
polygonSeries . calculateVisualCenter = true ;
polygonSeries . mapPolygons . template . tooltipPosition = " fixed " ;
polygonSeries . calculateVisualCenter = true ;
polygonSeries . mapPolygons . template . tooltipPosition = " fixed " ;
"type" : " MapPolygonSeries " ,
"calculateVisualCenter" : true ,
"tooltipPosition" : " fixed "
Now, in order to simulate hover (by setting isHover property) we need an actual MapPolygon object.
We can find it using Serie’s getPolygonById(id) method.
Let’s find and “hover” France:
let france = polygonSeries . getPolygonById ( " FR " );
var france = polygonSeries . getPolygonById ( " FR " );
Similarly, if we’d like to “unhover” a polygon via API, we’d set its isHover = false.
It will not only remove any “hover” state properties, but will also hide tooltip.
Run * ---------------------------------------
* This demo was created using amCharts 4.
* For more information visit:
* https://www.amcharts.com/
* Documentation is available at:
* https://www.amcharts.com/docs/v4/
* ---------------------------------------
var chart = am4core . create ( " chartdiv " , am4maps . MapChart );
chart . geodata = am4geodata_worldLow ;
chart . projection = new am4maps . projections . Miller ();
// Create map polygon series
var polygonSeries = chart . series . push ( new am4maps . MapPolygonSeries ());
polygonSeries . exclude = [ " AQ " ];
// Make map load polygon (like country names) data from GeoJSON
polygonSeries . useGeodata = true ;
var polygonTemplate = polygonSeries . mapPolygons . template ;
polygonTemplate . tooltipText = " {name} " ;
polygonTemplate . fill = am4core . color ( " #74B266 " );
// Create hover state and set alternative fill color
var hs = polygonTemplate . states . create ( " hover " );
hs . properties . fill = am4core . color ( " #367B25 " );
polygonSeries . calculateVisualCenter = true ;
polygonTemplate . tooltipPosition = " fixed " ;
function selectCountry ( id ) {
selected . isHover = false ;
selected = polygonSeries . getPolygonById ( id );
polygonTemplate . events . on ( " over " , function ( ev ) {
if ( ev . target != selected ) {
selected . isHover = false ;
// This is needed so that "unhovering" previous polygon does not hide tooltip
ev . target . isHover = false ;
ev . target . isHover = true ;
< div id = " chartdiv " ></ div >
< a onclick = " selectCountry ( ' US ' ) " > United States </ a >
< a onclick = " selectCountry ( ' BR ' ) " > Brazil </ a >
< a onclick = " selectCountry ( ' FR ' ) " > France </ a >
< a onclick = " selectCountry ( ' CN ' ) " > China </ a >
< a onclick = " selectCountry ( ' AU ' ) " > Australia </ a >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
text-decoration : underline ;
triggering map polygon tooltip via API triggering-map-polygon-tooltip-via-api-2