-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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 6 commits
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 |
---|---|---|
|
@@ -25,6 +25,13 @@ module.exports = function style(s, gd) { | |
var layers = Lib.ensureSingle(traceGroup, 'g', 'layers'); | ||
layers.style('opacity', d[0].trace.opacity); | ||
|
||
// Marker vertical alignment | ||
var valignFactor = 0; | ||
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)); | ||
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. Nice use of |
||
if(!isNaN(markerOffsetY)) layers.attr('transform', 'translate(0,' + markerOffsetY + ')'); | ||
|
||
var fill = layers | ||
.selectAll('g.legendfill') | ||
.data([d]); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"data": [{ | ||
"y": [1, 5, 3, 4, 5], | ||
"name": "Super long name<br>Super long name<br>Super long name<br>Super long name", | ||
"type": "scatter", | ||
"showlegend": true | ||
}, | ||
{ | ||
"y": [3, 2, 5, 1, 5], | ||
"name": "Also super long name<br>Also super long name<br>Also super long name", | ||
"type": "scatter", | ||
"showlegend": true | ||
} | ||
], | ||
"layout": { | ||
"width": 800, | ||
"legend": { | ||
"bgcolor": "rgba(0,255,255,1)", | ||
"valign": "middle", | ||
"font": { | ||
"size": 20 | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"data": [{ | ||
"y": [1, 5, 3, 4, 5], | ||
"name": "Super long name<br>Super long name<br>Super long name<br>Super long name", | ||
"type": "scatter", | ||
"showlegend": true | ||
}, | ||
{ | ||
"y": [3, 2, 5, 1, 5], | ||
"name": "Also super long name<br>Also super long name<br>Also super long name", | ||
"type": "scatter", | ||
"showlegend": true | ||
} | ||
], | ||
"layout": { | ||
"width": 800, | ||
"legend": { | ||
"bgcolor": "rgba(0,255,255,1)", | ||
"valign": "top", | ||
"font": { | ||
"size": 20 | ||
} | ||
} | ||
} | ||
} |
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.
... and let's add one jasmine test 🔒 ing down
relayout
for'valign'
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 catch! Thank you so much! Test can be found in a375c99