Skip to content

Commit c5ddc28

Browse files
committed
[poc] colorbar split pushMargin from draw
1 parent a6cf612 commit c5ddc28

File tree

2 files changed

+83
-50
lines changed

2 files changed

+83
-50
lines changed

src/components/colorbar/draw.js

Lines changed: 79 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,7 @@ function draw(gd) {
4848

4949
colorBars.each(function(opts) {
5050
var g = d3.select(this);
51-
52-
Lib.ensureSingle(g, 'rect', cn.cbbg);
53-
Lib.ensureSingle(g, 'g', cn.cbfills);
54-
Lib.ensureSingle(g, 'g', cn.cblines);
55-
Lib.ensureSingle(g, 'g', cn.cbaxis, function(s) { s.classed(cn.crisp, true); });
56-
Lib.ensureSingle(g, 'g', cn.cbtitleunshift, function(s) { s.append('g').classed(cn.cbtitle, true); });
57-
Lib.ensureSingle(g, 'rect', cn.cboutline);
51+
makeFramework(g);
5852

5953
var done = drawColorBar(g, opts, gd);
6054
if(done && done.then) (gd._promises || []).push(done);
@@ -64,9 +58,7 @@ function draw(gd) {
6458
}
6559
});
6660

67-
colorBars.exit()
68-
.each(function(opts) { Plots.autoMargin(gd, opts._id); })
69-
.remove();
61+
colorBars.exit().remove();
7062

7163
colorBars.order();
7264
}
@@ -173,6 +165,15 @@ function makeColorBarData(gd) {
173165
return out;
174166
}
175167

