Skip to content

Commit 1c38597

Browse files
authored
Merge pull request #1791 from plotly/fix-category-autorange2
Fix annotation autorange on category axes
2 parents b53197b + 0758b3b commit 1c38597

File tree

4 files changed

+203
-3
lines changed

4 files changed

+203
-3
lines changed

src/components/shapes/calc_autorange.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module.exports = function calcAutorange(gd) {
4343
};
4444

4545
function shapeBounds(ax, v0, v1, path, paramsToUse) {
46-
var convertVal = (ax.type === 'category') ? Number : ax.d2c;
46+
var convertVal = (ax.type === 'category') ? ax.r2c : ax.d2c;
4747

4848
if(v0 !== undefined) return [convertVal(v0), convertVal(v1)];
4949
if(!path) return;

src/plots/cartesian/set_convert.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,15 @@ module.exports = function setConvert(ax, fullLayout) {
233233

234234
ax.d2r = ax.d2l_noadd = getCategoryIndex;
235235

236-
ax.l2r = ax.r2c = ax.c2r = ensureNumber;
236+
ax.r2c = function(v) {
237+
var index = getCategoryIndex(v);
238+
return index !== undefined ? index : ax.fraction2r(0.5);
239+
};
240+
241+
ax.l2r = ax.c2r = ensureNumber;
237242
ax.r2l = getCategoryIndex;
238243

239-
ax.d2p = function(v) { return ax.l2p(getCategoryIndex(v)); };
244+
ax.d2p = function(v) { return ax.l2p(ax.r2c(v)); };
240245
ax.p2d = function(px) { return getCategoryName(p2l(px)); };
241246
ax.r2p = ax.d2p;
242247
ax.p2r = p2l;
35 KB
Loading
+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
{
2+
"data":[{
3+
"mode": "markers",
4+
"x": ["a", "b", "c"],
5+
"y": ["X", "Y", "Z"],
6+
"xaxis": "x",
7+
"yaxis": "y"
8+
}, {
9+
"mode": "markers",
10+
"x": ["a", "b", "c"],
11+
"y": ["X", "Y", "Z"],
12+
"xaxis": "x2",
13+
"yaxis": "y2"
14+
}, {
15+
"mode": "markers",
16+
"x": ["a", "b", "c"],
17+
"y": ["X", "Y", "Z"],
18+
"xaxis": "x3",
19+
"yaxis": "y3"
20+
}, {
21+
"mode": "markers",
22+
"x": ["a", "b", "c"],
23+
"y": ["X", "Y", "Z"],
24+
"xaxis": "x4",
25+
"yaxis": "y4"
26+
}],
27+
"layout":{
28+
"xaxis":{
29+
"categoryarray": ["a", "b", "c", "d"],
30+
"anchor": "x",
31+
"domain": [0, 0.48],
32+
"ticks": "outside",
33+
"mirror":"allticks",
34+
"zeroline":false,
35+
"showline":true
36+
},
37+
"xaxis2":{
38+
"categoryarray": ["a", "b", "c", "d"],
39+
"anchor": "y2",
40+
"domain": [0.52, 1],
41+
"ticks": "outside",
42+
"mirror":"allticks",
43+
"zeroline":false,
44+
"showline":true,
45+
"range": [-0.5, 2.5]
46+
},
47+
"xaxis3":{
48+
"categoryarray": ["a", "b", "c", "d"],
49+
"side": "top",
50+
"anchor": "y3",
51+
"domain": [0, 0.48],
52+
"ticks": "outside",
53+
"mirror":"allticks",
54+
"zeroline":false,
55+
"showline":true
56+
},
57+
"xaxis4":{
58+
"categoryarray": ["a", "b", "c", "d"],
59+
"side": "top",
60+
"anchor": "y4",
61+
"domain": [0.52, 1],
62+
"ticks": "outside",
63+
"mirror":"allticks",
64+
"zeroline":false,
65+
"showline":true,
66+
"range": [-0.5, 2.5]
67+
},
68+
"yaxis":{
69+
"categoryarray": ["W", "X", "Y", "Z"],
70+
"anchor": "y",
71+
"title": "letter refs",
72+
"domain": [0, 0.48],
73+
"ticks": "outside",
74+
"mirror":"allticks",
75+
"zeroline":false,
76+
"showline":true
77+
},
78+
"yaxis2":{
79+
"categoryarray": ["W", "X", "Y", "Z"],
80+
"domain": [0, 0.48],
81+
"anchor": "x2",
82+
"side": "right",
83+
"ticks": "outside",
84+
"mirror":"allticks",
85+
"zeroline":false,
86+
"showline":true,
87+
"range": [-0.5, 2.5]
88+
},
89+
"yaxis3":{
90+
"categoryarray": ["W", "X", "Y", "Z"],
91+
"title": "number refs",
92+
"domain": [0.52, 1],
93+
"anchor": "x3",
94+
"ticks": "outside",
95+
"mirror":"allticks",
96+
"zeroline":false,
97+
"showline":true
98+
},
99+
"yaxis4":{
100+
"categoryarray": ["W", "X", "Y", "Z"],
101+
"domain": [0.52, 1],
102+
"anchor": "x4",
103+
"side": "right",
104+
"ticks": "outside",
105+
"mirror":"allticks",
106+
"zeroline":false,
107+
"showline":true,
108+
"range": [-0.5, 2.5]
109+
},
110+
"height": 500,
111+
"width": 500,
112+
"showlegend": false,
113+
"margin":{"r":40,"b":25,"l":40,"t":55},
114+
"title": "left: auto range || right: set range",
115+
"font": {"size": 10},
116+
"annotations":[{
117+
"xref": "x", "yref": "y", "axref": "x", "ayref": "y",
118+
"text": "yrange bump",
119+
"x": "a", "y": "X", "ax": "c", "ay": "Z",
120+
"xanchor": "left", "yanchor": "bottom", "borderpad": 10
121+
}, {
122+
"xref": "x", "yref": "y", "showarrow": false,
123+
"text": "xrange bump",
124+
"x": "a", "y": "X",
125+
"xanchor": "right", "borderpad": 10
126+
},
127+
128+
{
129+
"xref": "x2", "yref": "y2", "showarrow": false,
130+
"text": "should not see this",
131+
"x": "b", "y": "Z"
132+
}, {
133+
"xref": "x2", "yref": "y2", "showarrow": false,
134+
"text": "should not see this",
135+
"x": "d", "y": "X",
136+
"xanchor": "right", "borderpad": 10
137+
}, {
138+
"xref": "x2", "yref": "y2", "showarrow": false,
139+
"text": "invalid category coords<br>should appear in center of graph",
140+
"x": "wrong", "y": "not-gonna-work"
141+
},
142+
143+
{
144+
"xref": "x3", "yref": "y3", "axref": "x3", "ayref": "y3",
145+
"text": "yrange bump",
146+
"x": 1, "y": 2, "ax": 2, "ay": 4.5,
147+
"xanchor": "left", "yanchor": "top", "borderpad": 10
148+
}, {
149+
"xref": "x3", "yref": "y3", "showarrow": false,
150+
"text": "xrange bump",
151+
"x": -2, "y": 2,
152+
"xanchor": "left", "borderpad": 10
153+
},
154+
155+
{
156+
"xref": "x4", "yref": "y4", "showarrow": false,
157+
"text": "should not see this",
158+
"x": -2, "y": 2
159+
}, {
160+
"xref": "x4", "yref": "y4", "showarrow": false,
161+
"text": "should not see this",
162+
"x": 2, "y": 4.5
163+
}],
164+
"shapes": [{
165+
"type": "rectangle",
166+
"line": {"color": "purple"},
167+
"x0": "b", "x1": "d",
168+
"y0": "W", "y1": "Z"
169+
},
170+
171+
{
172+
"xref": "x2", "yref": "y2",
173+
"type": "rectangle",
174+
"line": {"color": "purple"},
175+
"x0": "---", "x1": "d",
176+
"y0": "wrong", "y1": "Z"
177+
},
178+
179+
{
180+
"xref": "x3", "yref": "y3",
181+
"type": "rectangle",
182+
"line": {"color": "purple"},
183+
"x0": 0.25, "x1": 6,
184+
"y0": 1, "y1": 2.75
185+
},
186+
187+
{
188+
"xref": "x4", "yref": "y4",
189+
"type": "rectangle",
190+
"line": {"color": "purple"},
191+
"x0": 0.25, "x1": 6,
192+
"y0": 1, "y1": 2.75
193+
}]
194+
}
195+
}

0 commit comments

Comments
 (0)