Skip to content

Commit

Permalink
Draw labels on top of lines. Fixes chartjs#94.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Hunt committed May 13, 2019
1 parent fdafa46 commit 74e66f4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ module.exports = function(Chart) {
return function(chartInstance, easingDecimal) {
var defaultDrawTime = chartInstance.annotation.options.drawTime;

helpers.elements(chartInstance)
var elements = helpers.elements(chartInstance)
.filter(function(element) {
return drawTime === (element.options.drawTime || defaultDrawTime);
})
.forEach(function(element) {
element.configure();
element.transition(easingDecimal).draw();
});

// Draw our box/line first, then the labels, so the labels will be on top.
elements.forEach(function(element) {
element.configure();
element.transition(easingDecimal).draw();
});
elements.forEach(function(element) {
element.configure();
element.transition(easingDecimal).drawLabel();
});
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/types/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ module.exports = function(Chart) {
ctx.strokeRect(view.left, view.top, width, height);

ctx.restore();
},
drawLabel: function() {
// Box doesn't draw labels yet, but we add this for consistency of interfaces.
}
});

Expand Down
23 changes: 23 additions & 0 deletions src/types/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,29 @@ module.exports = function(Chart) {
ctx.lineTo(view.x2, view.y2);
ctx.stroke();

ctx.restore();
},
drawLabel: function() {
var view = this._view;
var ctx = this.chartInstance.chart.ctx;

if (!view.clip) {
return;
}

// Canvas setup
ctx.beginPath();
ctx.rect(view.clip.x1, view.clip.y1, view.clip.x2 - view.clip.x1, view.clip.y2 - view.clip.y1);
ctx.clip();

ctx.lineWidth = view.borderWidth;
ctx.strokeStyle = view.borderColor;

if (ctx.setLineDash) {
ctx.setLineDash(view.borderDash);
}
ctx.lineDashOffset = view.borderDashOffset;

if (view.labelEnabled && view.labelContent) {
ctx.beginPath();
ctx.rect(view.clip.x1, view.clip.y1, view.clip.x2 - view.clip.x1, view.clip.y2 - view.clip.y1);
Expand Down

0 comments on commit 74e66f4

Please sign in to comment.