Skip to content

Commit e06fc92

Browse files
committed
Updated _drawCoords()
- Added ctx.beginPath() to start a new path for drawing. - Improved _drawCoords() path construction by directly connecting the last and first coordinates.
1 parent c43fcd4 commit e06fc92

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed
-1.27 KB
Loading

src/elements/Violin.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,44 @@ export class Violin extends StatsBase<IViolinElementProps, IViolinElementOptions
7070
} else {
7171
maxEstimate = props.maxEstimate;
7272
}
73+
const firstCoord = props.coords[0];
74+
const lastCoord = props.coords[props.coords.length - 1];
75+
76+
ctx.beginPath();
7377
if (this.isVertical()) {
7478
const { x, width } = props;
7579
const factor = width / 2 / maxEstimate;
76-
ctx.moveTo(x, props.min);
80+
ctx.moveTo(x - firstCoord.estimate * factor, firstCoord.v);
7781
props.coords.forEach((c) => {
7882
ctx.lineTo(x - c.estimate * factor, c.v);
7983
});
80-
ctx.lineTo(x, props.max);
81-
ctx.moveTo(x, props.min);
82-
props.coords.forEach((c) => {
83-
ctx.lineTo(x + c.estimate * factor, c.v);
84-
});
85-
ctx.lineTo(x, props.max);
84+
85+
ctx.lineTo(x - lastCoord.estimate * factor, props.max);
86+
ctx.lineTo(x + lastCoord.estimate * factor, props.max);
87+
props.coords
88+
.slice()
89+
.reverse()
90+
.forEach((c) => {
91+
ctx.lineTo(x + c.estimate * factor, c.v);
92+
});
93+
ctx.lineTo(x - firstCoord.estimate * factor, firstCoord.v);
8694
} else {
8795
const { y, height } = props;
8896
const factor = height / 2 / maxEstimate;
89-
ctx.moveTo(props.min, y);
97+
ctx.moveTo(firstCoord.v, y - firstCoord.estimate * factor);
9098
props.coords.forEach((c) => {
9199
ctx.lineTo(c.v, y - c.estimate * factor);
92100
});
93-
ctx.lineTo(props.max, y);
94-
ctx.moveTo(props.min, y);
95-
props.coords.forEach((c) => {
96-
ctx.lineTo(c.v, y + c.estimate * factor);
97-
});
98-
ctx.lineTo(props.max, y);
101+
102+
ctx.lineTo(props.max, y - props.coords[props.coords.length - 1].estimate * factor);
103+
ctx.lineTo(props.max, y + props.coords[props.coords.length - 1].estimate * factor);
104+
props.coords
105+
.slice()
106+
.reverse()
107+
.forEach((c) => {
108+
ctx.lineTo(c.v, y + c.estimate * factor);
109+
});
110+
ctx.lineTo(props.min, y - props.coords[0].estimate * factor);
99111
}
100112
ctx.closePath();
101113
ctx.stroke();

0 commit comments

Comments
 (0)