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

Use hoverlabel.font for group titles in unified hover modes #5895

Merged
merged 7 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions draftlogs/5895_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Use hoverlabel.font for group titles in unified hover modes
11 changes: 7 additions & 4 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -1029,13 +1029,15 @@ function createHoverText(hoverData, opts, gd) {
if(hoverData.length === 0) return;

// mock legend
var hoverlabel = fullLayout.hoverlabel;
var font = hoverlabel.font;
var mockLayoutIn = {
showlegend: true,
legend: {
title: {text: t0, font: fullLayout.hoverlabel.font},
font: fullLayout.hoverlabel.font,
bgcolor: fullLayout.hoverlabel.bgcolor,
bordercolor: fullLayout.hoverlabel.bordercolor,
title: {text: t0, font: font},
font: font,
bgcolor: hoverlabel.bgcolor,
bordercolor: hoverlabel.bordercolor,
borderwidth: 1,
tracegroupgap: 7,
traceorder: fullLayout.legend ? fullLayout.legend.traceorder : undefined,
Expand Down Expand Up @@ -1080,6 +1082,7 @@ function createHoverText(hoverData, opts, gd) {

// Draw unified hover label
mockLegend._inHover = true;
mockLegend._groupTitleFont = font;
legendDraw(gd, mockLegend);

// Position the hover
Expand Down
4 changes: 3 additions & 1 deletion src/components/legend/get_legend_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var Registry = require('../../registry');
var helpers = require('./helpers');

module.exports = function getLegendData(calcdata, opts) {
var inHover = opts._inHover;
var grouped = helpers.isGrouped(opts);
var reversed = helpers.isReversed(opts);

Expand Down Expand Up @@ -39,7 +40,7 @@ module.exports = function getLegendData(calcdata, opts) {
var trace = cd0.trace;
var lgroup = trace.legendgroup;

if(!opts._inHover && (!trace.visible || !trace.showlegend)) continue;
if(!inHover && (!trace.visible || !trace.showlegend)) continue;

if(Registry.traceIs(trace, 'pie-like')) {
if(!slicesShown[lgroup]) slicesShown[lgroup] = {};
Expand Down Expand Up @@ -126,6 +127,7 @@ module.exports = function getLegendData(calcdata, opts) {
var gt = legendData[i][j].trace.legendgrouptitle;
if(gt && gt.text) {
groupTitle = gt;
if(inHover) gt.font = opts._groupTitleFont;
break;
}
}
Expand Down
56 changes: 45 additions & 11 deletions test/jasmine/tests/hover_label_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ var assertElemRightTo = customAssertions.assertElemRightTo;
var assertElemTopsAligned = customAssertions.assertElemTopsAligned;
var assertElemInside = customAssertions.assertElemInside;

var groupTitlesMock = require('@mocks/legendgroup-titles');

function touch(path, options) {
var len = path.length;
Lib.clearThrottle();
Expand Down Expand Up @@ -4547,17 +4549,6 @@ describe('hovermode: (x|y)unified', function() {
});
}

function assertFont(fontFamily, fontSize, fontColor) {
var hover = getHoverLabel();
var text = hover.select('text.legendtext');
var node = text.node();

var textStyle = window.getComputedStyle(node);
expect(textStyle.fontFamily.split(',')[0]).toBe(fontFamily, 'wrong font family');
expect(textStyle.fontSize).toBe(fontSize, 'wrong font size');
expect(textStyle.fill).toBe(fontColor, 'wrong font color');
}

it('set smart defaults for spikeline in x unified', function(done) {
Plotly.newPlot(gd, [{y: [4, 6, 5]}], {'hovermode': 'x unified', 'xaxis': {'color': 'red'}})
.then(function(gd) {
Expand Down Expand Up @@ -6011,6 +6002,17 @@ describe('hovermode: (x|y)unified', function() {
});

it('should use hoverlabel.font or legend.font or layout.font', function(done) {
function assertFont(fontFamily, fontSize, fontColor) {
var hover = getHoverLabel();
var text = hover.select('text.legendtext');
var node = text.node();

var textStyle = window.getComputedStyle(node);
expect(textStyle.fontFamily.split(',')[0]).toBe(fontFamily, 'wrong font family');
expect(textStyle.fontSize).toBe(fontSize, 'wrong font size');
expect(textStyle.fill).toBe(fontColor, 'wrong font color');
}

var mockCopy = Lib.extendDeep({}, mock);

// Set layout.font
Expand Down Expand Up @@ -6078,6 +6080,38 @@ describe('hovermode: (x|y)unified', function() {
.then(done, done.fail);
});

it('should use hoverlabel.font for group titles as well as traces', function(done) {
function assertFont(fontFamily, fontSize, fontColor) {
var hover = getHoverLabel();
var traces = hover.selectAll('g.traces');

traces.each(function() {
var e = d3Select(this);
var text = e.select('text.legendtext');
var node = text.node();

var textStyle = window.getComputedStyle(node);
expect(textStyle.fontFamily.split(',')[0]).toBe(fontFamily, 'wrong font family');
expect(textStyle.fontSize).toBe(fontSize, 'wrong font size');
expect(textStyle.fill).toBe(fontColor, 'wrong font color');
});
}

var mockCopy = Lib.extendDeep({}, groupTitlesMock);

mockCopy.layout.hoverlabel = {
font: {size: 20, family: 'Mono', color: 'rgb(255, 127, 0)'}
};

Plotly.newPlot(gd, mockCopy)
.then(function(gd) {
_hover(gd, { xval: 0});

assertFont('Mono', '20px', 'rgb(255, 127, 0)');
})
.then(done, done.fail);
});

it('should work with hovertemplate', function(done) {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].hovertemplate = 'hovertemplate: %{y:0.2f}';
Expand Down