Skip to content

Commit 0620416

Browse files
authored
Merge pull request #1142 from n-riesco/pr-20161114-bar-fixes
Bar fixes
2 parents 7dd96f0 + f4ac3a4 commit 0620416

File tree

5 files changed

+55
-17
lines changed

5 files changed

+55
-17
lines changed

src/traces/bar/hover.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
6767

6868
if(trace.orientation === 'h') {
6969
pointData.x0 = pointData.x1 = xa.c2p(di.x, true);
70-
pointData.xLabelVal = di.s;
70+
pointData.xLabelVal = di.b + di.s;
7171

7272
pointData.y0 = ya.c2p(barPos(di) - barDelta, true);
7373
pointData.y1 = ya.c2p(barPos(di) + barDelta, true);
7474
pointData.yLabelVal = di.p;
7575
}
7676
else {
7777
pointData.y0 = pointData.y1 = ya.c2p(di.y, true);
78-
pointData.yLabelVal = di.s;
78+
pointData.yLabelVal = di.b + di.s;
7979

8080
pointData.x0 = xa.c2p(barPos(di) - barDelta, true);
8181
pointData.x1 = xa.c2p(barPos(di) + barDelta, true);

src/traces/bar/set_positions.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,8 @@ function stackBars(gd, sa, sieve) {
485485
if(!isNumeric(bar.s)) continue;
486486

487487
// stack current bar and get previous sum
488-
var barBase = sieve.put(bar.p, bar.s),
489-
barTop = barBase + bar.s;
488+
var barBase = sieve.put(bar.p, bar.b + bar.s),
489+
barTop = barBase + bar.b + bar.s;
490490

491491
// store the bar base and top in each calcdata item
492492
bar.b = barBase;
@@ -524,7 +524,7 @@ function sieveBars(gd, sa, sieve) {
524524
for(var j = 0; j < trace.length; j++) {
525525
var bar = trace[j];
526526

527-
if(isNumeric(bar.s)) sieve.put(bar.p, bar.s);
527+
if(isNumeric(bar.s)) sieve.put(bar.p, bar.b + bar.s);
528528
}
529529
}
530530
}
-770 Bytes
Loading
+5-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
22
"data":[
33
{
4-
"base":4,
5-
"x":[3,2,1,0],
4+
"base":[100,40,25,10],
5+
"x":[-50,10,25,40],
66
"type":"bar"
77
}, {
8-
"base":[7,6,5,4],
9-
"x":[1,2,3,4],
8+
"base":[0,60,75,90],
9+
"x":[50,-10,-25,-40],
1010
"type":"bar"
1111
}
1212
],
1313
"layout":{
1414
"height":400,
1515
"width":400,
1616
"barmode":"group",
17-
"barnorm":"percent"
17+
"barnorm":"fraction"
1818
}
1919
}

test/jasmine/tests/bar_test.js

+45-7
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,27 @@ describe('Bar.setPositions', function() {
453453
assertPointField(cd, 'y', [[0.75, 0.50, 0.25], [0.25, 0.50, 0.75]]);
454454
});
455455

456+
it('should honor barnorm (group+base case)', function() {
457+
var gd = mockBarPlot([{
458+
base: [3, 2, 1],
459+
y: [0, 0, 0]
460+
}, {
461+
y: [1, 2, 3]
462+
}], {
463+
bargap: 0,
464+
barmode: 'group',
465+
barnorm: 'fraction'
466+
});
467+
468+
expect(gd._fullLayout.barnorm).toBe('fraction');
469+
470+
var cd = gd.calcdata;
471+
assertPointField(cd, 'b', [[0.75, 0.50, 0.25], [0, 0, 0]]);
472+
assertPointField(cd, 's', [[0, 0, 0], [0.25, 0.50, 0.75]]);
473+
assertPointField(cd, 'x', [[-0.25, 0.75, 1.75], [0.25, 1.25, 2.25]]);
474+
assertPointField(cd, 'y', [[0.75, 0.50, 0.25], [0.25, 0.50, 0.75]]);
475+
});
476+
456477
it('should honor barnorm (stack case)', function() {
457478
var gd = mockBarPlot([{
458479
y: [3, 2, 1]
@@ -753,17 +774,34 @@ describe('bar hover', function() {
753774
});
754775

755776
it('should return the correct hover point data (case y)', function() {
756-
var out = _hover(gd, 185.645, 0.15, 'y');
777+
var out = _hover(gd, 0.75, 0.15, 'y'),
778+
subplot = gd._fullLayout._plots.xy,
779+
xa = subplot.xaxis,
780+
ya = subplot.yaxis,
781+
barDelta = 1 * 0.8 / 2,
782+
x0 = xa.c2p(0.5, true),
783+
x1 = x0,
784+
y0 = ya.c2p(0 - barDelta, true),
785+
y1 = ya.c2p(0 + barDelta, true);
757786

758-
expect(out.style).toEqual([0, '#1f77b4', 75, 0]);
759-
assertPos(out.pos, [182.88, 182.88, 214.5, 170.5]);
787+
expect(out.style).toEqual([0, '#1f77b4', 0.5, 0]);
788+
assertPos(out.pos, [x0, x1, y0, y1]);
760789
});
761790

762791
it('should return the correct hover point data (case closest)', function() {
763-
var out = _hover(gd, 135.88, -0.15, 'closest');
764-
765-
expect(out.style).toEqual([0, '#1f77b4', 75, 0]);
766-
assertPos(out.pos, [182.88, 182.88, 214.5, 192.5]);
792+
var out = _hover(gd, 0.75, -0.15, 'closest'),
793+
subplot = gd._fullLayout._plots.xy,
794+
xa = subplot.xaxis,
795+
ya = subplot.yaxis,
796+
barDelta = 1 * 0.8 / 2 / 2,
797+
barPos = 0 - 1 * 0.8 / 2 + barDelta,
798+
x0 = xa.c2p(0.5, true),
799+
x1 = x0,
800+
y0 = ya.c2p(barPos - barDelta, true),
801+
y1 = ya.c2p(barPos + barDelta, true);
802+
803+
expect(out.style).toEqual([0, '#1f77b4', 0.5, 0]);
804+
assertPos(out.pos, [x0, x1, y0, y1]);
767805
});
768806
});
769807

0 commit comments

Comments
 (0)