Sometimes you might need to programatically position your chart cursor. This tutorial will show how.
Let’s use this stacked XY chart with a Cursor pre-configured:
Run * --------------------------------------------------------
* This demo was created using amCharts V4 preview release.
* V4 is the latest installement in amCharts data viz
* library family, to be released in the first half of
* For more information and documentation visit:
* https://www.amcharts.com/docs/v4/
* --------------------------------------------------------
am4core . useTheme ( am4themes_animated );
var chart = am4core . create ( " chartdiv " , am4charts . XYChart );
chart . padding = ( 0 , 0 , 0 , 0 );
var categoryAxis = chart . xAxes . push ( new am4charts . CategoryAxis ());
categoryAxis . dataFields . category = " category " ;
categoryAxis . renderer . grid . template . location = 0 ;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
valueAxis . renderer . minWidth = 35 ;
valueAxis . tooltip . disabled = true ;
var series1 = chart . series . push ( new am4charts . ColumnSeries ());
series1 . name = " Series 1 " ;
series1 . dataFields . categoryX = " category " ;
series1 . dataFields . valueY = " value1 " ;
series1 . tooltipText = " {valueY} " ;
var series2 = chart . series . push ( new am4charts . ColumnSeries ());
series2 . name = " Series 2 " ;
series2 . dataFields . categoryX = " category " ;
series2 . dataFields . valueY = " value2 " ;
series2 . tooltipText = " {valueY} " ;
var series3 = chart . series . push ( new am4charts . ColumnSeries ());
series3 . name = " Series 3 " ;
series3 . dataFields . categoryX = " category " ;
series3 . dataFields . valueY = " value3 " ;
series3 . tooltipText = " {valueY} " ;
var series4 = chart . series . push ( new am4charts . ColumnSeries ());
series4 . name = " Series 4 " ;
series4 . dataFields . categoryX = " category " ;
series4 . dataFields . valueY = " value4 " ;
series4 . tooltipText = " {valueY} " ;
chart . cursor = new am4charts . XYCursor ();
chart . cursor . xAxis = categoryAxis ;
chart . cursor . fullWidthLineX = true ;
chart . cursor . lineX . strokeWidth = 0 ;
chart . cursor . lineX . fill = am4core . color ( " #000 " );
chart . cursor . lineX . fillOpacity = 0.1 ;
chart . cursor . behavior = " selectX " ;
chart . cursor . lineY . disabled = true ;
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 " ;
Moving chart cursor via API (1) moving-chart-cursor-via-api-1
Our reference chart has a Category axis with categories like “One”, “Two”, “Three”, etc.
We’d like to place our cursor at, say, “Three”. This means that we’ll need to find out X position of the target category, then instruct the Cursor to move to that position.
And, since we want our cursor to appear on chart load, we’ll also be utilizing chart’s "datavalidated" event.
To get a position to specific category, we’ll need to use our Category axis’ categoryToPoint(category) method:
let point = categoryAxis.categoryToPoint("Three");
var point = categoryAxis.categoryToPoint("Three");
This function will return a simple “point” object that will have x and y properties for the specific category position.
Now that we have position of the category, we just need to instruct our cursor to move there.
For that, we’ll be using Cursor’s triggerMove(point[, triggeredByPointer]) method.
chart.cursor.triggerMove(point, false);
chart.cursor.triggerMove(point, false);
Since our chart is not quite ready at the moment we’re creating it, we’re going to use it event to trigger cursor move:
chart.events.on("datavalidated", function(ev) {
let point = categoryAxis.categoryToPoint("Three");
chart.cursor.triggerMove(point, "none");
chart.events.on("datavalidated", function(ev) {
var point = categoryAxis.categoryToPoint("Three");
chart.cursor.triggerMove(point, "none");
If you are using animated theme, you might also delay your cursor’s grand appearance until the chart has had a chance to animate itself into view:
chart.events.on("datavalidated", function(ev) {
let point = categoryAxis.categoryToPoint("Three");
chart.cursor.triggerMove(point, "none");
}, series1.defaultState.transitionDuration + 100);
chart.events.on("datavalidated", function(ev) {
var point = categoryAxis.categoryToPoint("Three");
chart.cursor.triggerMove(point, "none");
}, series1.defaultState.transitionDuration + 100);
Run * --------------------------------------------------------
* This demo was created using amCharts V4 preview release.
* V4 is the latest installement in amCharts data viz
* library family, to be released in the first half of
* For more information and documentation visit:
* https://www.amcharts.com/docs/v4/
* --------------------------------------------------------
am4core . useTheme ( am4themes_animated );
var chart = am4core . create ( " chartdiv " , am4charts . XYChart );
chart . padding = ( 0 , 0 , 0 , 0 );
var categoryAxis = chart . xAxes . push ( new am4charts . CategoryAxis ());
categoryAxis . dataFields . category = " category " ;
categoryAxis . renderer . grid . template . location = 0 ;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
valueAxis . renderer . minWidth = 35 ;
valueAxis . tooltip . disabled = true ;
var series1 = chart . series . push ( new am4charts . ColumnSeries ());
series1 . name = " Series 1 " ;
series1 . dataFields . categoryX = " category " ;
series1 . dataFields . valueY = " value1 " ;
series1 . tooltipText = " {valueY} " ;
var series2 = chart . series . push ( new am4charts . ColumnSeries ());
series2 . name = " Series 2 " ;
series2 . dataFields . categoryX = " category " ;
series2 . dataFields . valueY = " value2 " ;
series2 . tooltipText = " {valueY} " ;
var series3 = chart . series . push ( new am4charts . ColumnSeries ());
series3 . name = " Series 3 " ;
series3 . dataFields . categoryX = " category " ;
series3 . dataFields . valueY = " value3 " ;
series3 . tooltipText = " {valueY} " ;
var series4 = chart . series . push ( new am4charts . ColumnSeries ());
series4 . name = " Series 4 " ;
series4 . dataFields . categoryX = " category " ;
series4 . dataFields . valueY = " value4 " ;
series4 . tooltipText = " {valueY} " ;
chart . cursor = new am4charts . XYCursor ();
chart . cursor . xAxis = categoryAxis ;
chart . cursor . fullWidthLineX = true ;
chart . cursor . lineX . strokeWidth = 0 ;
chart . cursor . lineX . fill = am4core . color ( " #000 " );
chart . cursor . lineX . fillOpacity = 0.1 ;
chart . cursor . behavior = " selectX " ;
chart . cursor . lineY . disabled = true ;
chart . legend = new am4charts . Legend ();
series4 . events . on ( " ready " , function ( ev ) {
var point = categoryAxis . categoryToPoint ( " Three " );
chart . cursor . triggerMove ( point , " none " );
} , series4 . defaultState . transitionDuration + 100 );
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Moving chart cursor via API (2) moving-chart-cursor-via-api-2
There’s one important caveat, though.
Tooltips are hidden when their related series are being animated.
This means that if you invoke Cursor move when series is animating, which happens when “animated” theme is used and chart is loaded, no Tooltips will be shown.
So, if you are positioning the cursor on chart load, and are using “animated” theme, delay this operation by enough time.