Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draw labels on top of lines. Fixes #94. #161

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ 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.transition(easingDecimal).drawLabel();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not need to transition again?

});
};
}

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() {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
// Box doesn't draw labels yet, but we add this for consistency of interfaces.
}
});

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

ctx.restore();
},
drawLabel: function() {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
var view = this._view;
var ctx = this.chartInstance.chart.ctx;

if (!view.clip) {
return;
}

ctx.save();

// 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