Skip to content

Commit d1aed48

Browse files
committed
ignore box/violin *gap and *groupgap when 'width' is set
1 parent 34722d3 commit d1aed48

File tree

6 files changed

+60
-19
lines changed

6 files changed

+60
-19
lines changed

src/traces/box/layout_attributes.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ module.exports = {
2222
'If *group*, the boxes are plotted next to one another',
2323
'centered around the shared location.',
2424
'If *overlay*, the boxes are plotted over one another,',
25-
'you might need to set *opacity* to see them multiple boxes.'
25+
'you might need to set *opacity* to see them multiple boxes.',
26+
'Has no effect on traces that have *width* set.'
2627
].join(' ')
2728
},
2829
boxgap: {
@@ -34,7 +35,8 @@ module.exports = {
3435
editType: 'calc',
3536
description: [
3637
'Sets the gap (in plot fraction) between boxes of',
37-
'adjacent location coordinates.'
38+
'adjacent location coordinates.',
39+
'Has no effect on traces that have *width* set.'
3840
].join(' ')
3941
},
4042
boxgroupgap: {
@@ -46,7 +48,8 @@ module.exports = {
4648
editType: 'calc',
4749
description: [
4850
'Sets the gap (in plot fraction) between boxes of',
49-
'the same location coordinate.'
51+
'the same location coordinate.',
52+
'Has no effect on traces that have *width* set.'
5053
].join(' ')
5154
}
5255
};

src/traces/box/plot.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,32 @@ function plot(gd, plotinfo, cdbox, boxLayer) {
2222
var xa = plotinfo.xaxis;
2323
var ya = plotinfo.yaxis;
2424
var numBoxes = fullLayout._numBoxes;
25-
var groupFraction = (1 - fullLayout.boxgap);
2625
var group = (fullLayout.boxmode === 'group' && numBoxes > 1);
26+
var groupFraction = (1 - fullLayout.boxgap);
27+
var groupGapFraction = 1 - fullLayout.boxgroupgap;
2728

2829
Lib.makeTraceGroups(boxLayer, cdbox, 'trace boxes').each(function(cd) {
2930
var plotGroup = d3.select(this);
3031
var cd0 = cd[0];
3132
var t = cd0.t;
3233
var trace = cd0.trace;
3334
if(!plotinfo.isRangePlot) cd0.node3 = plotGroup;
34-
// box half width
35-
var bdPos = t.dPos * groupFraction * (1 - fullLayout.boxgroupgap) / (group ? numBoxes : 1);
35+
36+
// position coordinate delta
37+
var dPos = t.dPos;
38+
// box half width;
39+
var bdPos;
3640
// box center offset
37-
var bPos = group ? 2 * t.dPos * (-0.5 + (t.num + 0.5) / numBoxes) * groupFraction : 0;
41+
var bPos;
42+
43+
if(trace.width) {
44+
bdPos = dPos;
45+
bPos = 0;
46+
} else {
47+
bdPos = dPos * groupFraction * groupGapFraction / (group ? numBoxes : 1);
48+
bPos = group ? 2 * dPos * (-0.5 + (t.num + 0.5) / numBoxes) * groupFraction : 0;
49+
}
50+
3851
// whisker width
3952
var wdPos = bdPos * trace.whiskerwidth;
4053

src/traces/violin/layout_attributes.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@ module.exports = {
1919
'If *group*, the violins are plotted next to one another',
2020
'centered around the shared location.',
2121
'If *overlay*, the violins are plotted over one another,',
22-
'you might need to set *opacity* to see them multiple violins.'
22+
'you might need to set *opacity* to see them multiple violins.',
23+
'Has no effect on traces that have *width* set.'
2324
].join(' ')
2425
}),
2526
violingap: extendFlat({}, boxLayoutAttrs.boxgap, {
2627
description: [
2728
'Sets the gap (in plot fraction) between violins of',
28-
'adjacent location coordinates.'
29+
'adjacent location coordinates.',
30+
'Has no effect on traces that have *width* set.'
2931
].join(' ')
3032
}),
3133
violingroupgap: extendFlat({}, boxLayoutAttrs.boxgroupgap, {
3234
description: [
3335
'Sets the gap (in plot fraction) between violins of',
34-
'the same location coordinate.'
36+
'the same location coordinate.',
37+
'Has no effect on traces that have *width* set.'
3538
].join(' ')
3639
})
3740
};

src/traces/violin/plot.js

+24-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ module.exports = function plot(gd, plotinfo, cdViolins, violinLayer) {
2020
var fullLayout = gd._fullLayout;
2121
var xa = plotinfo.xaxis;
2222
var ya = plotinfo.yaxis;
23+
var numViolins = fullLayout._numViolins;
24+
var group = (fullLayout.violinmode === 'group' && numViolins > 1);
25+
var groupFraction = 1 - fullLayout.violingap;
26+
var groupGapFraction = 1 - fullLayout.violingroupgap;
2327

2428
function makePath(pts) {
2529
var segments = linePoints(pts, {
@@ -39,16 +43,30 @@ module.exports = function plot(gd, plotinfo, cdViolins, violinLayer) {
3943
var t = cd0.t;
4044
var trace = cd0.trace;
4145
if(!plotinfo.isRangePlot) cd0.node3 = plotGroup;
42-
var numViolins = fullLayout._numViolins;
43-
var group = (fullLayout.violinmode === 'group' && numViolins > 1);
44-
var groupFraction = 1 - fullLayout.violingap;
46+
47+
// position coordinate delta
48+
var dPos = t.dPos;
4549
// violin max half width
46-
var bdPos = t.bdPos = t.dPos * groupFraction * (1 - fullLayout.violingroupgap) / (group ? numViolins : 1);
50+
var bdPos;
4751
// violin center offset
48-
var bPos = t.bPos = group ? 2 * t.dPos * (-0.5 + (t.num + 0.5) / numViolins) * groupFraction : 0;
52+
var bPos;
4953
// half-width within which to accept hover for this violin
5054
// always split the distance to the closest violin
51-
t.wHover = t.dPos * (group ? groupFraction / numViolins : 1);
55+
var wHover;
56+
57+
if(trace.width) {
58+
bdPos = dPos;
59+
bPos = 0;
60+
wHover = dPos;
61+
} else {
62+
bdPos = dPos * groupFraction * groupGapFraction / (group ? numViolins : 1);
63+
bPos = group ? 2 * dPos * (-0.5 + (t.num + 0.5) / numViolins) * groupFraction : 0;
64+
wHover = dPos * (group ? groupFraction / numViolins : 1);
65+
}
66+
67+
t.bdPos = bdPos;
68+
t.bPos = bPos;
69+
t.wHover = wHover;
5270

5371
if(trace.visible !== true || t.empty) {
5472
plotGroup.remove();
Loading

test/image/mocks/violin_box_multiple_widths.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"data": [{
33
"type": "violin",
4-
"width": 0.315,
4+
"width": 0.4,
5+
"name": "width: 0.4",
56
"x": [0, 5, 7, 8],
67
"side": "positive",
78
"line": {
@@ -12,6 +13,7 @@
1213
"y0": 0.0
1314
}, {
1415
"type": "violin",
16+
"name": "auto",
1517
"x": [0, 5, 7, 8],
1618
"side": "positive",
1719
"line": {
@@ -22,7 +24,8 @@
2224
"y0": 0.1
2325
}, {
2426
"type": "box",
25-
"width": 0.5421,
27+
"width": 0.6,
28+
"name": "width: 0.6",
2629
"x": [0, 5, 7, 8],
2730
"side": "positive",
2831
"line": {
@@ -34,7 +37,8 @@
3437
}],
3538
"layout": {
3639
"title": "Joyplot - Violin with multiple widths",
40+
"legend": {"x": 0},
3741
"xaxis": {"zeroline": false},
38-
"violingap": 0
42+
"yaxis": {"dtick": 0.1, "gridcolor": "black"}
3943
}
4044
}

0 commit comments

Comments
 (0)