Skip to content

Commit

Permalink
adjust first and last tick labels for horizontal colorbars
Browse files Browse the repository at this point in the history
 - when there is a border line or different background
  • Loading branch information
archmoj committed Dec 9, 2021
1 parent 8a137e3 commit 2f09b6e
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion src/components/colorbar/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ function drawColorBar(g, opts, gd) {
var thicknessmode = opts.thicknessmode;
var outlinewidth = opts.outlinewidth;
var borderwidth = opts.borderwidth;
var bgcolor = opts.bgcolor;
var xanchor = opts.xanchor;
var yanchor = opts.yanchor;
var xpad = opts.xpad;
Expand Down Expand Up @@ -672,7 +673,7 @@ function drawColorBar(g, opts, gd) {
.attr('y', (isVertical ? vPx : uPx) - (isVertical ? lenPx : ypad + moveY - hColorbarMoveTitle))
.attr(isVertical ? 'width' : 'height', Math.max(outerThickness - hColorbarMoveTitle, 2))
.attr(isVertical ? 'height' : 'width', Math.max(lenPx + extraW, 2))
.call(Color.fill, opts.bgcolor)
.call(Color.fill, bgcolor)
.call(Color.stroke, opts.bordercolor)
.style('stroke-width', borderwidth);

Expand All @@ -697,6 +698,55 @@ function drawColorBar(g, opts, gd) {
gs.t - (isVertical ? 0 : (1 - yRatio) * outerThickness - moveY)
));

if(!isVertical && (
borderwidth || (
tinycolor(bgcolor).getAlpha() &&
!tinycolor.equals(fullLayout.plot_bgcolor, bgcolor)
)
)) {
// for horizontal colorbars when there is a border line or having different background color
// adjust x positioning for the first/last tick labels if they go outside the border
var border = g.select('.' + cn.cbbg).node();
var oBb = Drawing.bBox(border);
var oTr = Drawing.getTranslate(g);

var TEXTPAD = 2;

var tickLabels = axLayer.selectAll('text');
tickLabels.each(function(d, i) {
var first = 0;
var last = tickLabels[0].length - 1;
if(i === first || i === last) {
var iBb = Drawing.bBox(this);
var iTr = Drawing.getTranslate(this);
var deltaX;

if(i === last) {
var iRight = iBb.right + iTr.x;
var oRight = oBb.right + oTr.x + vPx - borderwidth - TEXTPAD + optsX;

deltaX = oRight - iRight;
if(deltaX > 0) deltaX = 0;
}

if(i === first) {
var iLeft = iBb.left + iTr.x;
var oLeft = oBb.left + oTr.x + vPx + borderwidth + TEXTPAD;

deltaX = oLeft - iLeft;
if(deltaX < 0) deltaX = 0;
}

if(deltaX) {
this.setAttribute('transform',
'translate(' + deltaX + ',0) ' +
this.getAttribute('transform')
);
}
}
});
}

// auto margin adjustment
var marginOpts = {};
var lFrac = FROM_TL[xanchor];
Expand Down
Binary file modified test/image/baselines/h-colorbar01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/h-colorbar06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/h-colorbar07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/h-colorbar08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/h-colorbar09.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/h-colorbar10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/h-colorbar11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/h-colorbar12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2f09b6e

Please sign in to comment.