-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Legend Marker Vertical Alignment #3263
Changes from 1 commit
407c361
8e2d575
33cf9c3
c533683
a02910a
a375c99
c1b871d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ module.exports = function style(s, gd) { | |
if(gd._fullLayout.legend.valign === 'top') valignFactor = 1.0; | ||
if(gd._fullLayout.legend.valign === 'bottom') valignFactor = -1.0; | ||
var markerOffsetY = valignFactor * (0.5 * (d[0].lineHeight - d[0].height + 3)); | ||
if(markerOffsetY) layers.attr('transform', 'translate(0,' + markerOffsetY + ')'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix bug when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. But There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the mocks were rendered properly because the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
OK this makes sense. I don't understand how the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't something like var valign = gd._fullLayout.valign;
if(valign === 'middle') {
layers.attr('transform', null); // this here is a fun d3 trick to unset DOM attributes
} else {
var factor = {top: 1, bottom: -1}[valign];
var markerOffsetY = factor * (0.5 * (d[0].lineHeight - d[0].height + 3));
layers.attr('transform', 'translate(0,' + markerOffsetY + ')');
} be what we're looking for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your solution would work except that we should check whether There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd have to check to make sure but I think it's called on first draw to figure out how big the legend will be. At this point, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is indeed what is happening which is why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, in this case if we know that var valign = gd._fullLayout.valign;
var lineHeight = d[0].lineHeight;
var height = d[0].height;
if(valign === 'middle' || !lineHeigth || !lheight) {
layers.attr('transform', null); // this here is a fun d3 trick to unset DOM attributes
} else {
var factor = {top: 1, bottom: -1}[valign];
var markerOffsetY = factor * (0.5 * lineHeight - height + 3));
layers.attr('transform', 'translate(0,' + markerOffsetY + ')');
} would make that clearer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In commit c1b871d, I reused most of the above 😄 ! |
||
if(!isNaN(markerOffsetY)) layers.attr('transform', 'translate(0,' + markerOffsetY + ')'); | ||
|
||
var fill = layers | ||
.selectAll('g.legendfill') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -665,6 +665,43 @@ describe('legend relayout update', function() { | |
.catch(failTest) | ||
.then(done); | ||
}); | ||
|
||
describe('should update legend valign', function() { | ||
var mock = require('@mocks/legend_valign_top.json'); | ||
var gd; | ||
|
||
beforeEach(function() { | ||
gd = createGraphDiv(); | ||
}); | ||
afterEach(destroyGraphDiv); | ||
|
||
function markerOffsetY() { | ||
var translateY = d3.select('.legend .traces .layers').attr('transform').match(/translate\(\d+,(-?\d+)\)/)[1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just noticed this. Using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! 🔍 Done in c1b871d! |
||
return parseFloat(translateY); | ||
} | ||
|
||
it('it should translate markers', function(done) { | ||
var mockCopy = Lib.extendDeep({}, mock); | ||
|
||
var top, middle, bottom; | ||
Plotly.plot(gd, mockCopy.data, mockCopy.layout) | ||
.then(function() { | ||
top = markerOffsetY(); | ||
return Plotly.relayout(gd, 'legend.valign', 'middle'); | ||
}) | ||
.then(function() { | ||
middle = markerOffsetY(); | ||
expect(middle).toBeGreaterThan(top); | ||
return Plotly.relayout(gd, 'legend.valign', 'bottom'); | ||
}) | ||
.then(function() { | ||
bottom = markerOffsetY(); | ||
expect(bottom).toBeGreaterThan(middle); | ||
}) | ||
.catch(failTest) | ||
.then(done); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('legend orientation change:', function() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice use of
lineHeight
🎉