Skip to content

Commit 32ba83d

Browse files
authored
Add borderRadius option (#34)
1 parent 21562af commit 32ba83d

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/element.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Element} from 'chart.js';
2-
import {isObject} from 'chart.js/helpers';
2+
import {isObject, addRoundedRectPath, toTRBLCorners} from 'chart.js/helpers';
33

44
/**
55
* Helper function to get the bounds of the rect
@@ -86,21 +86,23 @@ export default class MatrixElement extends Element {
8686
draw(ctx) {
8787
const options = this.options;
8888
const {inner, outer} = boundingRects(this);
89+
const radius = toTRBLCorners(options.borderRadius);
8990

9091
ctx.save();
9192

9293
if (outer.w !== inner.w || outer.h !== inner.h) {
9394
ctx.beginPath();
94-
ctx.rect(outer.x, outer.y, outer.w, outer.h);
95-
ctx.clip();
96-
ctx.rect(inner.x, inner.y, inner.w, inner.h);
95+
addRoundedRectPath(ctx, {x: outer.x, y: outer.y, w: outer.w, h: outer.h, radius});
96+
addRoundedRectPath(ctx, {x: inner.x, y: inner.y, w: inner.w, h: inner.h, radius});
9797
ctx.fillStyle = options.backgroundColor;
9898
ctx.fill();
9999
ctx.fillStyle = options.borderColor;
100100
ctx.fill('evenodd');
101101
} else {
102+
ctx.beginPath();
103+
addRoundedRectPath(ctx, {x: inner.x, y: inner.y, w: inner.w, h: inner.h, radius});
102104
ctx.fillStyle = options.backgroundColor;
103-
ctx.fillRect(inner.x, inner.y, inner.w, inner.h);
105+
ctx.fill();
104106
}
105107

106108
ctx.restore();
@@ -140,6 +142,7 @@ MatrixElement.defaults = {
140142
backgroundColor: undefined,
141143
borderColor: undefined,
142144
borderWidth: undefined,
145+
borderRadius: 0,
143146
anchorX: undefined,
144147
anchorY: undefined,
145148
width: 20,

types/index.esm.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
BorderRadius,
23
Chart,
34
ChartType,
45
ChartComponent,
@@ -46,6 +47,7 @@ export interface MatrixProps {
4647
export type AnchorX = 'left' | 'center' | 'right';
4748
export type AnchorY = 'top' | 'center' | 'bottom';
4849
export interface MatrixOptions extends CommonElementOptions {
50+
borderRadius: number | BorderRadius;
4951
anchorX: AnchorX;
5052
anchorY: AnchorY;
5153
width: number;

0 commit comments

Comments
 (0)