Skip to content

Commit d02c76b

Browse files
committed
base fractional colorbar size on post-expanded-margin plot size
and fix up size mode changes, along with a test that *actually* tests this instead of just looking like it tested it...
1 parent bb6c720 commit d02c76b

File tree

3 files changed

+86
-34
lines changed

3 files changed

+86
-34
lines changed

src/components/colorbar/draw.js

+37-13
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ var Drawing = require('../drawing');
2323
var Color = require('../color');
2424
var Titles = require('../titles');
2525
var svgTextUtils = require('../../lib/svg_text_utils');
26-
var LINE_SPACING = require('../../constants/alignment').LINE_SPACING;
26+
var alignmentConstants = require('../../constants/alignment');
27+
var LINE_SPACING = alignmentConstants.LINE_SPACING;
28+
var FROM_TL = alignmentConstants.FROM_TL;
29+
var FROM_BR = alignmentConstants.FROM_BR;
2730

2831
var handleAxisDefaults = require('../../plots/cartesian/axis_defaults');
2932
var handleAxisPositionDefaults = require('../../plots/cartesian/position_defaults');
@@ -122,13 +125,13 @@ module.exports = function draw(gd, id) {
122125
// when the colorbar itself is pushing the margins.
123126
// but then the fractional size is calculated based on the
124127
// actual graph size, so that the axes will size correctly.
125-
var originalPlotHeight = fullLayout.height - fullLayout.margin.t - fullLayout.margin.b,
126-
originalPlotWidth = fullLayout.width - fullLayout.margin.l - fullLayout.margin.r,
128+
var plotHeight = gs.h,
129+
plotWidth = gs.w,
127130
thickPx = Math.round(opts.thickness *
128-
(opts.thicknessmode === 'fraction' ? originalPlotWidth : 1)),
131+
(opts.thicknessmode === 'fraction' ? plotWidth : 1)),
129132
thickFrac = thickPx / gs.w,
130133
lenPx = Math.round(opts.len *
131-
(opts.lenmode === 'fraction' ? originalPlotHeight : 1)),
134+
(opts.lenmode === 'fraction' ? plotHeight : 1)),
132135
lenFrac = lenPx / gs.h,
133136
xpadFrac = opts.xpad / gs.w,
134137
yExtraPx = (opts.borderwidth + opts.outlinewidth) / 2,
@@ -537,14 +540,35 @@ module.exports = function draw(gd, id) {
537540
'translate(' + (gs.l - xoffset) + ',' + gs.t + ')');
538541

539542
// auto margin adjustment
540-
Plots.autoMargin(gd, id, {
541-
x: opts.x,
542-
y: opts.y,
543-
l: outerwidth * ({right: 1, center: 0.5}[opts.xanchor] || 0),
544-
r: outerwidth * ({left: 1, center: 0.5}[opts.xanchor] || 0),
545-
t: outerheight * ({bottom: 1, middle: 0.5}[opts.yanchor] || 0),
546-
b: outerheight * ({top: 1, middle: 0.5}[opts.yanchor] || 0)
547-
});
543+
var marginOpts = {};
544+
var tFrac = FROM_TL[opts.yanchor];
545+
var bFrac = FROM_BR[opts.yanchor];
546+
if(opts.lenmode === 'pixels') {
547+
marginOpts.y = opts.y;
548+
marginOpts.t = outerheight * tFrac;
549+
marginOpts.b = outerheight * bFrac;
550+
}
551+
else {
552+
marginOpts.t = marginOpts.b = 0; // TODO - not zero?
553+
marginOpts.yt = opts.y + opts.len * tFrac;
554+
marginOpts.yb = opts.y - opts.len * bFrac;
555+
}
556+
557+
var lFrac = FROM_TL[opts.xanchor];
558+
var rFrac = FROM_BR[opts.xanchor];
559+
if(opts.thicknessmode === 'pixels') {
560+
marginOpts.x = opts.x;
561+
marginOpts.l = outerwidth * lFrac;
562+
marginOpts.r = outerwidth * rFrac;
563+
}
564+
else {
565+
var extraThickness = outerwidth - thickPx;
566+
marginOpts.l = extraThickness * lFrac;
567+
marginOpts.r = extraThickness * rFrac;
568+
marginOpts.xl = opts.x - opts.thickness * lFrac;
569+
marginOpts.xr = opts.x + opts.thickness * rFrac;
570+
}
571+
Plots.autoMargin(gd, id, marginOpts);
548572
}
549573

