This tutorial will explain how you can set up your charts to initialize pre-zoomed.
This tutorial relies on event handlers. We recommend reading up on how events work in amCharts 4 in “Event Listeners” article.
Let’s start off with a simple date-based chart:
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 , 3 , 20 ),
date: new Date ( 2018 , 3 , 21 ),
date: new Date ( 2018 , 3 , 22 ),
date: new Date ( 2018 , 3 , 23 ),
date: new Date ( 2018 , 3 , 24 ),
date: new Date ( 2018 , 3 , 25 ),
date: new Date ( 2018 , 3 , 26 ),
date: new Date ( 2018 , 3 , 27 ),
date: new Date ( 2018 , 3 , 28 ),
var dateAxis = chart . xAxes . push ( new am4charts . DateAxis ());
dateAxis . renderer . minGridDistance = 50 ;
dateAxis . renderer . grid . template . location = 0.5 ;
dateAxis . startLocation = 0.5 ;
dateAxis . endLocation = 0.5 ;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
var lineSeries = chart . series . push ( new am4charts . LineSeries ());
lineSeries . dataFields . valueY = " value " ;
lineSeries . dataFields . dateX = " date " ;
lineSeries . strokeWidth = 3 ;
chart . scrollbarX = new am4charts . XYChartScrollbar ();
chart . scrollbarX . series . push ( lineSeries );
chart . scrollbarX . scrollbarChart . series . getIndex ( 0 ) . xAxis . startLocation = 0.5 ;
chart . scrollbarX . scrollbarChart . series . getIndex ( 0 ) . xAxis . endLocation = 0.5 ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Pre-zooming Date axis (1) pre-zooming-date-axis-1
There currently are no properties on axes that could be used to tell it to “start off zoomed from X to Y”. By default charts (and is axes) will started full zoomed out, showing full scope of data.
We’re going to be using event handler to catch the moment when chart is ready then use axis methods to trigger zoom.
The most perfect event for our purposes is "ready" which kicks in when our chart data is ready, but before the chart had a chance to draw itself.
Since we’re going to be using a chart with Date axis as an example, the handler for our event will call axis’ zoomToDates() method.
Here’s how the code might look like:
chart.events.on("ready", function () {
chart.events.on("ready", function () {
ev.target.xAxes.getIndex(0).zoomToDates(
Now, when you run the the chart, it will start fully zoomed out, then will zoom to particular date range:
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 , 3 , 20 ),
date: new Date ( 2018 , 3 , 21 ),
date: new Date ( 2018 , 3 , 22 ),
date: new Date ( 2018 , 3 , 23 ),
date: new Date ( 2018 , 3 , 24 ),
date: new Date ( 2018 , 3 , 25 ),
date: new Date ( 2018 , 3 , 26 ),
date: new Date ( 2018 , 3 , 27 ),
date: new Date ( 2018 , 3 , 28 ),
var dateAxis = chart . xAxes . push ( new am4charts . DateAxis ());
dateAxis . renderer . minGridDistance = 50 ;
dateAxis . renderer . grid . template . location = 0.5 ;
dateAxis . startLocation = 0.5 ;
dateAxis . endLocation = 0.5 ;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
var lineSeries = chart . series . push ( new am4charts . LineSeries ());
lineSeries . dataFields . valueY = " value " ;
lineSeries . dataFields . dateX = " date " ;
lineSeries . strokeWidth = 3 ;
lineSeries . showOnInit = false ;
chart . scrollbarX = new am4charts . XYChartScrollbar ();
chart . scrollbarX . series . push ( lineSeries );
chart . scrollbarX . scrollbarChart . series . getIndex ( 0 ) . xAxis . startLocation = 0.5 ;
chart . scrollbarX . scrollbarChart . series . getIndex ( 0 ) . xAxis . endLocation = 0.5 ;
chart . events . on ( " ready " , function () {
dateAxis . zoomToDates ( new Date ( 2018 , 3 , 23 ) , new Date ( 2018 , 3 , 26 ));
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Pre-zooming Date axis (2) pre-zooming-date-axis-2
Important
If you are using an animated theme, you also need to set showOnInit = false for your series. Animated series influences how chart zooms on init, hence disabling your pre-zoom, hence the need for showOnInit to be disabled.
Note
This tutorial uses Date axis as an example, however this technique can be used with any axis type. There are zoomToCategories() method for Category axis, zoomToValues() method for value axis, as well as zoomToIndexes() which works for any axis type, but probably best for Category axis.
Here’s another example, with Value axes on both X and Y. Both with pre-zoom:
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 );
var xAxis = chart . xAxes . push ( new am4charts . ValueAxis ());
xAxis . renderer . minGridDistance = 40 ;
var yAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
var series1 = chart . series . push ( new am4charts . LineSeries ());
series1 . dataFields . valueX = " x " ;
series1 . dataFields . valueY = " ay " ;
series1 . dataFields . value = " aValue " ;
series1 . showOnInit = false ;
var bullet1 = series1 . bullets . push ( new am4charts . CircleBullet ());
bullet1 . tooltipText = " {valueX} x {valueY}: [bold]{value}[/] " ;
var series2 = chart . series . push ( new am4charts . LineSeries ());
series2 . dataFields . valueX = " x " ;
series2 . dataFields . valueY = " by " ;
series2 . dataFields . value = " bValue " ;
series2 . showOnInit = false ;
var bullet2 = series2 . bullets . push ( new am4charts . CircleBullet ());
bullet2 . tooltipText = " {valueX} x {valueY}: [bold]{value}[/] " ;
chart . scrollbarX = new am4core . Scrollbar ();
chart . scrollbarY = new am4core . Scrollbar ();
chart . events . on ( " ready " , function () {
xAxis . zoomToValues ( 7 , 19 , true );
chart . events . on ( " ready " , function () {
yAxis . zoomToValues ( 4 , 14 , true );
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
pre-zooming a ValueAxis pre-zooming-a-valueaxis
If you are not using animated theme, your pre-zoom is already instant. However, if you have your charts animated, and want your chart to start of pre-zoomed instantly, without the animation, all you need to do is to add some parameters to zoom functions.
Namely the fourth parameter to zoomToDates indicates whether zoom should be instant.
The following will make the chart start off pre-zoomed, even with animated theme enabled:
dateAxis.showOnInit = false;
chart.events.on("ready", function () {
true // this makes zoom instant
dateAxis.showOnInit = false;
chart.events.on("ready", function () {
true // this makes zoom instant
ev.target.xAxes.getIndex(0).zoomToDates(
true // this makes zoom instant
Let’s see how it turned out:
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 , 3 , 20 ),
date: new Date ( 2018 , 3 , 21 ),
date: new Date ( 2018 , 3 , 22 ),
date: new Date ( 2018 , 3 , 23 ),
date: new Date ( 2018 , 3 , 24 ),
date: new Date ( 2018 , 3 , 25 ),
date: new Date ( 2018 , 3 , 26 ),
date: new Date ( 2018 , 3 , 27 ),
date: new Date ( 2018 , 3 , 28 ),
var dateAxis = chart . xAxes . push ( new am4charts . DateAxis ());
dateAxis . renderer . minGridDistance = 50 ;
dateAxis . renderer . grid . template . location = 0.5 ;
dateAxis . startLocation = 0.5 ;
dateAxis . endLocation = 0.5 ;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
var lineSeries = chart . series . push ( new am4charts . LineSeries ());
lineSeries . dataFields . valueY = " value " ;
lineSeries . dataFields . dateX = " date " ;
lineSeries . strokeWidth = 3 ;
lineSeries . showOnInit = false ;
chart . scrollbarX = new am4charts . XYChartScrollbar ();
chart . scrollbarX . series . push ( lineSeries );
chart . scrollbarX . scrollbarChart . series . getIndex ( 0 ) . xAxis . startLocation = 0.5 ;
chart . scrollbarX . scrollbarChart . series . getIndex ( 0 ) . xAxis . endLocation = 0.5 ;
chart . events . on ( " ready " , function () {
dateAxis . zoomToDates ( new Date ( 2018 , 3 , 23 ) , new Date ( 2018 , 3 , 26 ) , false , true );
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Pre-zooming Date axis (3) pre-zooming-date-axis-3
So far in this tutorial we’ve looked at pre-zooming to specific dates. However, sometimes we might not care about that, but rather pre-zoom to a relative specific portion of the chart.
For example, what if we would like to pre-zoom to the last 20% of the chart, regardless of the actual scope?
In this case we can use axis’ start and end properties.
Both of those are relative values between 0 (zero) and 1 (one) indicating relative range between first and last data item in the data.
Going back to our tasks of pre-zooming to the last 20% we would have to set those to 0.8 and 1 respectively:
dateAxis . keepSelection = true ;
dateAxis . keepSelection = true ;
Here’s a working example:
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 , 3 , 20 ),
date: new Date ( 2018 , 3 , 21 ),
date: new Date ( 2018 , 3 , 22 ),
date: new Date ( 2018 , 3 , 23 ),
date: new Date ( 2018 , 3 , 24 ),
date: new Date ( 2018 , 3 , 25 ),
date: new Date ( 2018 , 3 , 26 ),
date: new Date ( 2018 , 3 , 27 ),
date: new Date ( 2018 , 3 , 28 ),
var dateAxis = chart . xAxes . push ( new am4charts . DateAxis ());
dateAxis . renderer . minGridDistance = 50 ;
dateAxis . renderer . grid . template . location = 0.5 ;
dateAxis . startLocation = 0.5 ;
dateAxis . endLocation = 0.5 ;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
var lineSeries = chart . series . push ( new am4charts . LineSeries ());
lineSeries . dataFields . valueY = " value " ;
lineSeries . dataFields . dateX = " date " ;
lineSeries . strokeWidth = 3 ;
lineSeries . showOnInit = false ;
chart . scrollbarX = new am4charts . XYChartScrollbar ();
chart . scrollbarX . series . push ( lineSeries );
chart . scrollbarX . scrollbarChart . series . getIndex ( 0 ) . xAxis . startLocation = 0.5 ;
chart . scrollbarX . scrollbarChart . series . getIndex ( 0 ) . xAxis . endLocation = 0.5 ;
dateAxis . keepSelection = true ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Pre-zooming Date axis (4) pre-zooming-date-axis-4