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

Configurable width of the symbol area of legends #5212

Merged
merged 2 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions src/components/legend/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ module.exports = {
'or remain *constant* independent of the symbol size on the graph.'
].join(' ')
},
itemwidth: {
valType: 'number',
dflt: 30,
archmoj marked this conversation as resolved.
Show resolved Hide resolved
role: 'style',
editType: 'legend',
description: 'Sets the width (in px) of the legend item symbols (the part other than the text).',
archmoj marked this conversation as resolved.
Show resolved Hide resolved
},

itemclick: {
valType: 'enumerated',
Expand Down
2 changes: 0 additions & 2 deletions src/components/legend/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ module.exports = {

// number of px between legend title and (left) side of legend (always in x direction and from inner border)
titlePad: 2,
// number of px between legend symbol and legend text (always in x direction)
textGap: 40,
// number of px between each legend item (x and/or y direction)
itemGap: 5
};
1 change: 1 addition & 0 deletions src/components/legend/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ module.exports = function legendDefaults(layoutIn, layoutOut, fullData) {
if(helpers.isGrouped(layoutOut.legend)) coerce('tracegroupgap');

coerce('itemsizing');
coerce('itemwidth');

coerce('itemclick');
coerce('itemdoubleclick');
Expand Down
8 changes: 5 additions & 3 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ function drawTexts(g, gd, opts) {
.call(Drawing.font, opts.font)
.text(isEditable ? ensureLength(name, maxNameLength) : name);

svgTextUtils.positionText(textEl, constants.textGap, 0);
var textGap = opts.itemwidth + constants.itemGap * 2;
svgTextUtils.positionText(textEl, textGap, 0);

if(isEditable) {
textEl.call(svgTextUtils.makeEditable, {gd: gd, text: name})
Expand Down Expand Up @@ -539,7 +540,8 @@ function computeTextDimensions(g, gd, opts) {
// to avoid getBoundingClientRect
var textY = lineHeight * ((textLines - 1) / 2 - 0.3);
if(legendItem) {
svgTextUtils.positionText(textEl, constants.textGap, -textY);
var textGap = opts.itemwidth + constants.itemGap * 2;
svgTextUtils.positionText(textEl, textGap, -textY);
} else { // case of title
svgTextUtils.positionText(textEl, constants.titlePad + bw, lineHeight + bw);
}
Expand Down Expand Up @@ -592,8 +594,8 @@ function computeLegendDimensions(gd, groups, traces, opts) {

var bw = opts.borderwidth;
var bw2 = 2 * bw;
var textGap = constants.textGap;
var itemGap = constants.itemGap;
var textGap = opts.itemwidth + itemGap * 2;
var endPad = 2 * (bw + itemGap);

var yanchor = getYanchor(opts);
Expand Down
26 changes: 15 additions & 11 deletions src/components/legend/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var subTypes = require('../../traces/scatter/subtypes');
var stylePie = require('../../traces/pie/style_one');
var pieCastOption = require('../../traces/pie/helpers').castOption;

var constants = require('./constants');

var CST_MARKER_SIZE = 12;
var CST_LINE_WIDTH = 5;
var CST_MARKER_LINE_WIDTH = 2;
Expand All @@ -30,6 +32,8 @@ module.exports = function style(s, gd, legend) {
var fullLayout = gd._fullLayout;
if(!legend) legend = fullLayout.legend;
var constantItemSizing = legend.itemsizing === 'constant';
var itemWidth = legend.itemwidth;
var centerPos = (itemWidth + constants.itemGap * 2) / 2;
archmoj marked this conversation as resolved.
Show resolved Hide resolved

var boundLineWidth = function(mlw, cont, max, cst) {
var v;
Expand Down Expand Up @@ -161,7 +165,7 @@ module.exports = function style(s, gd, legend) {
.data(showFill || showGradientFill ? [d] : []);
fill.enter().append('path').classed('js-fill', true);
fill.exit().remove();
fill.attr('d', pathStart + 'h30v6h-30z')
fill.attr('d', pathStart + 'h' + itemWidth + 'v6h-' + itemWidth + 'z')
.call(showFill ? Drawing.fillGroupStyle : fillGradient);

if(showLine || showGradientLine) {
Expand All @@ -181,7 +185,7 @@ module.exports = function style(s, gd, legend) {
// though there *is* no vertical variation in this case.
// so add an invisibly small angle to the line
// This issue (and workaround) exist across (Mac) Chrome, FF, and Safari
line.attr('d', pathStart + (showGradientLine ? 'l30,0.0001' : 'h30'))
line.attr('d', pathStart + (showGradientLine ? 'l' + itemWidth + ',0.0001' : 'h' + itemWidth))
.call(showLine ? Drawing.lineGroupStyle : lineGradient);
}

Expand Down Expand Up @@ -271,7 +275,7 @@ module.exports = function style(s, gd, legend) {
// make sure marker is on the bottom, in case it enters after text
pts.enter().insert('path', ':first-child')
.classed('scatterpts', true)
.attr('transform', 'translate(20,0)');
.attr('transform', 'translate(' + centerPos + ',0)');
pts.exit().remove();
pts.call(Drawing.pointStyle, tMod, gd);

Expand All @@ -283,7 +287,7 @@ module.exports = function style(s, gd, legend) {
.data(showText ? dMod : []);
txt.enter()
.append('g').classed('pointtext', true)
.append('text').attr('transform', 'translate(20,0)');
.append('text').attr('transform', 'translate(' + centerPos + ',0)');
txt.exit().remove();
txt.selectAll('text').call(Drawing.textPointStyle, tMod, gd);
}
Expand Down Expand Up @@ -311,7 +315,7 @@ module.exports = function style(s, gd, legend) {
.selectAll('path.legendwaterfall')
.data(ptsData);
pts.enter().append('path').classed('legendwaterfall', true)
.attr('transform', 'translate(20,0)')
.attr('transform', 'translate(' + centerPos + ',0)')
.style('stroke-miterlimit', 1);
pts.exit().remove();

Expand Down Expand Up @@ -351,7 +355,7 @@ module.exports = function style(s, gd, legend) {
.data(isVisible ? [d] : []);
barpath.enter().append('path').classed('legend' + desiredType, true)
.attr('d', 'M6,6H-6V-6H6Z')
.attr('transform', 'translate(20,0)');
.attr('transform', 'translate(' + centerPos + ',0)');
barpath.exit().remove();

barpath.each(function(d) {
Expand All @@ -375,7 +379,7 @@ module.exports = function style(s, gd, legend) {
pts.enter().append('path').classed('legendbox', true)
// if we want the median bar, prepend M6,0H-6
.attr('d', 'M6,6H-6V-6H6Z')
.attr('transform', 'translate(20,0)');
.attr('transform', 'translate(' + centerPos + ',0)');
pts.exit().remove();

pts.each(function() {
Expand Down Expand Up @@ -415,7 +419,7 @@ module.exports = function style(s, gd, legend) {
if(i) return 'M-15,0H-8M-8,6V-6H8Z'; // increasing
return 'M15,0H8M8,-6V6H-8Z'; // decreasing
})
.attr('transform', 'translate(20,0)')
.attr('transform', 'translate(' + centerPos + ',0)')
.style('stroke-miterlimit', 1);
pts.exit().remove();

Expand All @@ -442,7 +446,7 @@ module.exports = function style(s, gd, legend) {
if(i) return 'M-15,0H0M-8,-6V0'; // increasing
return 'M15,0H0M8,6V0'; // decreasing
})
.attr('transform', 'translate(20,0)')
.attr('transform', 'translate(' + centerPos + ',0)')
.style('stroke-miterlimit', 1);
pts.exit().remove();

Expand Down Expand Up @@ -478,7 +482,7 @@ module.exports = function style(s, gd, legend) {
.data(isVisible ? [d] : []);
pts.enter().append('path').classed('legend' + desiredType, true)
.attr('d', 'M6,6H-6V-6H6Z')
.attr('transform', 'translate(20,0)');
.attr('transform', 'translate(' + centerPos + ',0)');
pts.exit().remove();

if(pts.size()) {
Expand Down Expand Up @@ -576,7 +580,7 @@ module.exports = function style(s, gd, legend) {
.selectAll('path.legend3dandfriends')
.data(ptsData);
pts.enter().append('path').classed('legend3dandfriends', true)
.attr('transform', 'translate(20,0)')
.attr('transform', 'translate(' + centerPos + ',0)')
.style('stroke-miterlimit', 1);
pts.exit().remove();

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions test/image/mocks/legend_itemwidth_dashline.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"data": [
{
"x": [
2,
3,
4,
5
],
"y": [
16,
5,
11,
9
],
"line": {
"dash": "dashdot"
},
"type": "scatter"
},
{
"x": [
1,
2,
3,
4
],
"y": [
12,
9,
15,
12
],
"line": {
"dash": "dash"
},
"type": "scatter"
}
],
"layout": {
"legend": {
"itemwidth": 60
}
}
}
2 changes: 2 additions & 0 deletions test/jasmine/tests/mock_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ var list = [
'legend_horizontal_one_row',
'legend_horizontal_wrap-alll-lines',
'legend_inside',
'legend_itemwidth_dashline',
'legend_labels',
'legend_large_margin',
'legend_margin-autoexpand-false',
Expand Down Expand Up @@ -1733,6 +1734,7 @@ figs['legend_horizontal_groups'] = require('@mocks/legend_horizontal_groups');
figs['legend_horizontal_one_row'] = require('@mocks/legend_horizontal_one_row');
figs['legend_horizontal_wrap-alll-lines'] = require('@mocks/legend_horizontal_wrap-alll-lines');
figs['legend_inside'] = require('@mocks/legend_inside');
figs['legend_itemwidth_dashline'] = require('@mocks/legend_itemwidth_dashline');
figs['legend_labels'] = require('@mocks/legend_labels');
figs['legend_large_margin'] = require('@mocks/legend_large_margin');
figs['legend_margin-autoexpand-false'] = require('@mocks/legend_margin-autoexpand-false');
Expand Down