我使用angular-chart.js
(v1.1.1),chart.js
周围的角度包装(我相信angular-chart.js
使用chart.js
v2.x),我是当条形图中的条形数量开始变大时,无法创建水平滚动效果。
我已将其视为其他地方的解决方案:http://jsfiddle.net/mbhavfwm/
可是:
angular-chart.js
onAnimationComplete
中的angular-chart.js
。这是我当前的解决方案:我有3个包含div,canvas
最终在包含div的第3个内部使用以下配置呈现:
this.chartOptions = this.composeChartService.createChartOptions({
title: ctrl.chartTitle,
legend: {
position: 'bottom'
},
responsive: true,
maintainAspectRatio: true
});
这是玉:
mixin dynamic-chart(type)
canvas(
class=`chart chart-${ type }`
title="$ctrl.chartId"
chart-data="$ctrl.events"
chart-series="$ctrl.cities"
chart-labels="$ctrl.labels"
chart-click="$ctrl.onClickBar"
chart-options="$ctrl.chartOptions")
div.chart-wrapper(ng-switch="$ctrl.chartType")
//- Bar chart
div.chart-area-wrapper(ng-when="bar")
div.chart-test
+dynamic-chart('bar')
这是影响这些div的手写笔:
.chart-wrapper
width: 800px
overflow: auto
canvas
background-color: white
width: 100%
.chart-test
width: 1200px !important
background-color: yellow
横向滚动类型适用于此解决方案,但条形厚度太低,我不喜欢任意选择.chart-test
div的宽度,当不需要宽度时。此外,标题和图例有时会隐藏在此解决方案中(取决于滚动的时长)。
那么,有没有更好的方法让angular.chart.js
在水平滚动中发挥出色呢?
答案 0 :(得分:2)
有一些API更改可用于更高版本的chart.js
。
因此,例如,xScalePaddingLeft
等属性不再可用,
onAnimationComplete : function(){}
现在是
animation: { onComplete : function(){} }
没有更好的方法(IMO),因为你真的需要有独立的浮动Y轴和chart.js不提供开箱即用的选项。
我已重写上述示例以使用最新版本的angular-charts.js
和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>
jsfiddle中的情况相同:https://jsfiddle.net/vfzyvg6z/1/
使用非滚动的标题和图例