Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/helpers/helpers.canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,17 @@ export function measureLabelSize(ctx, options) {
*/
export function drawBox(ctx, rect, options) {
const {x, y, width, height} = rect;
if (width <= 0 || height <= 0) {
return;
}
ctx.save();
setShadowStyle(ctx, options);
const stroke = setBorderStyle(ctx, options);
ctx.fillStyle = options.backgroundColor;
ctx.beginPath();
addRoundedRectPath(ctx, {
x, y, w: width, h: height,
radius: clampAll(toTRBLCorners(options.borderRadius), 0, Math.min(width, height) / 2)
radius: clampAll(toTRBLCorners(options.borderRadius), 0, Math.max(0, Math.min(width, height) / 2))
});
ctx.closePath();
ctx.fill();
Expand Down
30 changes: 30 additions & 0 deletions test/specs/box.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,34 @@ describe('Box annotation', function() {
}
});
});

describe('non-positive dimensions', function() {
it('should not throw error when drawn with negative or zero dimensions', function() {
expect(function() {
window.scatterChart(10, 10, {
test: {
type: 'box',
xMin: 8,
xMax: 2,
yMin: 6,
yMax: 4,
borderRadius: 5
}
});
}).not.toThrowError();

expect(function() {
window.scatterChart(10, 10, {
test: {
type: 'box',
xMin: 5,
xMax: 5,
yMin: 5,
yMax: 5,
borderRadius: 5
}
});
}).not.toThrowError();
});
});
});