Skip to content

Commit 59cc28e

Browse files
authored
Merge pull request #4186 from plotly/bar-obj-constancy
bar transitions: support object constancy
2 parents 4a01011 + 12f7337 commit 59cc28e

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

Diff for: src/traces/bar/plot.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ var appendArrayPointValue = require('../../components/fx/helpers').appendArrayPo
3131
// padding in pixels around text
3232
var TEXTPAD = 3;
3333

34+
function keyFunc(d) {return d.id;}
35+
function getKeyFunc(trace) {
36+
if(trace.ids) {
37+
return keyFunc;
38+
}
39+
}
40+
3441
function dirSign(a, b) {
3542
return (a < b) ? 1 : -1;
3643
}
@@ -103,7 +110,8 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
103110

104111
var pointGroup = Lib.ensureSingle(plotGroup, 'g', 'points');
105112

106-
var bars = pointGroup.selectAll('g.point').data(Lib.identity);
113+
var keyFunc = getKeyFunc(trace);
114+
var bars = pointGroup.selectAll('g.point').data(Lib.identity, keyFunc);
107115

108116
bars.enter().append('g')
109117
.classed('point', true);

Diff for: test/image/mocks/animation_bar.json

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"type": "bar",
44
"x": ["A", "B", "C"],
55
"y": [24, 5, 8],
6+
"ids": ["A", "B", "C"],
67
"error_y": {"array": [3, 2, 1]}
78
}],
89
"layout": {
@@ -17,6 +18,7 @@
1718
{"data": [{"width": 0.25}]},
1819
{"data": [{"marker": {"line": {"width": 10}}}]},
1920
{"data": [{"marker": {"line": {"color": ["orange", "yellow", "blue"]}}}]},
21+
{"data": [{"ids": ["B", "C", "A"]}]},
2022
{"layout": {"yaxis": {"range": [0, 20]}}}
2123
]
2224
}

Diff for: test/jasmine/assets/check_transitions.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ function assert(test) {
8989
var cur = [];
9090
d3.selectAll(test[1]).each(function(d, i) {
9191
if(test[2] === 'style') cur[i] = this.style[test[3]];
92-
if(test[2] === 'attr') cur[i] = d3.select(this).attr(test[3]);
92+
else if(test[2] === 'attr') cur[i] = d3.select(this).attr(test[3]);
93+
else if(test[2] === 'datum') {
94+
cur[i] = d3.select(this).datum()[test[3]];
95+
}
9396
});
9497
switch(test[3]) {
9598
case 'd':

Diff for: test/jasmine/tests/bar_test.js

+41
Original file line numberDiff line numberDiff line change
@@ -2683,4 +2683,45 @@ describe('bar tweening', function() {
26832683
.catch(failTest)
26842684
.then(done);
26852685
});
2686+
2687+
it('for bar positions during object-constancy transitions', function(done) {
2688+
var _mock = {
2689+
data: [{
2690+
type: 'bar',
2691+
ids: ['A', 'B', 'C'],
2692+
x: ['A', 'B', 'C'],
2693+
text: ['A', 'B', 'C'],
2694+
textposition: 'inside',
2695+
y: [24, 5, 8],
2696+
error_y: {'array': [3, 2, 1]},
2697+
marker: {color: ['red', 'green', 'blue']}
2698+
}]
2699+
};
2700+
2701+
var nextFrame = { data: [{ ids: ['B', 'C', 'A'] }] };
2702+
2703+
var tests = [
2704+
[0, '.point path', 'datum', 'id', ['A', 'B', 'C']],
2705+
[0, '.point path', 'style', 'fill', ['rgb(255, 0, 0)', 'rgb(0, 128, 0)', 'rgb(0, 0, 255)']],
2706+
[0, '.point path', 'attr', 'd', ['M18,270V42H162V270Z', 'M198,270V222.5H342V270Z', 'M378,270V194H522V270Z']],
2707+
[0, 'text.bartext', 'attr', 'transform', ['translate(90 56)', 'translate(270 236.5)', 'translate(450 208)']],
2708+
[0, 'path.yerror', 'attr', 'd', ['M86,14h8m-4,0V71m-4,0h8', 'M266,204h8m-4,0V242m-4,0h8', 'M446,185h8m-4,0V204m-4,0h8']],
2709+
2710+
[250, '.point path', 'datum', 'id', ['A', 'B', 'C']],
2711+
[250, '.point path', 'style', 'fill', ['rgb(128, 0, 128)', 'rgb(128, 64, 0)', 'rgb(0, 64, 128)']],
2712+
[250, '.point path', 'attr', 'd', ['M198,270V118H342V270Z', 'M108,270V132H252V270Z', 'M288,270V208H432V270Z']],
2713+
[250, 'text.bartext', 'attr', 'transform', ['translate(269.7890625 134)', 'translate(179.5859375 148.25)', 'translate(359.578125 224.25)']],
2714+
[250, 'path.yerror', 'attr', 'd', ['M266,99h8m-4,0V137m-4,0h8', 'M176,109h8m-4,0V156m-4,0h8', 'M356,194h8m-4,0V223m-4,0h8']],
2715+
2716+
[500, '.point path', 'datum', 'id', ['A', 'B', 'C']],
2717+
[500, '.point path', 'style', 'fill', ['rgb(0, 0, 255)', 'rgb(255, 0, 0)', 'rgb(0, 128, 0)']],
2718+
[500, '.point path', 'attr', 'd', ['M378,270V194H522V270Z', 'M18,270V42H162V270Z', 'M198,270V223H342V270Z']],
2719+
[500, 'text.bartext', 'attr', 'transform', ['translate(450 208)', 'translate(90 56)', 'translate(270 236.5)']],
2720+
[500, 'path.yerror', 'attr', 'd', ['M446,185h8m-4,0V204m-4,0h8', 'M86,14h8m-4,0V71m-4,0h8', 'M266,204h8m-4,0V242m-4,0h8']]
2721+
];
2722+
2723+
checkTransition(gd, _mock, nextFrame, transitionOpts, tests)
2724+
.catch(failTest)
2725+
.then(done);
2726+
});
26862727
});

0 commit comments

Comments
 (0)