Skip to content

Commit 4b82426

Browse files
committed
do not accept number as box/violin x0 on multicategory axes
1 parent 204b2e8 commit 4b82426

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

src/plots/cartesian/set_convert.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,16 @@ module.exports = function setConvert(ax, fullLayout) {
188188
}
189189

190190
function getCategoryIndex(v) {
191-
// d2l/d2c variant that that won't add categories but will also
192-
// allow numbers to be mapped to the linearized axis positions
193191
if(ax._categoriesMap) {
194-
var index = ax._categoriesMap[v];
195-
if(index !== undefined) return index;
192+
return ax._categoriesMap[v];
196193
}
194+
}
197195

196+
function getCategoryPosition(v) {
197+
// d2l/d2c variant that that won't add categories but will also
198+
// allow numbers to be mapped to the linearized axis positions
199+
var index = getCategoryIndex(v);
200+
if(index !== undefined) return index;
198201
if(isNumeric(v)) return +v;
199202
}
200203

@@ -280,15 +283,15 @@ module.exports = function setConvert(ax, fullLayout) {
280283
ax.d2c = ax.d2l = setCategoryIndex;
281284
ax.r2d = ax.c2d = ax.l2d = getCategoryName;
282285

283-
ax.d2r = ax.d2l_noadd = getCategoryIndex;
286+
ax.d2r = ax.d2l_noadd = getCategoryPosition;
284287

285288
ax.r2c = function(v) {
286-
var index = getCategoryIndex(v);
289+
var index = getCategoryPosition(v);
287290
return index !== undefined ? index : ax.fraction2r(0.5);
288291
};
289292

290293
ax.l2r = ax.c2r = ensureNumber;
291-
ax.r2l = getCategoryIndex;
294+
ax.r2l = getCategoryPosition;
292295

293296
ax.d2p = function(v) { return ax.l2p(ax.r2c(v)); };
294297
ax.p2d = function(px) { return getCategoryName(p2l(px)); };
@@ -306,15 +309,17 @@ module.exports = function setConvert(ax, fullLayout) {
306309
// account all data array items as in ax.makeCalcdata.
307310

308311
ax.r2d = ax.c2d = ax.l2d = getCategoryName;
309-
ax.d2r = ax.d2l_noadd = getCategoryIndex;
312+
ax.d2r = ax.d2l_noadd = getCategoryPosition;
310313

311314
ax.r2c = function(v) {
312-
var index = getCategoryIndex(v);
315+
var index = getCategoryPosition(v);
313316
return index !== undefined ? index : ax.fraction2r(0.5);
314317
};
315318

319+
ax.r2c_just_indices = getCategoryIndex;
320+
316321
ax.l2r = ax.c2r = ensureNumber;
317-
ax.r2l = getCategoryIndex;
322+
ax.r2l = getCategoryPosition;
318323

319324
ax.d2p = function(v) { return ax.l2p(ax.r2c(v)); };
320325
ax.p2d = function(px) { return getCategoryName(p2l(px)); };

src/traces/box/calc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function getPos(trace, posLetter, posAxis, val, num) {
179179
}
180180

181181
var pos0c = posAxis.type === 'multicategory' ?
182-
posAxis.r2c(pos0) :
182+
posAxis.r2c_just_indices(pos0) :
183183
posAxis.d2c(pos0, 0, trace[posLetter + 'calendar']);
184184

185185
return val.map(function() { return pos0c; });
Loading

test/image/mocks/box-violin-x0-category-position.json

+21-13
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@
1616
},
1717
{
1818
"y": [ 0.6, 0.7, 0.3, 0.6, 0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2, 0.1, 0.3, 0.1, 0.9, 0.6, 0.6, 0.9, 1, 0.3, 0.6, 0.8, 0.5, 0.6, 0.7, 0.3, 0.6, 0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2 ],
19-
"x0": 0.5,
19+
"x0": ["2017", "day 1"],
2020
"type": "violin"
2121
},
2222
{
2323
"y": [ 0.6, 0.7, 0.3, 0.6, 0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2, 0.1, 0.3, 0.1, 0.9, 0.6, 0.6, 0.9, 1, 0.3, 0.6, 0.8, 0.5, 0.6, 0.7, 0.3, 0.6, 0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2 ],
24-
"x0": ["2017", "day 1"],
24+
"x0": "2017,day 2",
2525
"type": "violin"
2626
},
2727
{
2828
"y": [ 0.6, 0.7, 0.3, 0.6, 0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2, 0.1, 0.3, 0.1, 0.9, 0.6, 0.6, 0.9, 1, 0.3, 0.6, 0.8, 0.5, 0.6, 0.7, 0.3, 0.6, 0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2 ],
29-
"x0": "2017,day 2",
30-
"type": "violin"
29+
"x0": "1",
30+
"type": "violin",
31+
"name": "SHOULD NOT BE VISIBLE"
3132
},
3233

3334
{
@@ -47,6 +48,13 @@
4748
"type": "violin",
4849
"xaxis": "x2",
4950
"yaxis": "y2"
51+
},
52+
{
53+
"y": [ 0.6, 0.7, 0.3, 0.6, 0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2, 0.1, 0.3, 0.1, 0.9, 0.6, 0.6, 0.9, 1, 0.3, 0.6, 0.8, 0.5, 0.6, 0.7, 0.3, 0.6, 0, 0.5, 0.7, 0.9, 0.5, 0.8, 0.7, 0.2 ],
54+
"x0": 2,
55+
"type": "violin",
56+
"xaxis": "x2",
57+
"yaxis": "y2"
5058
}
5159
],
5260
"layout": {
@@ -56,13 +64,6 @@
5664
"pattern": "independent"
5765
},
5866
"annotations": [
59-
{
60-
"xref": "x",
61-
"yref": "y",
62-
"x": 0.5,
63-
"text": "violin at x0:0.5",
64-
"y": 1.5
65-
},
6667
{
6768
"xref": "x",
6869
"yref": "y",
@@ -76,13 +77,20 @@
7677
"x": "2017,day 2",
7778
"text": "violin at x0:\"2017,day 2\"",
7879
"y": 1.5,
79-
"ay": -50
80+
"ax": 20
8081
},
8182
{
8283
"xref": "x2",
8384
"yref": "y2",
8485
"x": "day 2",
85-
"text": "box at x0:\"day 2\"",
86+
"text": "violin at x0:\"day 2\"",
87+
"y": 1.5
88+
},
89+
{
90+
"xref": "x2",
91+
"yref": "y2",
92+
"x": 2,
93+
"text": "violin at x0:2",
8694
"y": 1.5
8795
}
8896
],

0 commit comments

Comments
 (0)