Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4972429

Browse files
committed
fix issue 4567 - display narrow bars
1 parent acc9c21 commit 4972429

File tree

4 files changed

+224
-12
lines changed

4 files changed

+224
-12
lines changed

src/traces/bar/plot.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,6 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
157157
if(isBlank && isHorizontal) x1 = x0;
158158
if(isBlank && !isHorizontal) y1 = y0;
159159

160-
var spansHorizontal = isHorizontal && (x0 !== x1);
161-
var spansVertical = !isHorizontal && (y0 !== y1);
162-
163160
// in waterfall mode `between` we need to adjust bar end points to match the connector width
164161
if(adjustPixel && !isBlank) {
165162
if(isHorizontal) {
@@ -194,7 +191,9 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
194191
d3.round(Math.round(v) - offset, 2) : v;
195192
}
196193

197-
function expandToVisible(v, vc) {
194+
function expandToVisible(v, vc, hideZeroSpan) {
195+
if(hideZeroSpan && v === vc) return v;
196+
198197
// if it's not in danger of disappearing entirely,
199198
// round more precisely
200199
return Math.abs(v - vc) >= 2 ? roundWithLine(v) :
@@ -215,14 +214,10 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
215214
var op = Color.opacity(mc);
216215
var fixpx = (op < 1 || lw > 0.01) ? roundWithLine : expandToVisible;
217216

218-
if(spansHorizontal) {
219-
x0 = fixpx(x0, x1);
220-
x1 = fixpx(x1, x0);
221-
}
222-
if(spansVertical) {
223-
y0 = fixpx(y0, y1);
224-
y1 = fixpx(y1, y0);
225-
}
217+
x0 = fixpx(x0, x1, isHorizontal);
218+
x1 = fixpx(x1, x0, isHorizontal);
219+
y0 = fixpx(y0, y1, !isHorizontal);
220+
y1 = fixpx(y1, y0, !isHorizontal);
226221
}
227222

228223
var sel = transition(Lib.ensureSingle(bar, 'path'), fullLayout, opts, makeOnCompleteCallback);
28.6 KB
Loading

test/image/mocks/bar_show_narrow.json

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
{
2+
"data": [
3+
{
4+
"type": "bar",
5+
"width": 0.001,
6+
"x": [
7+
"A",
8+
"B",
9+
"C",
10+
"D"
11+
],
12+
"y": [
13+
0.001,
14+
0.01,
15+
0.1,
16+
1
17+
]
18+
},
19+
{
20+
"type": "bar",
21+
"width": 0.001,
22+
"x": [
23+
"A",
24+
"B",
25+
"C",
26+
"D"
27+
],
28+
"y": [
29+
0.001,
30+
0.01,
31+
0.1,
32+
1
33+
],
34+
"xaxis": "x2",
35+
"yaxis": "y2"
36+
},
37+
{
38+
"type": "bar",
39+
"width": 0.001,
40+
"orientation": "h",
41+
"x": [
42+
0.001,
43+
0.01,
44+
0.1,
45+
1
46+
],
47+
"y": [
48+
"A",
49+
"B",
50+
"C",
51+
"D"
52+
],
53+
"text": [
54+
0,
55+
null,
56+
0.001,
57+
1
58+
],
59+
"xaxis": "x3",
60+
"yaxis": "y3"
61+
},
62+
{
63+
"type": "bar",
64+
"width": 0.001,
65+
"orientation": "h",
66+
"x": [
67+
0.001,
68+
0.01,
69+
0.1,
70+
1
71+
],
72+
"y": [
73+
"A",
74+
"B",
75+
"C",
76+
"D"
77+
],
78+
"xaxis": "x4",
79+
"yaxis": "y4"
80+
}
81+
],
82+
"layout": {
83+
"showlegend": false,
84+
"width": 800,
85+
"height": 800,
86+
"dragmode": "pan",
87+
"xaxis": {
88+
"domain": [
89+
0,
90+
0.48
91+
]
92+
},
93+
"xaxis2": {
94+
"autorange": "reversed",
95+
"anchor": "y2",
96+
"domain": [
97+
0.52,
98+
1
99+
]
100+
},
101+
"xaxis3": {
102+
"range": [
103+
-0.01,
104+
1
105+
],
106+
"zeroline": false,
107+
"anchor": "y3",
108+
"domain": [
109+
0,
110+
0.48
111+
]
112+
},
113+
"xaxis4": {
114+
"range": [
115+
-0.01,
116+
1
117+
],
118+
"zeroline": false,
119+
"autorange": "reversed",
120+
"anchor": "y4",
121+
"domain": [
122+
0.52,
123+
1
124+
]
125+
},
126+
"yaxis": {
127+
"range": [
128+
-0.01,
129+
1
130+
],
131+
"zeroline": false,
132+
"domain": [
133+
0,
134+
0.48
135+
]
136+
},
137+
"yaxis2": {
138+
"range": [
139+
-0.01,
140+
1
141+
],
142+
"zeroline": false,
143+
"autorange": "reversed",
144+
"anchor": "x2",
145+
"domain": [
146+
0.52,
147+
1
148+
]
149+
},
150+
"yaxis3": {
151+
"anchor": "x3",
152+
"domain": [
153+
0.52,
154+
1
155+
]
156+
},
157+
"yaxis4": {
158+
"autorange": "reversed",
159+
"anchor": "x4",
160+
"domain": [
161+
0,
162+
0.48
163+
]
164+
}
165+
}
166+
}

test/jasmine/tests/bar_test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,6 +2026,57 @@ describe('A bar plot', function() {
20262026
.then(done);
20272027
});
20282028

2029+
it('should show up narrow bars as thin bars', function(done) {
2030+
var mock = Lib.extendDeep({}, require('@mocks/bar_show_narrow.json'));
2031+
2032+
function getArea(path) {
2033+
var pos = path
2034+
.substr(1, path.length - 2)
2035+
.replace('V', ',')
2036+
.replace('H', ',')
2037+
.replace('V', ',')
2038+
.split(',');
2039+
var dx = +pos[0];
2040+
var dy = +pos[1];
2041+
dy -= +pos[2];
2042+
dx -= +pos[3];
2043+
2044+
return Math.abs(dx * dy);
2045+
}
2046+
2047+
Plotly.plot(gd, mock)
2048+
.then(function() {
2049+
var nodes = gd.querySelectorAll('g.point > path');
2050+
expect(nodes.length).toBe(16, '# of bars');
2051+
2052+
[
2053+
[0, true],
2054+
[1, true],
2055+
[2, true],
2056+
[3, true],
2057+
[4, true],
2058+
[5, true],
2059+
[6, true],
2060+
[7, true],
2061+
[8, true],
2062+
[9, true],
2063+
[10, true],
2064+
[11, true],
2065+
[12, true],
2066+
[13, true],
2067+
[14, true],
2068+
[15, true]
2069+
].forEach(function(e) {
2070+
var i = e[0];
2071+
var d = nodes[i].getAttribute('d');
2072+
var visible = e[1];
2073+
expect(getArea(d) > 0).toBe(visible, 'item:' + i);
2074+
});
2075+
})
2076+
.catch(failTest)
2077+
.then(done);
2078+
});
2079+
20292080
it('should not show up null and zero bars as thin bars', function(done) {
20302081
var mock = Lib.extendDeep({}, require('@mocks/bar_hide_nulls.json'));
20312082

0 commit comments

Comments
 (0)