[Chartjs]-How can I create a horizontal scroll effect with angular-chart.js and chart.js

2👍

There are some API changes to work with the later version of chart.js.

So, for instance, properties like xScalePaddingLeft are no longer available, and

onAnimationComplete : function(){} 

is now

animation: { onComplete : function(){} }

There is no better way to do it (IMO) because you really need to have that independent floating Y-axis and chart.js doesn’t provide that option out of the box.

I’ve rewritten the above example to work with the latest versions of angular-charts.js and chart.js:

var app = angular.module("app", ["chart.js"]);

app.controller("LineCtrl", function($scope) {

  $scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
  $scope.series = ['Series A', 'Series B'];
  $scope.data = [
    [65, 59, 80, 81, 56, 55, 40],
    [28, 48, 40, 19, 86, 27, 90]
  ];
  $scope.onClick = function(points, evt) {
    console.log(points, evt);
  };
  $scope.datasetOverride = [{
    yAxisID: 'y-axis-1'
  }];
  $scope.options = {
    scales: {
      yAxes: [{
        id: 'y-axis-1',
        type: 'linear',
        display: true,
        position: 'left'
      }]
    },
    responsive: false,
    animation: {
      onComplete: function() {
        var sourceCanvas = this.chart.ctx.canvas;
        var copyWidth = this.chart.controller.chartArea.left - 5;
        // the +5 is so that the bottommost y axis label is not clipped off
        // we could factor this in using measureText if we wanted to be generic
        var copyHeight = this.chart.controller.chartArea.bottom + 5; // 282 //this.scale.endPoint + 5;

        var targetCtx = document.getElementById("myChartAxis").getContext("2d");
        targetCtx.canvas.width = copyWidth;
        targetCtx.drawImage(sourceCanvas, 0, 0, copyWidth, copyHeight, 0, 0, copyWidth, copyHeight);

      }
    }
  };
});
.chartWrapper {
  position: relative;
}

.chartWrapper>canvas {
  position: absolute;
  left: 0;
  top: 0;
  pointer-events: none;
}

.chartAreaWrapper {
  width: 600px;
  overflow-x: scroll;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.js"></script>
<script src="https://cdn.jsdelivr.net/angular.chartjs/latest/angular-chart.min.js"></script>

<div ng-app="app" ng-controller="LineCtrl">
  <div class="chartWrapper">
    <div class="chartAreaWrapper">
      <canvas id="line" class="chart chart-line" chart-data="data" chart-labels="labels" chart-series="series" chart-options="options" chart-dataset-override="datasetOverride" chart-click="onClick" width="1200" height="300"></canvas>
    </div>
    <canvas id="myChartAxis" height="300" width="0"></canvas>
  </div>
</div>

Here’s the same in a jsfiddle: https://jsfiddle.net/vfzyvg6z/1/

Update with non-scrolling Title and Legend: https://jsfiddle.net/vfzyvg6z/2/

Leave a comment