Any element on a chart can have tooltipText set to display in form of a tooltip when hovered or touched. This tutorial will show how you can turn those tooltips into “sticky”, so they are always shown, without requiring to hover/touch the element.
The functionality was introduced in version 4.5.4, so if you happen to be using some older version, you’ll need to upgrade.
To make an element display its tooltip permanently, we use its showTooltipOn property. To make tooltip always be shown, set it to "always".
The following will create a simple circle with it’s tooltip always visible:
let circle = chart . createChild (am4core . Circle );
circle . fill = am4core . color ( " #c00 " );
circle . valign = " middle " ;
circle . tooltipText = " Hi, I'm Mr. Circle. " ;
circle . showTooltipOn = " always " ;
var circle = chart . createChild ( am4core . Circle );
circle . fill = am4core . color ( " #c00 " );
circle . valign = " middle " ;
circle . tooltipText = " Hi, I'm Mr. Circle. " ;
circle . showTooltipOn = " always " ;
"tooltipText" : " Hi, I'm Mr. Circle. " ,
"showTooltipOn" : " always "
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 " , am4core . Container );
chart . width = am4core . percent ( 100 );
chart . height = am4core . percent ( 100 );
var circle = chart . createChild ( am4core . Circle );
circle . fill = am4core . color ( " #c00 " );
circle . valign = " middle " ;
circle . tooltipText = " Hi, I'm Mr. Circle. " ;
circle . showTooltipOn = " always " ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
sticky tooltips sticky-tooltips
However, most of the time we won’t be using it on a single element, but rather on all items of, say, Series.
In this case we can set it on list template, which will propagate it all elements.
For example, let’s enable sticky tooltips on all columns of a ColumnSeries:
series . columns . template . tooltipText = " {categoryX} \n [bold]{valueY}[/] " ;
series . columns . template . showTooltipOn = " always " ;
series . columns . template . tooltipText = " {categoryX} \n [bold]{valueY}[/] " ;
series . columns . template . showTooltipOn = " always " ;
"tooltipText" : " {categoryX} \n [bold]{valueY}[/] " ,
"showTooltipOn" : " always "
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/
* ---------------------------------------
am4core . useTheme ( am4themes_animated );
var chart = am4core . create ( " chartdiv " , am4charts . XYChart );
category: " Distribution " ,
var categoryAxis = chart . xAxes . push ( new am4charts . CategoryAxis ());
categoryAxis . dataFields . category = " category " ;
categoryAxis . renderer . grid . template . location = 0 ;
//categoryAxis.renderer.minGridDistance = 30;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
var series = chart . series . push ( new am4charts . ColumnSeries ());
series . dataFields . valueY = " value " ;
series . dataFields . categoryX = " category " ;
series . tooltip . label . textAlign = " middle " ;
series . columns . template . tooltipText = " {categoryX} \n [bold]{valueY}[/] " ;
series . columns . template . showTooltipOn = " always " ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
sticky tooltips sticky-tooltips-2
Tooltip positioning is bound to a certain logic in amCharts 4.
For series, it usually is placed to the side. It also depends on vertical and horizontal position.
For sticky tooltips that might not work, and we might want to micro-manage tooltip positions and orientation.
For that we can use Tooltip property pointerOrientation.
Let’s make our tooltips always stick upwards instead of to the side:
series . tooltip . pointerOrientation = " down " ;
series . tooltip . pointerOrientation = " down " ;
"pointerOrientation" : " down "
pointerOrientation is set to 'down'
You can see that the tooltip is now pointing to the center of each column.
Let’s make them point to the top.
For that we do have a handy properties like tooltipX and tooltipY, which indicate the relative position within element where tooltip should point to horizontally and vertically.
We want to position the tooltip on the very top of the column, which means we need to set tooltipY to zero.
series . columns . template . tooltipY = 0 ;
series . columns . template . tooltipY = 0 ;
tooltipY = 0
If we want, we can micro-adjust the position using tooltip’s dx and dy, which move element by X pixels horizontally or vertically respectively.
Let’s move the tooltip a few pixels up.
dy = -5
Sometimes we won’t want to show tooltips for all items in the series.
Let’s say we want to show a tooltip for the first and last item.
That’s where “property fields ” come into play.
Property fields allow overriding virtually any property of an item in series with a value from data. The properties like showTooltipOn, pointerOrientation, and dy are no exception.
bullet . propertyFields . showTooltipOn = " showTooltip " ;
series . tooltip . propertyFields . pointerOrientation = " orientation " ;
series . tooltip . propertyFields . dy = " offset " ;
bullet . propertyFields . showTooltipOn = " showTooltip " ;
series . tooltip . propertyFields . pointerOrientation = " orientation " ;
series . tooltip . propertyFields . dy = " offset " ;
"showTooltipOn" : " showTooltip "
"pointerOrientation" : " orientation " ,
The above means that for each and every item, the chart will look for values keys showTooltip, orientation, and offset in data context of the data item, to override default settings for properties showTooltipOn, pointerOrientation, and dy respectively.
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/
* ---------------------------------------
am4core . useTheme ( am4themes_animated );
var chart = am4core . create ( " chartdiv " , am4charts . XYChart );
date: new Date ( 2018 , 0 , 1 ),
date: new Date ( 2018 , 0 , 2 ),
date: new Date ( 2018 , 0 , 3 ),
date: new Date ( 2018 , 0 , 4 ),
date: new Date ( 2018 , 0 , 5 ),
var dateAxis = chart . xAxes . push ( new am4charts . DateAxis ());
dateAxis . renderer . grid . template . location = 0 ;
dateAxis . renderer . minGridDistance = 30 ;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
var series = chart . series . push ( new am4charts . LineSeries ());
series . dataFields . valueY = " value " ;
series . dataFields . dateX = " date " ;
var bullet = series . bullets . push ( new am4charts . CircleBullet ());
bullet . circle . stroke = am4core . color ( " #fff " );
bullet . circle . strokeWidth = 2 ;
bullet . tooltipText = " {dateX}: [bold]{valueY}[/] " ;
series . tooltip . label . textAlign = " middle " ;
series . tooltip . pointerOrientation = " down " ;
bullet . propertyFields . showTooltipOn = " showTooltip " ;
series . tooltip . propertyFields . pointerOrientation = " orientation " ;
series . tooltip . propertyFields . dy = " offset " ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
sticky tooltips sticky-tooltips-3
If you were using alwaysShowTooltip = true property before, make sure you update your code to use showTooltipOn = "always" instead. The former method is being deprecated in favor of the latter, which is more flexible.
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/
* ---------------------------------------
am4core . useTheme ( am4themes_animated );
var chart = am4core . create ( " chartdiv " , am4charts . XYChart );
category: " Distribution " ,
var categoryAxis = chart . xAxes . push ( new am4charts . CategoryAxis ());
categoryAxis . dataFields . category = " category " ;
categoryAxis . renderer . grid . template . location = 0 ;
//categoryAxis.renderer.minGridDistance = 30;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
valueAxis . extraMax = 0.2 ;
valueAxis . renderer . minGridDistance = 30 ;
var series = chart . series . push ( new am4charts . ColumnSeries ());
series . dataFields . valueY = " value " ;
series . dataFields . categoryX = " category " ;
series . tooltip . label . textAlign = " middle " ;
series . tooltip . pointerOrientation = " down " ;
series . columns . template . tooltipText = " {categoryX} \n [bold]{valueY}[/] " ;
series . columns . template . showTooltipOn = " always " ;
series . columns . template . tooltipY = 0 ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
sticky tooltips sticky-tooltips-4
For LineSeries, we are setting tooltip information on its bullets.
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/
* ---------------------------------------
am4core . useTheme ( am4themes_animated );
var chart = am4core . create ( " chartdiv " , am4charts . XYChart );
date: new Date ( 2018 , 0 , 1 ),
date: new Date ( 2018 , 0 , 2 ),
date: new Date ( 2018 , 0 , 3 ),
date: new Date ( 2018 , 0 , 4 ),
date: new Date ( 2018 , 0 , 5 ),
var dateAxis = chart . xAxes . push ( new am4charts . DateAxis ());
dateAxis . renderer . grid . template . location = 0 ;
dateAxis . renderer . minGridDistance = 30 ;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
var series = chart . series . push ( new am4charts . LineSeries ());
series . dataFields . valueY = " value " ;
series . dataFields . dateX = " date " ;
var bullet = series . bullets . push ( new am4charts . CircleBullet ());
bullet . circle . stroke = am4core . color ( " #fff " );
bullet . circle . strokeWidth = 2 ;
bullet . tooltipText = " {dateX}: [bold]{valueY}[/] " ;
series . tooltip . label . textAlign = " middle " ;
series . tooltip . pointerOrientation = " down " ;
bullet . showTooltipOn = " always " ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
sticky tooltips sticky-tooltips-5
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/
* ---------------------------------------
am4core . useTheme ( am4themes_animated );
var chart = am4core . create ( " chartdiv " , am4charts . TreeMap );
chart . hiddenState . properties . opacity = 0 ; // this makes initial fade in effect
chart . dataFields . value = " value " ;
chart . dataFields . name = " name " ;
chart . dataFields . children = " children " ;
var bgColor = new am4core . InterfaceColorSet () . getFor ( " background " );
// level 0 series template
var level0SeriesTemplate = chart . seriesTemplates . create ( " 0 " );
var level0ColumnTemplate = level0SeriesTemplate . columns . template ;
level0ColumnTemplate . column . cornerRadius ( 10 , 10 , 10 , 10 );
level0ColumnTemplate . fillOpacity = 0 ;
level0ColumnTemplate . strokeWidth = 4 ;
level0ColumnTemplate . strokeOpacity = 0 ;
// level 1 series template
var level1SeriesTemplate = chart . seriesTemplates . create ( " 1 " );
var level1ColumnTemplate = level1SeriesTemplate . columns . template ;
level1SeriesTemplate . tooltip . animationDuration = 0 ;
level1SeriesTemplate . strokeOpacity = 1 ;
level1ColumnTemplate . column . cornerRadius ( 10 , 10 , 10 , 10 );
level1ColumnTemplate . fillOpacity = 1 ;
level1ColumnTemplate . strokeWidth = 4 ;
level1ColumnTemplate . stroke = bgColor ;
level1ColumnTemplate . showTooltipOn = " always " ;
level1SeriesTemplate . tooltip . pointerOrientation = " down " ;
level1SeriesTemplate . tooltip . dy = - 15 ;
var bullet1 = level1SeriesTemplate . bullets . push ( new am4charts . LabelBullet ());
bullet1 . label . text = " {name} " ;
bullet1 . label . fill = am4core . color ( " #ffffff " );
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
TreeMap with always on tooltips treemap-with-always-on-tooltips
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 " ];
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 ;
tooltipContent: " United States "
tooltipContent: " Australia "
polygonTemplate . tooltipText = " {tooltipContent} " ;
polygonTemplate . tooltipPosition = " fixed " ;
polygonTemplate . showTooltipOn = " always " ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Show always-on tooltips on MapChart show-always-on-tooltips-on-mapchart