Skip to content

Commit 0a32b98

Browse files
committed
update box and meanline attribute syntax
- use nested attribute style for box and meanline settings - update test and mocks
1 parent 677aacc commit 0a32b98

File tree

10 files changed

+187
-155
lines changed

10 files changed

+187
-155
lines changed

src/traces/box/hover.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function hoverOnBoxes(pointData, xval, yval, hovermode) {
140140
var attrs = ['med', 'min', 'q1', 'q3', 'max'];
141141
var prefixes = ['median', 'min', 'q1', 'q3', 'max'];
142142

143-
if(trace.boxmean || trace.showmeanline) {
143+
if(trace.boxmean || (trace.meanline || {}).visible) {
144144
attrs.push('mean');
145145
prefixes.push(trace.boxmean === 'sd' ? 'mean ± σ' : 'mean');
146146
}

src/traces/box/plot.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function plotPoints(sel, axes, trace, t) {
160160
var bdPos = t.bdPos;
161161
var bPos = t.bPos;
162162

163-
// to support violin innerbox
163+
// to support violin points
164164
var mode = trace.boxpoints || trace.points;
165165

166166
// repeatable pseudorandom number generator

src/traces/violin/attributes.js

+70-64
Original file line numberDiff line numberDiff line change
@@ -138,72 +138,78 @@ module.exports = {
138138
marker: boxAttrs.marker,
139139
text: boxAttrs.text,
140140

141-
showinnerbox: {
142-
valType: 'boolean',
143-
dflt: false,
144-
role: 'info',
145-
editType: 'plot',
146-
description: [
147-
'Determines if an miniature box plot is drawn inside the violins. '
148-
].join(' ')
149-
},
150-
innerboxwidth: {
151-
valType: 'number',
152-
min: 0,
153-
max: 1,
154-
dflt: 0.25,
155-
role: 'info',
156-
editType: 'plot',
157-
description: [
158-
'Sets the width of the inner box plots relative to',
159-
'the violins\' width.',
160-
'For example, with 1, the inner box plots are as wide as the violins.'
161-
].join(' ')
162-
},
163-
innerboxlinecolor: {
164-
valType: 'color',
165-
role: 'style',
166-
editType: 'style',
167-
description: 'Sets the inner box plot bounding line color.'
168-
},
169-
innerboxfillcolor: {
170-
valType: 'color',
171-
role: 'style',
172-
editType: 'style',
173-
description: 'Sets the inner box plot fill color.'
174-
},
175-
innerboxlinewidth: {
176-
valType: 'number',
177-
min: 0,
178-
role: 'style',
179-
editType: 'style',
180-
description: 'Sets the inner box plot bounding line width.'
141+
box: {
142+
visible: {
143+
valType: 'boolean',
144+
dflt: false,
145+
role: 'info',
146+
editType: 'plot',
147+
description: [
148+
'Determines if an miniature box plot is drawn inside the violins. '
149+
].join(' ')
150+
},
151+
width: {
152+
valType: 'number',
153+
min: 0,
154+
max: 1,
155+
dflt: 0.25,
156+
role: 'info',
157+
editType: 'plot',
158+
description: [
159+
'Sets the width of the inner box plots relative to',
160+
'the violins\' width.',
161+
'For example, with 1, the inner box plots are as wide as the violins.'
162+
].join(' ')
163+
},
164+
fillcolor: {
165+
valType: 'color',
166+
role: 'style',
167+
editType: 'style',
168+
description: 'Sets the inner box plot fill color.'
169+
},
170+
line: {
171+
color: {
172+
valType: 'color',
173+
role: 'style',
174+
editType: 'style',
175+
description: 'Sets the inner box plot bounding line color.'
176+
},
177+
width: {
178+
valType: 'number',
179+
min: 0,
180+
role: 'style',
181+
editType: 'style',
182+
description: 'Sets the inner box plot bounding line width.'
183+
}
184+
}
181185
},
182186

183-
showmeanline: {
184-
valType: 'boolean',
185-
dflt: false,
186-
role: 'info',
187-
editType: 'plot',
188-
description: [
189-
'Determines if a line corresponding to the sample\'s mean is shown',
190-
'inside the violins.',
191-
'If `showinnerbox` is turned on, the mean line is drawn inside the inner box.',
192-
'Otherwise, the mean line is drawn from one side of the violin to other.'
193-
].join(' ')
194-
},
195-
meanlinecolor: {
196-
valType: 'color',
197-
role: 'style',
198-
editType: 'style',
199-
description: 'Sets the mean line color.'
200-
},
201-
meanlinewidth: {
202-
valType: 'number',
203-
min: 0,
204-
role: 'style',
205-
editType: 'style',
206-
description: 'Sets the mean line width.'
187+
meanline: {
188+
visible: {
189+
valType: 'boolean',
190+
dflt: false,
191+
role: 'info',
192+
editType: 'plot',
193+
description: [
194+
'Determines if a line corresponding to the sample\'s mean is shown',
195+
'inside the violins.',
196+
'If `box.visible` is turned on, the mean line is drawn inside the inner box.',
197+
'Otherwise, the mean line is drawn from one side of the violin to other.'
198+
].join(' ')
199+
},
200+
color: {
201+
valType: 'color',
202+
role: 'style',
203+
editType: 'style',
204+
description: 'Sets the mean line color.'
205+
},
206+
width: {
207+
valType: 'number',
208+
min: 0,
209+
role: 'style',
210+
editType: 'style',
211+
description: 'Sets the mean line width.'
212+
}
207213
},
208214

209215
side: {

src/traces/violin/defaults.js

+11-21
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,15 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
4141

4242
boxDefaults.handlePointsDefaults(traceIn, traceOut, coerce, {prefix: ''});
4343

44-
var show;
45-
46-
var innerBoxWidth = coerce2('innerboxwidth');
47-
var innerBoxFillColor = coerce2('innerboxfillcolor', fillColor);
48-
var innerBoxLineColor = coerce2('innerboxlinecolor', lineColor);
49-
var innerBoxLineWidth = coerce2('innerboxlinewidth', lineWidth);
50-
show = coerce('showinnerbox', Boolean(innerBoxWidth || innerBoxFillColor || innerBoxLineColor || innerBoxLineWidth));
51-
if(!show) {
52-
delete traceOut.innerboxwidth;
53-
delete traceOut.innerboxfillcolor;
54-
delete traceOut.innerboxlinecolor;
55-
delete traceOut.innerboxlinewidth;
56-
}
57-
58-
var meanLineColor = coerce2('meanlinecolor', lineColor);
59-
var meanLineWidth = coerce2('meanlinewidth', lineWidth);
60-
show = coerce('showmeanline', Boolean(meanLineColor || meanLineWidth));
61-
if(!show) {
62-
delete traceOut.meanlinecolor;
63-
delete traceOut.meanlinewidth;
64-
}
44+
var boxWidth = coerce2('box.width');
45+
var boxFillColor = coerce2('box.fillcolor', fillColor);
46+
var boxLineColor = coerce2('box.line.color', lineColor);
47+
var boxLineWidth = coerce2('box.line.width', lineWidth);
48+
var boxVisible = coerce('box.visible', Boolean(boxWidth || boxFillColor || boxLineColor || boxLineWidth));
49+
if(!boxVisible) delete traceOut.box;
50+
51+
var meanLineColor = coerce2('meanline.color', lineColor);
52+
var meanLineWidth = coerce2('meanline.width', lineWidth);
53+
var meanLineVisible = coerce('meanline.visible', Boolean(meanLineColor || meanLineWidth));
54+
if(!meanLineVisible) delete traceOut.meanline;
6555
};

src/traces/violin/plot.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ module.exports = function plot(gd, plotinfo, cd) {
6060
var hasBothSides = trace.side === 'both';
6161
var hasPositiveSide = hasBothSides || trace.side === 'positive';
6262
var hasNegativeSide = hasBothSides || trace.side === 'negative';
63+
var hasBox = trace.box && trace.box.visible;
64+
var hasMeanLine = trace.meanline && trace.meanline.visible;
6365
var groupStats = fullLayout._violinScaleGroupStats[trace.scalegroup];
6466

6567
sel.selectAll('path.violin')
@@ -128,28 +130,28 @@ module.exports = function plot(gd, plotinfo, cd) {
128130
pathSel.attr('d', path);
129131

130132
// save a few things used in getPositionOnKdePath, getKdeValue
131-
// on hover and for showmeanline
133+
// on hover and for meanline draw block below
132134
d.posCenterPx = posCenterPx;
133135
d.posDensityScale = scale * bdPos;
134136
d.path = pathSel.node();
135137
d.pathLength = d.path.getTotalLength() / (hasBothSides ? 2 : 1);
136138
});
137139

138-
if(trace.showinnerbox) {
139-
var innerBoxWidth = trace.innerboxwidth;
140-
var innerBoxLineWidth = trace.innerboxlinewidth;
140+
if(hasBox) {
141+
var boxWidth = trace.box.width;
142+
var boxLineWidth = trace.box.line.width;
141143
var bdPosScaled;
142144
var bPosPxOffset;
143145

144146
if(hasBothSides) {
145-
bdPosScaled = bdPos * innerBoxWidth;
147+
bdPosScaled = bdPos * boxWidth;
146148
bPosPxOffset = 0;
147149
} else if(hasPositiveSide) {
148-
bdPosScaled = [0, bdPos * innerBoxWidth / 2];
149-
bPosPxOffset = -innerBoxLineWidth;
150+
bdPosScaled = [0, bdPos * boxWidth / 2];
151+
bPosPxOffset = -boxLineWidth;
150152
} else {
151-
bdPosScaled = [bdPos * innerBoxWidth / 2, 0];
152-
bPosPxOffset = innerBoxLineWidth;
153+
bdPosScaled = [bdPos * boxWidth / 2, 0];
154+
bPosPxOffset = boxLineWidth;
153155
}
154156

155157
// do not draw whiskers on inner boxes
@@ -161,10 +163,8 @@ module.exports = function plot(gd, plotinfo, cd) {
161163
bPosPxOffset: bPosPxOffset
162164
});
163165

164-
// if both showinnerbox and showmeanline are turned on, show mean
165-
// line inside inner box
166-
167-
if(trace.showmeanline) {
166+
// if both box and meanline are visible, show mean line inside box
167+
if(hasMeanLine) {
168168
boxPlot.plotBoxMean(sel, {pos: posAxis, val: valAxis}, trace, {
169169
bPos: bPos,
170170
bdPos: bdPosScaled,
@@ -173,7 +173,7 @@ module.exports = function plot(gd, plotinfo, cd) {
173173
}
174174
}
175175
else {
176-
if(trace.showmeanline) {
176+
if(hasMeanLine) {
177177
sel.selectAll('path.mean')
178178
.data(Lib.identity)
179179
.enter().append('path')

src/traces/violin/style.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,29 @@ module.exports = function style(gd) {
1919
.each(function(d) {
2020
var trace = d[0].trace;
2121
var sel = d3.select(this);
22+
var box = trace.box || {};
23+
var boxLine = box.line || {};
24+
var meanline = trace.meanline || {};
25+
var meanLineWidth = meanline.width;
2226

2327
sel.selectAll('path.violin')
2428
.style('stroke-width', trace.line.width + 'px')
2529
.call(Color.stroke, trace.line.color)
2630
.call(Color.fill, trace.fillcolor);
2731

2832
sel.selectAll('path.box')
29-
.style('stroke-width', trace.innerboxlinewidth + 'px')
30-
.call(Color.stroke, trace.innerboxlinecolor)
31-
.call(Color.fill, trace.innerboxfillcolor);
33+
.style('stroke-width', boxLine.width + 'px')
34+
.call(Color.stroke, boxLine.color)
35+
.call(Color.fill, box.fillcolor);
3236

3337
sel.selectAll('g.points path')
3438
.call(Drawing.pointStyle, trace, gd);
3539

36-
var meanLineWidth = trace.meanlinewidth;
37-
3840
sel.selectAll('path.mean')
3941
.style({
4042
'stroke-width': meanLineWidth + 'px',
4143
'stroke-dasharray': (2 * meanLineWidth) + 'px,' + meanLineWidth + 'px'
4244
})
43-
.call(Color.stroke, trace.meanlinecolor);
45+
.call(Color.stroke, meanline.color);
4446
});
4547
};

test/image/mocks/violin_non-linear.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"orientation": "h",
1414
"xcalendar": "discworld",
1515
"name": "discworld dates",
16-
"showinnerbox": true,
16+
"box": {"visible": true},
1717
"xaxis": "x",
1818
"yaxis": "y"
1919
}, {
@@ -29,7 +29,7 @@
2929
],
3030
"orientation": "h",
3131
"name": "categories",
32-
"showinnerbox": true,
32+
"box": {"visible": true},
3333
"xaxis": "x2",
3434
"yaxis": "y2"
3535

test/image/mocks/violin_old-faithful.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "violin",
44
"points": "all",
55
"name": "Old Faithful",
6-
"showmeanline": true,
6+
"meanline": {"visible": true},
77
"y": [79, 54, 74, 62, 85, 55, 88, 85, 51, 85, 54, 84, 78, 47, 83, 52, 62, 84, 52, 79, 51, 47, 78, 69, 74, 83, 55, 76, 78, 79, 73, 77, 66, 80, 74, 52, 48, 80, 59, 90, 80, 58, 84, 58, 73, 83, 64, 53, 82, 59, 75, 90, 54, 80, 54, 83, 71, 64, 77, 81, 59, 84, 48, 82, 60, 92, 78, 78, 65, 73, 82, 56, 79, 71, 62, 76, 60, 78, 76, 83, 75, 82, 70, 65, 73, 88, 76, 80, 48, 86, 60, 90, 50, 78, 63, 72, 84, 75, 51, 82, 62, 88, 49, 83, 81, 47, 84, 52, 86, 81, 75, 59, 89, 79, 59, 81, 50, 85, 59, 87, 53, 69, 77, 56, 88, 81, 45, 82, 55, 90, 45, 83, 56, 89, 46, 82, 51, 86, 53, 79, 81, 60, 82, 77, 76, 59, 80, 49, 96, 53, 77, 77, 65, 81, 71, 70, 81, 93, 53, 89, 45, 86, 58, 78, 66, 76, 63, 88, 52, 93, 49, 57, 77, 68, 81, 81, 73, 50, 85, 74, 55, 77, 83, 83, 51, 78, 84, 46, 83, 55, 81, 57, 76, 84, 77, 81, 87, 77, 51, 78, 60, 82, 91, 53, 78, 46, 77, 84, 49, 83, 71, 80, 49, 75, 64, 76, 53, 94, 55, 76, 50, 82, 54, 75, 78, 79, 78, 78, 70, 79, 70, 54, 86, 50, 90, 54, 54, 77, 79, 64, 75, 47, 86, 63, 85, 82, 57, 82, 67, 74, 54, 83, 73, 73, 88, 80, 71, 83, 56, 79, 78, 84, 58, 83, 43, 60, 75, 81, 46, 90, 46, 74 ]
88
}]
99
}

0 commit comments

Comments
 (0)