Skip to content
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

Merged
merged 7 commits into from
Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/components/legend/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,15 @@ module.exports = {
'or *bottom* of the legend.'
].join(' ')
},
editType: 'legend'
editType: 'legend',
valign: {
valType: 'enumerated',
values: ['top', 'middle', 'bottom'],
dflt: 'middle',
role: 'style',
Copy link
Contributor

@etpinard etpinard Nov 20, 2018

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'

Copy link
Contributor Author

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

editType: 'legend',
description: [
'Sets the vertical alignment of the symbols with respect to their associated text.',
].join(' ')
}
};
1 change: 1 addition & 0 deletions src/components/legend/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,6 @@ module.exports = function legendDefaults(layoutIn, layoutOut, fullData) {
coerce('xanchor', defaultXAnchor);
coerce('y', defaultY);
coerce('yanchor', defaultYAnchor);
coerce('valign');
Lib.noneOrAll(containerIn, containerOut, ['x', 'y']);
};
1 change: 1 addition & 0 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ function computeTextDimensions(g, gd) {
// to avoid getBoundingClientRect
var textY = lineHeight * (0.3 + (1 - textLines) / 2);
svgTextUtils.positionText(text, constants.textOffsetX, textY);
legendItem.lineHeight = lineHeight;
}

height = Math.max(height, 16) + 3;
Expand Down
7 changes: 7 additions & 0 deletions src/components/legend/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Contributor

@etpinard etpinard Nov 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of lineHeight 🎉

if(!isNaN(markerOffsetY)) layers.attr('transform', 'translate(0,' + markerOffsetY + ')');

var fill = layers
.selectAll('g.legendfill')
.data([d]);
Expand Down
Binary file added test/image/baselines/legend_valign_middle.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 added test/image/baselines/legend_valign_top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions test/image/mocks/legend_valign_middle.json
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
}
}
}
}
25 changes: 25 additions & 0 deletions test/image/mocks/legend_valign_top.json
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
}
}
}
}
37 changes: 37 additions & 0 deletions test/jasmine/tests/legend_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed this. Using Drawing.getTranslate would be cleaner.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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() {
Expand Down