Skip to content

Commit 0b54b3f

Browse files
authored
Merge pull request #4832 from plotly/react-data-order-matching-axes
Fix react category order of matching axes
2 parents 4d383f2 + c2dc252 commit 0b54b3f

File tree

2 files changed

+133
-3
lines changed

2 files changed

+133
-3
lines changed

src/lib/relink_private.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ module.exports = function relinkPrivateKeys(toContainer, fromContainer) {
2424
var fromVal = fromContainer[k];
2525
var toVal = toContainer[k];
2626

27-
if(toVal === fromVal) {
28-
continue;
29-
}
27+
if(toVal === fromVal) continue;
28+
if(toContainer.matches && k === '_categoriesMap') continue;
29+
3030
if(k.charAt(0) === '_' || typeof fromVal === 'function') {
3131
// if it already exists at this point, it's something
3232
// that we recreate each time around, so ignore it

test/jasmine/tests/axes_test.js

+130
Original file line numberDiff line numberDiff line change
@@ -5431,3 +5431,133 @@ describe('Test template:', function() {
54315431
.then(done);
54325432
});
54335433
});
5434+
5435+
describe('more react tests', function() {
5436+
var gd;
5437+
5438+
beforeEach(function() {
5439+
gd = createGraphDiv();
5440+
});
5441+
5442+
afterEach(destroyGraphDiv);
5443+
5444+
it('should sort catgories on matching axes using react', function(done) {
5445+
var fig = {
5446+
data: [{
5447+
yaxis: 'y',
5448+
xaxis: 'x',
5449+
y: [0, 0],
5450+
x: ['A', 'Z']
5451+
}, {
5452+
yaxis: 'y2',
5453+
xaxis: 'x2',
5454+
y: [0, 0],
5455+
x: ['A', 'Z']
5456+
}],
5457+
layout: {
5458+
width: 400,
5459+
height: 300,
5460+
showlegend: false,
5461+
xaxis: {
5462+
matches: 'x2',
5463+
domain: [ 0, 1]
5464+
},
5465+
yaxis: {
5466+
domain: [0.6, 1],
5467+
anchor: 'x'
5468+
},
5469+
xaxis2: {
5470+
domain: [0, 1],
5471+
anchor: 'y2'
5472+
},
5473+
yaxis2: {
5474+
domain: [0, 0.4],
5475+
anchor: 'x2'
5476+
}
5477+
}
5478+
};
5479+
5480+
Plotly.newPlot(gd, fig)
5481+
.then(function() {
5482+
expect(gd._fullLayout.xaxis._categories).toEqual(['A', 'Z']);
5483+
expect(gd._fullLayout.xaxis2._categories).toEqual(['A', 'Z']);
5484+
expect(gd._fullLayout.xaxis._categoriesMap).toEqual({A: 0, Z: 1});
5485+
expect(gd._fullLayout.xaxis2._categoriesMap).toEqual({A: 0, Z: 1});
5486+
})
5487+
.then(function() {
5488+
// flip order
5489+
fig.data[0].x = ['Z', 'A'];
5490+
fig.data[1].x = ['Z', 'A'];
5491+
5492+
return Plotly.react(gd, fig);
5493+
})
5494+
.then(function() {
5495+
expect(gd._fullLayout.xaxis._categories).toEqual(['Z', 'A']);
5496+
expect(gd._fullLayout.xaxis2._categories).toEqual(['Z', 'A']);
5497+
expect(gd._fullLayout.xaxis._categoriesMap).toEqual({Z: 0, A: 1});
5498+
expect(gd._fullLayout.xaxis2._categoriesMap).toEqual({Z: 0, A: 1});
5499+
})
5500+
.then(function() {
5501+
// should get the same order with newPlot
5502+
return Plotly.newPlot(gd, fig);
5503+
})
5504+
.then(function() {
5505+
expect(gd._fullLayout.xaxis._categories).toEqual(['Z', 'A']);
5506+
expect(gd._fullLayout.xaxis2._categories).toEqual(['Z', 'A']);
5507+
expect(gd._fullLayout.xaxis._categoriesMap).toEqual({Z: 0, A: 1});
5508+
expect(gd._fullLayout.xaxis2._categoriesMap).toEqual({Z: 0, A: 1});
5509+
})
5510+
.then(function() {
5511+
// add new category
5512+
fig.data[0].x = ['Z', 0, 'A'];
5513+
fig.data[1].x = ['Z', 0, 'A'];
5514+
fig.data[0].y = [1, 2, 3];
5515+
fig.data[1].y = [2, 4, 6];
5516+
5517+
return Plotly.react(gd, fig);
5518+
})
5519+
.then(function() {
5520+
expect(gd._fullLayout.xaxis._categories).toEqual(['Z', '0', 'A']);
5521+
expect(gd._fullLayout.xaxis2._categories).toEqual(['Z', '0', 'A']);
5522+
expect(gd._fullLayout.xaxis._categoriesMap).toEqual({Z: 0, 0: 1, A: 2});
5523+
expect(gd._fullLayout.xaxis2._categoriesMap).toEqual({Z: 0, 0: 1, A: 2});
5524+
})
5525+
.then(function() {
5526+
// should get the same order with newPlot
5527+
return Plotly.newPlot(gd, fig);
5528+
})
5529+
.then(function() {
5530+
expect(gd._fullLayout.xaxis._categories).toEqual(['Z', '0', 'A']);
5531+
expect(gd._fullLayout.xaxis2._categories).toEqual(['Z', '0', 'A']);
5532+
expect(gd._fullLayout.xaxis._categoriesMap).toEqual({Z: 0, 0: 1, A: 2});
5533+
expect(gd._fullLayout.xaxis2._categoriesMap).toEqual({Z: 0, 0: 1, A: 2});
5534+
})
5535+
.then(function() {
5536+
// change data
5537+
fig.data[0].x = ['Z', 0, 'A'];
5538+
fig.data[1].x = ['A', 'Z'];
5539+
fig.data[0].y = [3, 2, 1];
5540+
fig.data[1].y = [-1, 0];
5541+
5542+
return Plotly.react(gd, fig);
5543+
})
5544+
.then(function() {
5545+
expect(gd._fullLayout.xaxis._categories).toEqual(['Z', '0', 'A']);
5546+
expect(gd._fullLayout.xaxis2._categories).toEqual(['Z', '0', 'A']);
5547+
expect(gd._fullLayout.xaxis._categoriesMap).toEqual({Z: 0, 0: 1, A: 2});
5548+
expect(gd._fullLayout.xaxis2._categoriesMap).toEqual({Z: 0, 0: 1, A: 2});
5549+
})
5550+
.then(function() {
5551+
// should get the same order with newPlot
5552+
return Plotly.newPlot(gd, fig);
5553+
})
5554+
.then(function() {
5555+
expect(gd._fullLayout.xaxis._categories).toEqual(['Z', '0', 'A']);
5556+
expect(gd._fullLayout.xaxis2._categories).toEqual(['Z', '0', 'A']);
5557+
expect(gd._fullLayout.xaxis._categoriesMap).toEqual({Z: 0, 0: 1, A: 2});
5558+
expect(gd._fullLayout.xaxis2._categoriesMap).toEqual({Z: 0, 0: 1, A: 2});
5559+
})
5560+
.catch(failTest)
5561+
.then(done);
5562+
});
5563+
});

0 commit comments

Comments
 (0)