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 - hide empty bars in interactive mode #4522

Merged
merged 4 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/traces/bar/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
!isNumeric(y0) ||
!isNumeric(y1)
);

// display zeros if line.width > 0
if(isBlank && shouldDisplayZeros && helpers.getLineWidth(trace, di) && (isHorizontal ? x1 - x0 === 0 : y1 - y0 === 0)) {
isBlank = false;
Expand Down Expand Up @@ -190,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, hideZeroHeight) {
etpinard marked this conversation as resolved.
Show resolved Hide resolved
if(hideZeroHeight && 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 @@ -210,10 +213,11 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)

var op = Color.opacity(mc);
var fixpx = (op < 1 || lw > 0.01) ? roundWithLine : expandToVisible;
x0 = fixpx(x0, x1);
x1 = fixpx(x1, x0);
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);
}

var sel = transition(Lib.ensureSingle(bar, 'path'), fullLayout, opts, makeOnCompleteCallback);
Expand Down
Binary file added test/image/baselines/bar_hide_nulls.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
180 changes: 180 additions & 0 deletions test/image/mocks/bar_hide_nulls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
{
"data": [
{
"type": "bar",
"x": [
"A",
"B",
"C"
],
"y": [
0,
null,
1
],
"text": [
0,
null,
1
],
"textposition": "auto",
"insidetextanchor": "middle",
"cliponaxis": false
},
{
"type": "bar",
"x": [
"A",
"B",
"C"
],
"y": [
0,
null,
1
],
"text": [
0,
null,
1
],
"textposition": "auto",
"insidetextanchor": "middle",
"cliponaxis": false,
"xaxis": "x2",
"yaxis": "y2"
},
{
"type": "bar",
"orientation": "h",
"x": [
0,
null,
1
],
"y": [
"A",
"B",
"C"
],
"text": [
0,
null,
1
],
"textposition": "auto",
"insidetextanchor": "middle",
"cliponaxis": false,
"xaxis": "x3",
"yaxis": "y3"
},
{
"type": "bar",
"orientation": "h",
"x": [
0,
null,
1
],
"y": [
"A",
"B",
"C"
],
"text": [
0,
null,
1
],
"textposition": "auto",
"insidetextanchor": "middle",
"cliponaxis": false,
"xaxis": "x4",
"yaxis": "y4"
}
],
"layout": {
"showlegend": true,
"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
]
}
}
}
34 changes: 34 additions & 0 deletions test/jasmine/tests/bar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2025,6 +2025,40 @@ describe('A bar plot', function() {
.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'));

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 dx * dy;
}

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

[
0, 1, 3, 4, 6, 7, 9, 10
].forEach(function(i) {
var d = nodes[i].getAttribute('d');
expect(getArea(d)).toBe(0, 'item:' + i);
});
})
.catch(failTest)
.then(done);
});
});

describe('bar visibility toggling:', function() {
Expand Down