Chips Chips - 1 year ago 168
Javascript Question

Zoom back button in canvasjs

Is there a way to add a zoom back button in canvasjs using jQuery or normal JS?

I am trying to add it nearby the ones in the top right angle, but I must be
doing something wrong.

In alternative could it be possible to zoom in and out with the scrollwheel?

These are the graphs I want to apply the zoom buttons to:

enter image description here

Answer Source

As of now zoom out step-by-step is not available in the library. You can work around as shown in example.

var axisX = {};
var limit = 1000;    //increase number of dataPoints by increasing this
document.getElementById("button").style.visibility = "hidden";
    var y = 0;
    var data = []; var dataSeries = { type: "line" };
    var dataPoints = [];
    for (var i = 0; i < limit; i += 1) {
        y += (Math.random() * 10 - 5);
          x: i - limit / 2,
          y: y
dataSeries.dataPoints = dataPoints;
var chart = new CanvasJS.Chart("chartContainer",
      zoomEnabled: true,
      rangeChanged: function(e){
        if (!e.chart.options.viewportMinStack){
							e.chart.options.viewportMinStack = [];
						  e.chart.options.viewportMaxStack = [];
				if(e.trigger === "zoom"){
          document.getElementById("button").style.visibility = "visible";
        if(e.trigger === "reset"){
        	axisX.viewportMinimum = null;
          axisX.viewportMaximum = null;
         document.getElementById("button").style.visibility = "hidden";
        text: "Zoom & Pan with Back button"
      axisX: axisX,
      data: data,  // random generator below

//Function for back button
function back(){
	var viewportMinStack = chart.options.viewportMinStack;
  var viewportMaxStack = chart.options.viewportMaxStack;
  //		chart.axisX = {};
  //	}
      axisX.viewportMinimum = viewportMinStack[viewportMinStack.length-1];
      axisX.viewportMaximum = viewportMaxStack[viewportMaxStack.length-1];
      axisX.viewportMinimum = null;
      axisX.viewportMaximum = null;
      document.getElementById("button").style.visibility = "hidden";
var button = document.getElementById( "button" );
button.addEventListener( "click",  back);
    background-color: Transparent;
    border: none;
    overflow: hidden;    
    position: absolute;
    z-index: 100;
    margin-left: 10px;   
.btn-back {
    display: inline-block;
    position: absolute;
    z-index: 1;
    padding: 0 10px 0 30px;
    line-height: 44px;
    color: #000;
    text-decoration: none;
    font-family: sans-serif;
    font-size: 12px;
.btn-back:before {
    content: '';    
    position: absolute;
    box-sizing: border-box;
    left: 20px;
    right: 0;
    height: 34px;
    margin-top: 5px;
    z-index: -1;    
    border: 1px solid #b3b3b3;
    border-left-width: 0;
    border-radius: 5px;  
    background: #d9d9d9;

.btn-back:after {
    content: '';    
    position: absolute;
    box-sizing: border-box;
    left: 11px;
    height: 27px;
    width: 26px;
    top: 8px;
    z-index: -2;    
    border: 1px solid #b3b3b3;
    border-top: 0 transparent;
    border-right: 0 transparent;
    border-radius: 7px 5px 5px 7px;  
    background: #d9d9d9;    
    -webkit-transform: rotate(45deg);
       -moz-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
         -o-transform: rotate(45deg);
            transform: rotate(45deg);
<script src=""></script>

    <button class="btn-back" id="button">Back</button>
    <div id="chartContainer" style="height: 360px; width: 100%;"></div>

And if required you can implement zoom in and out with the scrollwheel using viewportMinimum and viewportMaximum along with javascript mousewheel event.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download