角度图添加水平线
我想在这里添加水平线 [Chart.js - 绘制水平线 到我的 angular-chart.js(如果我理解正确,我需要做的就是扩展线型图表).
I want add horizontal line like here [Chart.js - draw horizontal line to my angular-chart.js (if I understand right, what I need to do is to extend the line type chart).
我应该如何以及在哪里做(编写自己的指令,尝试在 angular .config 中扩展 char.js)?
How and where I should do it (write own directive, try extend the char.js in angular .config)?
推荐答案
首先,您需要使用您在帖子中提到的扩展来扩展 Chart.js,如下所示:(使用 @jbman223 片段)
First of all, you need to extends Chart.js with the extend you mentionned in your post, like this : (using @jbman223 snippet)
// 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);
}
});
然后,我们必须将这个新的图表类型与角度图表联系起来.不幸的是,它是一个高级抽象库,因此没有内置功能.因此,到目前为止,我发现这样做的唯一方法是通过在配置中添加一行来修改 angular-chart.js:
Then, we have to link this new Chart type with angular-chart. Unfortunately, it is a high level of abstraction library, so there is no built-in feature for that. So, the only way I have found to do that so far is to modify angular-chart.js by adding a line in the config :
return angular.module('chart.js', [])
.provider('ChartJs', ChartJsProvider)
.factory('ChartJsFactory', ['ChartJs', '$timeout', ChartJsFactory])
//...
.directive('chartLinebis', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('LineWithLine'); }]);
最后,用你的新图表标签调用 angular-chart :
Finally, call angular-chart with your new chart label :
<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
Note that it is very important that the js imports respect this order : chart.js -> myExtend.js -> angular-chart.js
JSFiddle (免责声明:我在片段中间包含了 angular-chart.js进口订单目的)
JSFiddle (disclaimer : I included angular-chart.js in the middle of the snippet for import order purpose)
相关文章