During the course of this tutorial we’ll modify the functionality of the legend from toggling series on and off, to rather highlighting it.
Here’s our base chart we’re going to be working with:
Result JS HTML CSS
Open in: New tab CodePen (opens in a new tab) jsFiddle (opens in a new tab) 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 );
am4core . useTheme ( am4themes_dataviz );
var chart = am4core . create ( " chartdiv " , am4charts . XYChart );
country: " Czech Republic " ,
country: " The Netherlands " ,
//console.log('chart', chart);
var categoryAxis = chart . xAxes . push ( new am4charts . CategoryAxis ());
categoryAxis . dataFields . category = " country " ;
categoryAxis . title . text = " Local country offices " ;
categoryAxis . renderer . grid . template . location = 0 ;
categoryAxis . renderer . minGridDistance = 20 ;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
valueAxis . title . text = " Expenditure (M) " ;
function createSeries ( field , name ) {
var series = chart . series . push ( new am4charts . ColumnSeries ());
series . dataFields . valueY = field ;
series . dataFields . categoryX = " country " ;
series . tooltipText = " {name}: [bold]{valueY}[/] " ;
series . columns . template . fillOpacity = 0.7 ;
series . columns . template . strokeWidth = 0 ;
createSeries ( " research " , " Research " );
createSeries ( " marketing " , " Martketing " );
createSeries ( " sales " , " Sales " );
chart . cursor = new am4charts . XYCursor ();
chart . legend = new am4charts . Legend ();
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Highlighting series on legend click highlighting-series-on-legend-click
It has a legend, which acts normally: toggles relative series on and off on click.
We’ve also set up our series to be somewhat semitransparent. We’re going to make them fully opaque when we implement “highlighting” functionality.
SIDE READING A state is a collection of property values to apply to object when certain conditions are met (e.g. it is hovered) or when applied manually by some code - one like we’re going to do. For more about states, read here .
We are going to create an "active" state for the columns in our series. Our code which triggers “highlight” will just have to apply that state to the series for it to cascade to all its columns and trigger state’s property override.
let hs = series . columns . template . states . create ( " active " );hs . properties . fillOpacity = 1 ;
var hs = series . columns . template . states . create ( " active " ); hs . properties . fillOpacity = 1 ;
{ // … "series": [{ // … "columns": { "states": { "active": { "properties": { "fillOpacity": 1 } } } } }]}
The above basically means: “when active state is applied set fillOpacity of each column to 1”.
Our series is all ready. Now let’s see how we need to mod our legend.
To make this work we’ll need to do two things:
Disable default functionality of the legend - toggling series.
Add our own events to legend that would apply (or remove) the “active” state for its respective series.
This one’s easy: simply disable toggling of the legend items:
chart . legend . itemContainers . template . togglable = false ;
chart . legend . itemContainers . template . togglable = false ;
{ // … "legend": { "itemContainers": { "togglable": false } }}
Now, if you click/tap on legend item, nothing happens. Good, let’s add our own thing.
Since we have disabled toggling, and built-in toggle events are not firing on legend items anymore, we can add our own "hit" event, that will do its magic.
chart . legend . itemContainers . template . events . on ( " hit " , function ( ev ) { let seriesColumn = ev . target . dataItem . dataContext . columns . template ; seriesColumn . isActive = ! seriesColumn . isActive ;});
chart . legend . itemContainers . template . events . on ( " hit " , function ( ev ) { var seriesColumn = ev . target . dataItem . dataContext . columns . template ; seriesColumn . isActive = ! seriesColumn . isActive ;});
{ // … "legend": { "itemContainers": { "togglable": false, "events": { "hit": function(ev) { var seriesColumn = ev.target.dataItem.dataContext.columns.template; seriesColumn.isActive = !seriesColumn.isActive; } } } }}
Note
Setting isActive = true automatically sets "active" state on an element, while setting it to false restores default state. This way we can easily make the series automatically apply relative state by toggling isActive on a column template, which will make the state change cascade to all actual columns.
Now that we have everything ready, let’s try it out.
Result JS HTML CSS
Open in: New tab CodePen (opens in a new tab) jsFiddle (opens in a new tab) 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 );
am4core . useTheme ( am4themes_dataviz );
var chart = am4core . create ( " chartdiv " , am4charts . XYChart );
country: " Czech Republic " ,
country: " The Netherlands " ,
//console.log('chart', chart);
var categoryAxis = chart . xAxes . push ( new am4charts . CategoryAxis ());
categoryAxis . dataFields . category = " country " ;
categoryAxis . title . text = " Local country offices " ;
categoryAxis . renderer . grid . template . location = 0 ;
categoryAxis . renderer . minGridDistance = 20 ;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
valueAxis . title . text = " Expenditure (M) " ;
function createSeries ( field , name ) {
var series = chart . series . push ( new am4charts . ColumnSeries ());
series . dataFields . valueY = field ;
series . dataFields . categoryX = " country " ;
series . tooltipText = " {name}: [bold]{valueY}[/] " ;
series . columns . template . fillOpacity = 0.7 ;
series . columns . template . strokeWidth = 0 ;
var hs = series . columns . template . states . create ( " active " );
hs . properties . fillOpacity = 1 ;
createSeries ( " research " , " Research " );
createSeries ( " marketing " , " Martketing " );
createSeries ( " sales " , " Sales " );
chart . cursor = new am4charts . XYCursor ();
chart . legend = new am4charts . Legend ();
// Disable toggling of slices
chart . legend . itemContainers . template . togglable = false ;
// Add hit event to pull out slice on click
chart . legend . itemContainers . template . events . on ( " hit " , function ( ev ) {
var seriesColumn = ev . target . dataItem . dataContext . columns . template ;
seriesColumn . isActive = ! seriesColumn . isActive ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Highlighting series on legend click highlighting-series-on-legend-click-2