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

Implement a separate labelDrawTime option #275

Merged
merged 4 commits into from
Jan 2, 2021
Merged
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
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ To configure the annotations plugin, you can simply add new config options to yo
// context: {chart, element}
},

// Array of annotation configuration objects
// Array of annotation configuration objects
// or Object with the annotation configuration objects, one for each key
// See below for detailed descriptions of the annotation options
annotations: [{
Expand Down Expand Up @@ -113,6 +113,9 @@ Vertical or horizontal lines are supported.
// Background color of label, default below
backgroundColor: 'rgba(0,0,0,0.8)',

// optional drawTime to control labels' layering; defaults to this element's drawTime
drawTime: 'afterDatasetsDraw',

font: {
// Font family of text, inherits from global
family: "sans-serif",
Expand Down Expand Up @@ -214,7 +217,7 @@ The 4 coordinates, xMin, xMax, yMin, yMax are optional. If not specified, the bo

// Fill color
backgroundColor: 'green',

// Radius of box rectangle, default below
cornerRadius: 0,

Expand Down Expand Up @@ -304,7 +307,7 @@ The 2 coordinates, xValue, yValue are optional. If not specified, the point is e

// Stroke width
borderWidth: 2,

// Radius of the point, default below
radius: 10,

Expand Down Expand Up @@ -397,7 +400,7 @@ The 2 coordinates, xValue, yValue are optional. If not specified, the point is e

// Stroke width
borderWidth: 2,

// Radius of the point, default below
radius: 10,

Expand Down
14 changes: 9 additions & 5 deletions src/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,19 @@ function resyncElements(elements, annotations) {

function draw(chart, options, caller) {
const {ctx, chartArea} = chart;
const elements = chartStates.get(chart).elements;
const elements = chartStates.get(chart).elements.filter(el => el._display);

clipArea(ctx, chartArea);
for (let i = 0; i < elements.length; i++) {
const el = elements[i];
if (el._display && (el.options.drawTime || options.drawTime || caller) === caller) {
elements.forEach(el => {
if ((el.options.drawTime || options.drawTime || caller) === caller) {
el.draw(ctx);
}
}
});
elements.forEach(el => {
if ('drawLabel' in el && el.options.label && (el.options.label.drawTime || el.options.drawTime || options.drawTime || caller) === caller) {
el.drawLabel(ctx);
}
});
unclipArea(ctx);
}

Expand Down
1 change: 1 addition & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function scaleValue(scale, value, fallback) {
* @todo handle `radius` as top-left, top-right, bottom-right, bottom-left array/object?
*/
export function roundedRect(ctx, x, y, width, height, radius) {
ctx.beginPath();
if (radius) {
const r = Math.min(radius, height / 2, width / 2);
const left = x + r;
Expand Down
1 change: 0 additions & 1 deletion src/types/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export default class BoxAnnotation extends Element {
ctx.strokeStyle = options.borderColor;
ctx.fillStyle = options.backgroundColor;

ctx.beginPath();
roundedRect(ctx, x, y, width, height, options.cornerRadius);
ctx.fill();
ctx.stroke();
Expand Down
8 changes: 6 additions & 2 deletions src/types/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ export default class LineAnnotation extends Element {
ctx.lineTo(x2, y2);
ctx.stroke();

ctx.restore();
}

drawLabel(ctx) {
if (this.labelIsVisible()) {
ctx.save();
drawLabel(ctx, this);
ctx.restore();
}

ctx.restore();
}

resolveElementProperties(chart, options) {
Expand Down
2 changes: 2 additions & 0 deletions types/label.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Color, FontSpec } from "chart.js";
import { DrawTime } from "./options";

export interface LabelOptions {
backgroundColor?: Color,
drawTime?: DrawTime,
font?: FontSpec
/**
* Padding of label to add left/right, default below
Expand Down