0

I would like to use the zooming and panning feature of canvasjs, but with my own buttons (so that I can control their style, position,..). Is this possible?

user2224350
  • 2,262
  • 5
  • 28
  • 54
  • Posible duplicate: http://stackoverflow.com/questions/5189968/zoom-canvas-to-mouse-cursor – Troyer Nov 28 '16 at 23:22
  • @Troyer, OP is talking about canvasjs library, not about `` tag. Last time when I was trying to customize canvasjs - it was hard, and I gave up (it was few years ago). I don't know what happens now. – Rudolf Manusachi Nov 28 '16 at 23:30
  • You can make use of jQuery to customize the button. Buttons are stored under class **canvasjs-chart-toolbar** – Bivek Nov 29 '16 at 11:19

1 Answers1

2

Take a look at this!

var dataPoints = [];
var y = 0;
for (var i = 0; i < 100; i += 1) {
 y += (Math.random() * 10 - 5);
  dataPoints.push({
    x: i - 100 / 2,
    y: y                
  });
}

var chart = new CanvasJS.Chart("chartContainer",
{
  title: {
   text: "Customized Zoom and Pan Buttons",
   horizontalAlign: "left"

  },
    zoomEnabled:true,
  data: [
  {
   type: "spline",
   dataPoints: dataPoints
  }
  ]
});
chart.render();

$('.canvasjs-chart-toolbar button:eq(0)').text('Pan').addClass('myButton').click(function() {
  if($(this).hasClass("clicked")) {
        $(this).text("Pan");
        $(this).removeClass("clicked");
    } else {
        $(this).text("Zoom");
        $(this).addClass("clicked");
    } 
 });
$('.canvasjs-chart-toolbar button:eq(1)').text('Restore').addClass('myButton').click(function() {
 $('.canvasjs-chart-toolbar button:eq(0)').text('Pan').removeClass("clicked");
});
.myButton {
 background-color:#599bb3;
 -moz-border-radius:28px;
 -webkit-border-radius:28px;
 border-radius:28px;
 border:1px solid #000 !important;
 cursor:pointer;
 color:#ffffff;
 font-family:Arial;
 font-size:17px;
 padding:6px 20px !important;
  margin-left: 3px !important;
 text-decoration:none;
 text-shadow:0px 1px 0px #2f6627;
}
.myButton:hover {
 background-color:#476e9e;
}
<script src="http://canvasjs.com/assets/script/canvasjs.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div id="chartContainer" style="height: 360px; width: 100%;"></div>
Bivek
  • 1,380
  • 10
  • 24
  • This doesnt work, but I took it as a hint: I can just disable the built in toolbar (by hide the toolbar via js) and "copy" their click listeners for my own buttons – user2224350 Nov 29 '16 at 22:54