168+
function makeFramework(g) {
169+
Lib.ensureSingle(g, 'rect', cn.cbbg);
170+
Lib.ensureSingle(g, 'g', cn.cbfills);
171+
Lib.ensureSingle(g, 'g', cn.cblines);
172+
Lib.ensureSingle(g, 'g', cn.cbaxis, function(s) { s.classed(cn.crisp, true); });
173+
Lib.ensureSingle(g, 'g', cn.cbtitleunshift, function(s) { s.append('g').classed(cn.cbtitle, true); });
174+
Lib.ensureSingle(g, 'rect', cn.cboutline);
175+
}
176+
176177
function drawColorBar(g, opts, gd) {
177178
var fullLayout = gd._fullLayout;
178179
var gs = fullLayout._size;
@@ -420,7 +421,6 @@ function drawColorBar(g, opts, gd) {
420421
// overlap, to prevent antialiasing gaps
421422
z[1] = Lib.constrain(z[1] + (z[1] > z[0]) ? 1 : -1, zBounds[0], zBounds[1]);
422423

423-
424424
// Colorbar cannot currently support opacities so we
425425
// use an opaque fill even when alpha channels present
426426
var fillEl = d3.select(this).attr({
@@ -478,10 +478,6 @@ function drawColorBar(g, opts, gd) {
478478
});
479479
}
480480

481-
// wait for the axis & title to finish rendering before
482-
// continuing positioning
483-
// TODO: why are we redrawing multiple times now with this?
484-
// I guess autoMargin doesn't like being post-promise?
485481
function positionCB() {
486482
var innerWidth = thickPx + opts.outlinewidth / 2 + Drawing.bBox(axLayer.node()).width;
487483
titleEl = titleCont.select('text');
@@ -501,8 +497,8 @@ function drawColorBar(g, opts, gd) {
501497
innerWidth = Math.max(innerWidth, titleWidth);
502498
}
503499

504-
var outerwidth = 2 * opts.xpad + innerWidth + opts.borderwidth + opts.outlinewidth / 2;
505-
var outerheight = yBottomPx - yTopPx;
500+
var outerwidth = opts._outerwidth = 2 * opts.xpad + innerWidth + opts.borderwidth + opts.outlinewidth / 2;
501+
var outerheight = opts._outerheight = yBottomPx - yTopPx;
506502

507503
g.select('.' + cn.cbbg).attr({
508504
x: xLeft - opts.xpad - (opts.borderwidth + opts.outlinewidth) / 2,
@@ -529,36 +525,6 @@ function drawColorBar(g, opts, gd) {
529525
// fix positioning for xanchor!='left'
530526
var xoffset = ({center: 0.5, right: 1}[opts.xanchor] || 0) * outerwidth;
531527
g.attr('transform', 'translate(' + (gs.l - xoffset) + ',' + gs.t + ')');
532-
533-
// auto margin adjustment
534-
var marginOpts = {};
535-
var tFrac = FROM_TL[opts.yanchor];
536-
var bFrac = FROM_BR[opts.yanchor];
537-
if(opts.lenmode === 'pixels') {
538-
marginOpts.y = opts.y;
539-
marginOpts.t = outerheight * tFrac;
540-
marginOpts.b = outerheight * bFrac;
541-
} else {
542-
marginOpts.t = marginOpts.b = 0;
543-
marginOpts.yt = opts.y + opts.len * tFrac;
544-
marginOpts.yb = opts.y - opts.len * bFrac;
545-
}
546-
547-
var lFrac = FROM_TL[opts.xanchor];
548-
var rFrac = FROM_BR[opts.xanchor];
549-
if(opts.thicknessmode === 'pixels') {
550-
marginOpts.x = opts.x;
551-
marginOpts.l = outerwidth * lFrac;
552-
marginOpts.r = outerwidth * rFrac;
553-
} else {
554-
var extraThickness = outerwidth - thickPx;
555-
marginOpts.l = extraThickness * lFrac;
556-
marginOpts.r = extraThickness * rFrac;
557-
marginOpts.xl = opts.x - opts.thickness * lFrac;
558-
marginOpts.xr = opts.x + opts.thickness * rFrac;
559-
}
560-
561-
Plots.autoMargin(gd, opts._id, marginOpts);
562528
}
563529

564530
return Lib.syncOrAsync([
@@ -719,6 +685,71 @@ function mockColorBarAxis(gd, opts, zrange) {
719685
return cbAxisOut;
720686
}
721687

688+
function pushMargin(gd) {
689+
var fullLayout = gd._fullLayout;
690+
var colorBarData = makeColorBarData(gd);
691+
var gs = fullLayout._size;
692+
693+
var fakeColorBars = Drawing.tester.selectAll('g.' + cn.colorbar)
694+
.data(colorBarData);
695+
696+
fakeColorBars.enter().append('g')
697+
.classed(cn.colorbar, true);
698+
699+
fakeColorBars.each(function(opts) {
700+
var g = d3.select(this);
701+
makeFramework(g);
702+
703+
var done = drawColorBar(g, opts, gd);
704+
if(done && done.then) (gd._promises || []).push(done);
705+
});
706+
707+
return Lib.syncOrAsync([
708+
Plots.previousPromises,
709+
function() {
710+
fakeColorBars.remove();
711+
712+
for(var i = 0; i < colorBarData.length; i++) {
713+
var opts = colorBarData[i];
714+
var outerwidth = opts._outerwidth;
715+
var outerheight = opts._outerheight;
716+
var tFrac = FROM_TL[opts.yanchor];
717+
var bFrac = FROM_BR[opts.yanchor];
718+
719+
var marginOpts = {};
720+
721+
if(opts.lenmode === 'pixels') {
722+
marginOpts.y = opts.y;
723+
marginOpts.t = outerheight * tFrac;
724+
marginOpts.b = outerheight * bFrac;
725+
} else {
726+
marginOpts.t = marginOpts.b = 0;
727+
marginOpts.yt = opts.y + opts.len * tFrac;
728+
marginOpts.yb = opts.y - opts.len * bFrac;
729+
}
730+
731+
var lFrac = FROM_TL[opts.xanchor];
732+
var rFrac = FROM_BR[opts.xanchor];
733+
if(opts.thicknessmode === 'pixels') {
734+
marginOpts.x = opts.x;
735+
marginOpts.l = outerwidth * lFrac;
736+
marginOpts.r = outerwidth * rFrac;
737+
} else {
738+
var thickPx = Math.round(opts.thickness * gs.w);
739+
var extraThickness = outerwidth - thickPx;
740+
marginOpts.l = extraThickness * lFrac;
741+
marginOpts.r = extraThickness * rFrac;
742+
marginOpts.xl = opts.x - opts.thickness * lFrac;
743+
marginOpts.xr = opts.x + opts.thickness * rFrac;
744+
}
745+
746+
Plots.autoMargin(gd, opts._id, marginOpts);
747+
}
748+
}
749+
], gd);
750+
}
751+
722752
module.exports = {
723-
draw: draw
753+
draw: draw,
754+
pushMargin: pushMargin
724755
};

src/components/colorbar/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ module.exports = {
1212
moduleType: 'component',
1313
name: 'colorbar',
1414

15+
hasColorbar: require('./has_colorbar'),
16+
1517
attributes: require('./attributes'),
1618
supplyDefaults: require('./defaults'),
1719

18-
draw: require('./draw').draw,
19-
hasColorbar: require('./has_colorbar')
20+
pushMargin: require('./draw').pushMargin,
21+
draw: require('./draw').draw
2022
};

0 commit comments

Comments
 (0)