首先,您需要使用您在帖子中提到的扩展来扩展Chart.js,如下所示:(使用@ jbman223片段)
// Extend chart.js with a new type of chart
Chart.types.Line.extend({
name: "LineWithLine",
initialize: function () {
Chart.types.Line.prototype.initialize.apply(this, arguments);
},
draw: function () {
Chart.types.Line.prototype.draw.apply(this, arguments);
// Needs to be set with angular-chart options
var lineAtIndex = 2;
var point = this.datasets[0].points[lineAtIndex]
var scale = this.scale
console.log(this);
// draw line
this.chart.ctx.beginPath();
this.chart.ctx.moveTo(scale.startPoint+12, point.y);
this.chart.ctx.strokeStyle = '#ff0000';
this.chart.ctx.lineTo(this.chart.width, point.y);
this.chart.ctx.stroke();
// write TODAY
this.chart.ctx.textAlign = 'center';
this.chart.ctx.fillText("TODAY", scale.startPoint + 35, point.y+10);
}
});
然后,我们必须将此新的Chart类型与angular-chart链接。不幸的是,它是高级抽象库,因此没有内置功能。因此,到目前为止,我发现这样做的唯一方法是通过在config中添加一行来修改angular-chart.js:
return angular.module('chart.js', [])
.provider('ChartJs', ChartJsProvider)
.factory('ChartJsFactory', ['ChartJs', '$timeout', ChartJsFactory])
//...
.directive('chartLinebis', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('LineWithLine'); }]);
最后,使用新的图表标签调用angular-chart:
<canvas class="chart chart-linebis" chart-data="data" chart-labels="labels" chart-legend="true" chart-series="series"></canvas>
请注意,js导入必须遵守此顺序,这一点非常重要:chart.js-> myExtend.js-> angular-chart.js
JSFiddle(免责声明:出于引入订单的目的,我在代码段的中间添加了angular-chart.js)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句