Skip to content

Add title feature to legend and support legend option for various traces #4386

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

Merged
merged 7 commits into from
Dec 2, 2019
Merged
Changes from all 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
33 changes: 33 additions & 0 deletions src/components/legend/attributes.js
Original file line number Diff line number Diff line change
@@ -193,5 +193,38 @@ module.exports = {
'Sets the vertical alignment of the symbols with respect to their associated text.',
].join(' ')
},
title: {
text: {
valType: 'string',
dflt: '',
role: 'info',
editType: 'legend',
description: [
'Sets the title of the legend.'
].join(' ')
},
font: fontAttrs({
editType: 'legend',
description: [
'Sets this legend\'s title font.'
].join(' '),
}),
side: {
valType: 'enumerated',
values: ['top', 'left', 'top left'],
role: 'style',
editType: 'legend',
description: [
'Determines the location of legend\'s title',
'with respect to the legend items.',
'Defaulted to *top* with `orientation` is *h*.',
'Defaulted to *left* with `orientation` is *v*.',
'The *top left* options could be used to expand',
'legend area in both x and y sides.'
].join(' ')
},
editType: 'legend',
},

editType: 'legend'
};
2 changes: 2 additions & 0 deletions src/components/legend/constants.js
Original file line number Diff line number Diff line change
@@ -15,6 +15,8 @@ module.exports = {
scrollBarMargin: 4,
scrollBarEnterAttrs: {rx: 20, ry: 3, width: 0, height: 0},

// 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)
15 changes: 14 additions & 1 deletion src/components/legend/defaults.js
Original file line number Diff line number Diff line change
@@ -33,7 +33,14 @@ module.exports = function legendDefaults(layoutIn, layoutOut, fullData) {
// *would* be shown by default, toward the two traces you need to
// ensure the legend is shown by default, because this can still help
// disambiguate.
if(trace.showlegend || trace._dfltShowLegend) {
if(trace.showlegend || (
trace._dfltShowLegend && !(
trace._module &&
trace._module.attributes &&
trace._module.attributes.showlegend &&
trace._module.attributes.showlegend.dflt === false
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain why we need this extra piece of logic:

!(
                trace._module &&
                trace._module.attributes &&
                trace._module.attributes.showlegend &&
                trace._module.attributes.showlegend.dflt === false

here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question.
This is to keep the baselines to what was in place before.
Otherwise if there are e.g. one scatter3d with other gl3d traces (that now support legend) the legend would show up on the graph for scatter3d.

diff-gl3d_world-cals

)
)) {
legendTraceCount++;
if(trace.showlegend) {
legendReallyHasATrace = true;
@@ -116,4 +123,10 @@ module.exports = function legendDefaults(layoutIn, layoutOut, fullData) {
coerce('yanchor', defaultYAnchor);
coerce('valign');
Lib.noneOrAll(containerIn, containerOut, ['x', 'y']);

var titleText = coerce('title.text');
if(titleText) {
coerce('title.side', orientation === 'h' ? 'left' : 'top');
Lib.coerceFont(coerce, 'title.font', layoutOut.font);
}
};
123 changes: 95 additions & 28 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
@@ -65,6 +65,19 @@ module.exports = function draw(gd) {

var scrollBox = Lib.ensureSingle(legend, 'g', 'scrollbox');

var title = opts.title;
opts._titleWidth = 0;
opts._titleHeight = 0;
if(title.text) {
var titleEl = Lib.ensureSingle(scrollBox, 'text', 'legendtitletext');
titleEl.attr('text-anchor', 'start')
.classed('user-select-none', true)
.call(Drawing.font, title.font)
.text(title.text);

textLayout(titleEl, scrollBox, gd); // handle mathjax or multi-line text and compute title height
}

var scrollBar = Lib.ensureSingle(legend, 'rect', 'scrollbar', function(s) {
s.attr(constants.scrollBarEnterAttrs)
.call(Color.fill, constants.scrollBarColor);
@@ -121,7 +134,7 @@ module.exports = function draw(gd) {
}

// Set size and position of all the elements that make up a legend:
// legend, background and border, scroll box and scroll bar
// legend, background and border, scroll box and scroll bar as well as title
Drawing.setTranslate(legend, lx, ly);

// to be safe, remove previous listeners
@@ -370,23 +383,17 @@ function drawTexts(g, gd) {

textEl.attr('text-anchor', 'start')
.classed('user-select-none', true)
.call(Drawing.font, fullLayout.legend.font)
.call(Drawing.font, opts.font)
.text(isEditable ? ensureLength(name, maxNameLength) : name);

svgTextUtils.positionText(textEl, constants.textGap, 0);

function textLayout(s) {
svgTextUtils.convertToTspans(s, gd, function() {
computeTextDimensions(g, gd);
});
}

if(isEditable) {
textEl.call(svgTextUtils.makeEditable, {gd: gd, text: name})
.call(textLayout)
.call(textLayout, g, gd)
.on('edit', function(newName) {
this.text(ensureLength(newName, maxNameLength))
.call(textLayout);
.call(textLayout, g, gd);

var fullInput = legendItem.trace._fullInput || {};
var update = {};
@@ -407,7 +414,7 @@ function drawTexts(g, gd) {
return Registry.call('_guiRestyle', gd, update, traceIndex);
});
} else {
textLayout(textEl);
textLayout(textEl, g, gd);
}
}

@@ -460,18 +467,24 @@ function setupTraceToggle(g, gd) {
});
}

function textLayout(s, g, gd) {
svgTextUtils.convertToTspans(s, gd, function() {
computeTextDimensions(g, gd);
});
}

function computeTextDimensions(g, gd) {
var legendItem = g.data()[0][0];

if(!legendItem.trace.showlegend) {
if(legendItem && !legendItem.trace.showlegend) {
g.remove();
return;
}

var mathjaxGroup = g.select('g[class*=math-group]');
var mathjaxNode = mathjaxGroup.node();
var bw = gd._fullLayout.legend.borderwidth;
var opts = gd._fullLayout.legend;
var lineHeight = opts.font.size * LINE_SPACING;
var lineHeight = (legendItem ? opts : opts.title).font.size * LINE_SPACING;
var height, width;

if(mathjaxNode) {
@@ -480,24 +493,56 @@ function computeTextDimensions(g, gd) {
height = mathjaxBB.height;
width = mathjaxBB.width;

Drawing.setTranslate(mathjaxGroup, 0, (height / 4));
if(legendItem) {
Drawing.setTranslate(mathjaxGroup, 0, height * 0.25);
} else { // case of title
Drawing.setTranslate(mathjaxGroup, bw, height * 0.75 + bw);
}
} else {
var text = g.select('.legendtext');
var textLines = svgTextUtils.lineCount(text);
var textNode = text.node();
var textEl = g.select(legendItem ?
'.legendtext' : '.legendtitletext'
);
var textLines = svgTextUtils.lineCount(textEl);
var textNode = textEl.node();

height = lineHeight * textLines;
width = textNode ? Drawing.bBox(textNode).width : 0;

// approximation to height offset to center the font
// to avoid getBoundingClientRect
var textY = lineHeight * (0.3 + (1 - textLines) / 2);
svgTextUtils.positionText(text, constants.textGap, textY);
var textY = lineHeight * ((textLines - 1) / 2 - 0.3);
if(legendItem) {
svgTextUtils.positionText(textEl, constants.textGap, -textY);
} else { // case of title
svgTextUtils.positionText(textEl, constants.titlePad + bw, lineHeight + bw);
}
}

if(legendItem) {
legendItem.lineHeight = lineHeight;
legendItem.height = Math.max(height, 16) + 3;
legendItem.width = width;
} else { // case of title
opts._titleWidth = width;
opts._titleHeight = height;
}
}

function getTitleSize(opts) {
var w = 0;
var h = 0;

var side = opts.title.side;
if(side) {
if(side.indexOf('left') !== -1) {
w = opts._titleWidth;
}
if(side.indexOf('top') !== -1) {
h = opts._titleHeight;
}
}

legendItem.lineHeight = lineHeight;
legendItem.height = Math.max(height, 16) + 3;
legendItem.width = width;
return [w, h];
}

/*
@@ -514,6 +559,7 @@ function computeLegendDimensions(gd, groups, traces) {
var fullLayout = gd._fullLayout;
var opts = fullLayout.legend;
var gs = fullLayout._size;

var isVertical = helpers.isVertical(opts);
var isGrouped = helpers.isGrouped(opts);

@@ -537,11 +583,15 @@ function computeLegendDimensions(gd, groups, traces) {
var toggleRectWidth = 0;
opts._width = 0;
opts._height = 0;
var titleSize = getTitleSize(opts);

if(isVertical) {
traces.each(function(d) {
var h = d[0].height;
Drawing.setTranslate(this, bw, itemGap + bw + opts._height + h / 2);
Drawing.setTranslate(this,
bw + titleSize[0],
bw + titleSize[1] + opts._height + h / 2 + itemGap
);
opts._height += h;
opts._width = Math.max(opts._width, d[0].width);
});
@@ -591,7 +641,10 @@ function computeLegendDimensions(gd, groups, traces) {
var offsetY = 0;
d3.select(this).selectAll('g.traces').each(function(d) {
var h = d[0].height;
Drawing.setTranslate(this, 0, itemGap + bw + h / 2 + offsetY);
Drawing.setTranslate(this,
titleSize[0],
titleSize[1] + bw + itemGap + h / 2 + offsetY
);
offsetY += h;
maxWidthInGroup = Math.max(maxWidthInGroup, textGap + d[0].width);
});
@@ -634,7 +687,10 @@ function computeLegendDimensions(gd, groups, traces) {
maxItemHeightInRow = 0;
}

Drawing.setTranslate(this, bw + offsetX, itemGap + bw + h / 2 + offsetY);
Drawing.setTranslate(this,
titleSize[0] + bw + offsetX,
titleSize[1] + bw + offsetY + h / 2 + itemGap
);

rowWidth = offsetX + w + itemGap;
offsetX += next;
@@ -651,8 +707,19 @@ function computeLegendDimensions(gd, groups, traces) {
}
}

opts._width = Math.ceil(opts._width);
opts._height = Math.ceil(opts._height);
opts._width = Math.ceil(
Math.max(
opts._width + titleSize[0],
opts._titleWidth + 2 * (bw + constants.titlePad)
)
);

opts._height = Math.ceil(
Math.max(
opts._height + titleSize[1],
opts._titleHeight + 2 * (bw + constants.itemGap)
)
);

opts._effHeight = Math.min(opts._height, opts._maxHeight);

Loading