Skip to content
This repository was archived by the owner on Nov 25, 2021. It is now read-only.

Commit a228345

Browse files
author
Luke Lowrey
committed
'medianColor' no longer affects current drawing context if not set
Only sets the stroke style if there is a color set (e.g. not null) else it uses the current border color. This also rearranges the drawing order so that the box outline is drawn on top of the median line, so that you don't end up with the median line cutting into the border.
1 parent 2f3d9b5 commit a228345

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

src/elements/base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const defaults = {
88
borderWidth: 1,
99
outlierRadius: 2,
1010
outlierColor: Chart.defaults.global.elements.rectangle.backgroundColor,
11-
medianColor: Chart.defaults.global.elements.rectangle.borderColor,
11+
medianColor: null,
1212
itemRadius: 0,
1313
itemStyle: 'circle',
1414
itemBackgroundColor: Chart.defaults.global.elements.rectangle.backgroundColor,

src/elements/boxandwhiskers.js

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,34 @@ const BoxAndWiskers = Chart.elements.BoxAndWhiskers = ArrayElementBase.extend({
8585
const x = vm.x;
8686
const width = vm.width;
8787
const x0 = x - width / 2;
88+
89+
// Draw the q1>q3 box
8890
if (boxplot.q3 > boxplot.q1) {
8991
ctx.fillRect(x0, boxplot.q1, width, boxplot.q3 - boxplot.q1);
90-
ctx.strokeRect(x0, boxplot.q1, width, boxplot.q3 - boxplot.q1);
9192
} else {
9293
ctx.fillRect(x0, boxplot.q3, width, boxplot.q1 - boxplot.q3);
94+
}
95+
96+
// Draw the median line
97+
ctx.save();
98+
if (vm.medianColor) {
99+
ctx.strokeStyle = vm.medianColor;
100+
}
101+
ctx.beginPath();
102+
ctx.moveTo(x0, boxplot.median);
103+
ctx.lineTo(x0 + width, boxplot.median);
104+
ctx.closePath();
105+
ctx.stroke();
106+
ctx.restore();
107+
108+
// Draw the border around the main q1>q3 box
109+
if (boxplot.q3 > boxplot.q1) {
110+
ctx.strokeRect(x0, boxplot.q1, width, boxplot.q3 - boxplot.q1);
111+
} else {
93112
ctx.strokeRect(x0, boxplot.q3, width, boxplot.q1 - boxplot.q3);
94113
}
114+
115+
// Draw the whiskers
95116
ctx.beginPath();
96117
ctx.moveTo(x0, boxplot.whiskerMin);
97118
ctx.lineTo(x0 + width, boxplot.whiskerMin);
@@ -103,26 +124,39 @@ const BoxAndWiskers = Chart.elements.BoxAndWhiskers = ArrayElementBase.extend({
103124
ctx.lineTo(x, boxplot.q3);
104125
ctx.closePath();
105126
ctx.stroke();
106-
ctx.save();
107-
ctx.strokeStyle = vm.medianColor;
108-
ctx.beginPath();
109-
ctx.moveTo(x0, boxplot.median);
110-
ctx.lineTo(x0 + width, boxplot.median);
111-
ctx.closePath();
112-
ctx.stroke();
113-
ctx.restore();
114127
},
115128
_drawBoxPlotHoriz(vm, boxplot, ctx) {
116129
const y = vm.y;
117130
const height = vm.height;
118131
const y0 = y - height / 2;
132+
133+
// Draw the q1>q3 box
119134
if (boxplot.q3 > boxplot.q1) {
120135
ctx.fillRect(boxplot.q1, y0, boxplot.q3 - boxplot.q1, height);
121-
ctx.strokeRect(boxplot.q1, y0, boxplot.q3 - boxplot.q1, height);
122136
} else {
123137
ctx.fillRect(boxplot.q3, y0, boxplot.q1 - boxplot.q3, height);
138+
}
139+
140+
// Draw the median line
141+
ctx.save();
142+
if (vm.medianColor) {
143+
ctx.strokeStyle = vm.medianColor;
144+
}
145+
ctx.beginPath();
146+
ctx.moveTo(boxplot.median, y0);
147+
ctx.lineTo(boxplot.median, y0 + height);
148+
ctx.closePath();
149+
ctx.stroke();
150+
ctx.restore();
151+
152+
// Draw the border around the main q1>q3 box
153+
if (boxplot.q3 > boxplot.q1) {
154+
ctx.strokeRect(boxplot.q1, y0, boxplot.q3 - boxplot.q1, height);
155+
} else {
124156
ctx.strokeRect(boxplot.q3, y0, boxplot.q1 - boxplot.q3, height);
125157
}
158+
159+
// Draw the whiskers
126160
ctx.beginPath();
127161
ctx.moveTo(boxplot.whiskerMin, y0);
128162
ctx.lineTo(boxplot.whiskerMin, y0 + height);
@@ -134,12 +168,6 @@ const BoxAndWiskers = Chart.elements.BoxAndWhiskers = ArrayElementBase.extend({
134168
ctx.lineTo(boxplot.q3, y);
135169
ctx.closePath();
136170
ctx.stroke();
137-
ctx.strokeStyle = vm.medianColor;
138-
ctx.beginPath();
139-
ctx.moveTo(boxplot.median, y0);
140-
ctx.lineTo(boxplot.median, y0 + height);
141-
ctx.closePath();
142-
ctx.stroke();
143171
},
144172
_getBounds() {
145173
const vm = this._view;

0 commit comments

Comments
 (0)