Skip to content

Set the maximum width of categorical bars & boxes to category unit - case of categoryarray #5732

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

Merged
merged 4 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
8 changes: 2 additions & 6 deletions src/lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ exports.sorterDes = function(a, b) { return b - a; };
* just be off by a rounding error
* return the distinct values and the minimum difference between any two
*/
exports.distinctVals = function(valsIn, opts) {
var unitMinDiff = (opts || {}).unitMinDiff;

exports.distinctVals = function(valsIn) {
var vals = valsIn.slice(); // otherwise we sort the original array...
vals.sort(exports.sorterAsc); // undefined listed in the end - also works on IE11

Expand All @@ -73,9 +71,7 @@ exports.distinctVals = function(valsIn, opts) {
if(vals[last] !== BADNUM) break;
}

var minDiff = 1;
if(!unitMinDiff) minDiff = (vals[last] - vals[0]) || 1;

var minDiff = (vals[last] - vals[0]) || 1;
var errDiff = minDiff / (last || 1) / 10000;
var newVals = [];
var preV;
Expand Down
9 changes: 6 additions & 3 deletions src/traces/bar/cross_trace_calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function setGroupPositionsInOverlayMode(pa, sa, calcTraces, opts) {
var calcTrace = calcTraces[i];

var sieve = new Sieve([calcTrace], {
unitMinDiff: opts.xCat || opts.yCat,
posAxis: pa,
sepNegVal: false,
overlapNoMerge: !opts.norm
});
Expand All @@ -196,6 +196,7 @@ function setGroupPositionsInOverlayMode(pa, sa, calcTraces, opts) {

function setGroupPositionsInGroupMode(gd, pa, sa, calcTraces, opts) {
var sieve = new Sieve(calcTraces, {
posAxis: pa,
sepNegVal: false,
overlapNoMerge: !opts.norm
});
Expand All @@ -205,7 +206,7 @@ function setGroupPositionsInGroupMode(gd, pa, sa, calcTraces, opts) {

// relative-stack bars within the same trace that would otherwise
// be hidden
unhideBarsWithinTrace(sieve);
unhideBarsWithinTrace(sieve, pa);

// set bar bases and sizes, and update size axis
if(opts.norm) {
Expand All @@ -218,6 +219,7 @@ function setGroupPositionsInGroupMode(gd, pa, sa, calcTraces, opts) {

function setGroupPositionsInStackOrRelativeMode(gd, pa, sa, calcTraces, opts) {
var sieve = new Sieve(calcTraces, {
posAxis: pa,
sepNegVal: opts.mode === 'relative',
overlapNoMerge: !(opts.norm || opts.mode === 'stack' || opts.mode === 'relative')
});
Expand Down Expand Up @@ -610,7 +612,7 @@ function sieveBars(sieve) {
}
}

function unhideBarsWithinTrace(sieve) {
function unhideBarsWithinTrace(sieve, pa) {
var calcTraces = sieve.traces;

for(var i = 0; i < calcTraces.length; i++) {
Expand All @@ -619,6 +621,7 @@ function unhideBarsWithinTrace(sieve) {

if(fullTrace.base === undefined) {
var inTraceSieve = new Sieve([calcTrace], {
posAxis: pa,
sepNegVal: true,
overlapNoMerge: true
});
Expand Down
9 changes: 6 additions & 3 deletions src/traces/bar/sieve.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ function Sieve(traces, opts) {
}
this.positions = positions;

var dv = distinctVals(positions, {
unitMinDiff: opts.unitMinDiff
});
var dv = distinctVals(positions);

this.distinctPositions = dv.vals;
if(dv.vals.length === 1 && width1 !== Infinity) this.minDiff = width1;
else this.minDiff = Math.min(dv.minDiff, width1);

var type = (opts.posAxis || {}).type;
if(type === 'category' || type === 'multicategory') {
this.minDiff = 1;
}

this.binWidth = this.minDiff;

this.bins = {};
Expand Down
2 changes: 1 addition & 1 deletion src/traces/box/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = function calc(gd, trace) {
var allPosArrays = getPosArrays(trace, posLetter, posAxis, fullLayout[numKey]);
var posArray = allPosArrays[0];
var origPos = allPosArrays[1];
var dv = Lib.distinctVals(posArray);
var dv = Lib.distinctVals(posArray, posAxis);
var posDistinct = dv.vals;
var dPos = dv.minDiff / 2;

Expand Down
7 changes: 4 additions & 3 deletions src/traces/box/cross_trace_calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ function setPositionOffset(traceType, gd, boxList, posAxis) {
if(!pointList.length) return;

// box plots - update dPos based on multiple traces
var boxdv = Lib.distinctVals(pointList, {
unitMinDiff: posAxis.type === 'category' || posAxis.type === 'multicategory'
});
var boxdv = Lib.distinctVals(pointList);
if(posAxis.type === 'category' || posAxis.type === 'multicategory') {
boxdv.minDiff = Math.min(1, boxdv.minDiff);
}

var dPos0 = boxdv.minDiff / 2;

Expand Down
16 changes: 14 additions & 2 deletions test/jasmine/tests/bar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -981,12 +981,10 @@ describe('Bar.crossTraceCalc (formerly known as setPositions)', function() {

it('should set unit width for categories in overlay mode', function() {
var gd = mockBarPlot([{
type: 'bar',
x: ['a', 'b', 'c'],
y: [2, 2, 2]
},
{
type: 'bar',
x: ['a', 'c'],
y: [1, 1]
}], {
Expand All @@ -996,6 +994,20 @@ describe('Bar.crossTraceCalc (formerly known as setPositions)', function() {
expect(gd.calcdata[1][0].t.bardelta).toBe(1);
});

it('should set unit width for categories case of missing data for defined category', function() {
var gd = mockBarPlot([{
x: ['a', 'c'],
y: [1, 2]
}, {
x: ['a', 'c'],
y: [1, 2],
}], {
xaxis: { categoryarray: ['a', 'b', 'c'] }
});

expect(gd.calcdata[1][0].t.bardelta).toBe(1);
});

describe('should relative-stack bar within the same trace that overlap under barmode=group', function() {
it('- base case', function() {
var gd = mockBarPlot([{
Expand Down