550574
var cbDone = Lib.syncOrAsync([

src/plot_api/plot_api.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,8 @@ function _restyle(gd, aobj, traces) {
14531453
var finalPart = param.parts[param.parts.length - 1];
14541454
var prefix = ai.substr(0, ai.length - finalPart.length - 1);
14551455
var prefixDot = prefix ? prefix + '.' : '';
1456-
var innerContFull = prefix ? Lib.nestedProperty(contFull, prefix) : contFull;
1456+
var innerContFull = prefix ?
1457+
Lib.nestedProperty(contFull, prefix).get() : contFull;
14571458

14581459
valObject = PlotSchema.getTraceValObject(contFull, param.parts);
14591460

@@ -1473,19 +1474,16 @@ function _restyle(gd, aobj, traces) {
14731474
(newVal === 'fraction' || newVal === 'pixels') &&
14741475
innerContFull
14751476
) {
1477+
var gs = fullLayout._size;
1478+
var orient = innerContFull.orient;
1479+
var topOrBottom = (orient === 'top') || (orient === 'bottom');
14761480
if(finalPart === 'thicknessmode') {
1477-
var thicknorm =
1478-
['top', 'bottom'].indexOf(innerContFull.orient) !== -1 ?
1479-
(fullLayout.height - fullLayout.margin.t - fullLayout.margin.b) :
1480-
(fullLayout.width - fullLayout.margin.l - fullLayout.margin.r);
1481+
var thicknorm = topOrBottom ? gs.h : gs.w;
14811482
doextra(prefixDot + 'thickness', innerContFull.thickness *
14821483
(newVal === 'fraction' ? 1 / thicknorm : thicknorm), i);
14831484
}
14841485
else {
1485-
var lennorm =
1486-
['top', 'bottom'].indexOf(innerContFull.orient) !== -1 ?
1487-
(fullLayout.width - fullLayout.margin.l - fullLayout.margin.r) :
1488-
(fullLayout.height - fullLayout.margin.t - fullLayout.margin.b);
1486+
var lennorm = topOrBottom ? gs.w : gs.h;
14891487
doextra(prefixDot + 'len', innerContFull.len *
14901488
(newVal === 'fraction' ? 1 / lennorm : lennorm), i);
14911489
}

test/jasmine/tests/colorbar_test.js

+42-12
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,33 @@ describe('Test colorbar:', function() {
6464

6565
// let heatmap stand in for all traces with trace.{showscale, colorbar}
6666
// also test impliedEdits for colorbars at the trace root
67-
it('can show and hide heatmap colorbars', function(done) {
68-
function assertCB(present, expandedMargin) {
67+
it('can show and hide heatmap colorbars and sizes correctly with automargin', function(done) {
68+
function assertCB(msg, present, expandedMarginR, expandedMarginT, cbTop, cbHeight) {
6969
var colorbars = d3.select(gd).selectAll('.colorbar');
7070
expect(colorbars.size()).toBe(present ? 1 : 0);
7171

72+
var cbbg = colorbars.selectAll('.colorbar .cbbg');
73+
7274
// check that the displayed object has the right size,
7375
// not just that fullLayout._size changed
74-
var heatmap = d3.select(gd).selectAll('.hm image');
75-
expect(heatmap.size()).toBe(1);
76-
var hmWidth = +heatmap.attr('width');
77-
if(expandedMargin) expect(hmWidth).toBeLessThan(400);
78-
else expect(hmWidth).toBe(400);
76+
var plotSizeTest = {};
77+
if(expandedMarginR) plotSizeTest.widthLessThan = 400;
78+
else plotSizeTest.width = 400;
79+
80+
if(expandedMarginT) plotSizeTest.heightLessThan = 400;
81+
else plotSizeTest.height = 400;
82+
83+
assertPlotSize(plotSizeTest);
84+
85+
if(present) {
86+
if(!cbHeight) cbHeight = 400;
87+
var bgHeight = +cbbg.attr('height');
88+
if(expandedMarginT) expect(bgHeight).toBeLessThan(cbHeight - 2);
89+
else expect(bgHeight).toBeWithin(cbHeight, 2);
90+
91+
var topShift = cbbg.node().getBoundingClientRect().top - gd.getBoundingClientRect().top;
92+
expect(topShift).toBeWithin(cbTop, 2);
93+
}
7994
}
8095

8196
var thickPx, lenFrac;
@@ -89,17 +104,32 @@ describe('Test colorbar:', function() {
89104
margin: {l: 50, r: 50, t: 50, b: 50}
90105
})
91106
.then(function() {
92-
assertCB(true, true);
107+
assertCB('initial', true, true, false, 50);
93108

94109
return Plotly.restyle(gd, {showscale: false});
95110
})
96111
.then(function() {
97-
assertCB(false, false);
112+
assertCB('hidden', false, false, false);
98113

99-
return Plotly.restyle(gd, {showscale: true, 'colorbar.x': 0.7});
114+
return Plotly.restyle(gd, {showscale: true, 'colorbar.y': 0.8});
100115
})
101116
.then(function() {
102-
assertCB(true, false);
117+
assertCB('up high', true, true, true, 12);
118+
119+
return Plotly.restyle(gd, {'colorbar.y': 0.7});
120+
})
121+
.then(function() {
122+
assertCB('a little lower', true, true, true, 12);
123+
124+
return Plotly.restyle(gd, {
125+
'colorbar.x': 0.7,
126+
'colorbar.y': 0.5,
127+
'colorbar.thickness': 50,
128+
'colorbar.len': 0.5
129+
});
130+
})
131+
.then(function() {
132+
assertCB('mid-plot', true, false, false, 150, 200);
103133

104134
thickPx = gd._fullData[0].colorbar.thickness;
105135
lenFrac = gd._fullData[0].colorbar.len;
@@ -116,7 +146,7 @@ describe('Test colorbar:', function() {
116146
expect(gd._fullData[0].colorbar.len)
117147
.toBeCloseTo(lenFrac * 400, 3);
118148

119-
assertCB(true, true);
149+
assertCB('changed size modes', true, true, false, 150, 200);
120150
})
121151
.catch(failTest)
122152
.then(done);

0 commit comments

Comments
 (0)