Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for displaying narrow bars in interactive mode #4568

Merged
merged 8 commits into from
Feb 12, 2020
Merged
19 changes: 7 additions & 12 deletions src/traces/bar/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,6 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
if(isBlank && isHorizontal) x1 = x0;
if(isBlank && !isHorizontal) y1 = y0;
etpinard marked this conversation as resolved.
Show resolved Hide resolved

var spansHorizontal = isHorizontal && (x0 !== x1);
var spansVertical = !isHorizontal && (y0 !== y1);

// in waterfall mode `between` we need to adjust bar end points to match the connector width
if(adjustPixel && !isBlank) {
if(isHorizontal) {
Expand Down Expand Up @@ -194,7 +191,9 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
d3.round(Math.round(v) - offset, 2) : v;
}

function expandToVisible(v, vc) {
function expandToVisible(v, vc, hideZeroSpan) {
if(hideZeroSpan && v === vc) return v;

// if it's not in danger of disappearing entirely,
// round more precisely
return Math.abs(v - vc) >= 2 ? roundWithLine(v) :
Expand All @@ -215,14 +214,10 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
var op = Color.opacity(mc);
var fixpx = (op < 1 || lw > 0.01) ? roundWithLine : expandToVisible;

if(spansHorizontal) {
x0 = fixpx(x0, x1);
x1 = fixpx(x1, x0);
}
if(spansVertical) {
y0 = fixpx(y0, y1);
y1 = fixpx(y1, y0);
}
x0 = fixpx(x0, x1, isHorizontal);
x1 = fixpx(x1, x0, isHorizontal);
y0 = fixpx(y0, y1, !isHorizontal);
y1 = fixpx(y1, y0, !isHorizontal);
etpinard marked this conversation as resolved.
Show resolved Hide resolved
}

var sel = transition(Lib.ensureSingle(bar, 'path'), fullLayout, opts, makeOnCompleteCallback);
Expand Down
Binary file added test/image/baselines/bar_show_narrow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
166 changes: 166 additions & 0 deletions test/image/mocks/bar_show_narrow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{
"data": [
{
"type": "bar",
"width": 0.001,
"x": [
"A",
"B",
"C",
"D"
],
"y": [
0.001,
0.01,
0.1,
1
]
},
{
"type": "bar",
"width": 0.001,
"x": [
"A",
"B",
"C",
"D"
],
"y": [
0.001,
0.01,
0.1,
1
],
"xaxis": "x2",
"yaxis": "y2"
},
{
"type": "bar",
"width": 0.001,
"orientation": "h",
"x": [
0.001,
0.01,
0.1,
1
],
"y": [
"A",
"B",
"C",
"D"
],
"text": [
0,
null,
0.001,
1
],
"xaxis": "x3",
"yaxis": "y3"
},
{
"type": "bar",
"width": 0.001,
"orientation": "h",
"x": [
0.001,
0.01,
0.1,
1
],
"y": [
"A",
"B",
"C",
"D"
],
"xaxis": "x4",
"yaxis": "y4"
}
],
"layout": {
"showlegend": false,
"width": 800,
"height": 800,
"dragmode": "pan",
"xaxis": {
"domain": [
0,
0.48
]
},
"xaxis2": {
"autorange": "reversed",
"anchor": "y2",
"domain": [
0.52,
1
]
},
"xaxis3": {
"range": [
-0.01,
1
],
"zeroline": false,
"anchor": "y3",
"domain": [
0,
0.48
]
},
"xaxis4": {
"range": [
-0.01,
1
],
"zeroline": false,
"autorange": "reversed",
"anchor": "y4",
"domain": [
0.52,
1
]
},
"yaxis": {
"range": [
-0.01,
1
],
"zeroline": false,
"domain": [
0,
0.48
]
},
"yaxis2": {
"range": [
-0.01,
1
],
"zeroline": false,
"autorange": "reversed",
"anchor": "x2",
"domain": [
0.52,
1
]
},
"yaxis3": {
"anchor": "x3",
"domain": [
0.52,
1
]
},
"yaxis4": {
"autorange": "reversed",
"anchor": "x4",
"domain": [
0,
0.48
]
}
}
}
51 changes: 51 additions & 0 deletions test/jasmine/tests/bar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2026,6 +2026,57 @@ describe('A bar plot', function() {
.then(done);
});

it('should show up narrow bars as thin bars', function(done) {
var mock = Lib.extendDeep({}, require('@mocks/bar_show_narrow.json'));

function getArea(path) {
var pos = path
.substr(1, path.length - 2)
.replace('V', ',')
.replace('H', ',')
.replace('V', ',')
.split(',');
var dx = +pos[0];
var dy = +pos[1];
dy -= +pos[2];
dx -= +pos[3];

return Math.abs(dx * dy);
}

Plotly.plot(gd, mock)
.then(function() {
var nodes = gd.querySelectorAll('g.point > path');
expect(nodes.length).toBe(16, '# of bars');

[
[0, true],
[1, true],
[2, true],
[3, true],
[4, true],
[5, true],
[6, true],
[7, true],
[8, true],
[9, true],
[10, true],
[11, true],
[12, true],
[13, true],
[14, true],
[15, true]
].forEach(function(e) {
var i = e[0];
var d = nodes[i].getAttribute('d');
var visible = e[1];
expect(getArea(d) > 0).toBe(visible, 'item:' + i);
});
})
.catch(failTest)
.then(done);
});

it('should not show up null and zero bars as thin bars', function(done) {
var mock = Lib.extendDeep({}, require('@mocks/bar_hide_nulls.json'));

Expand Down