Skip to content

add: Add unifiedhovertemplate to format unified hover title #7439

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 17 additions & 1 deletion src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -1211,10 +1211,26 @@ function createHoverText(hoverData, opts) {
// mock legend
var hoverlabel = fullLayout.hoverlabel;
var font = hoverlabel.font;

var item0 = groupedHoverData[0];

var unifiedhovertemplate = (
hovermode === 'y unified' ?
item0.xa :
item0.ya
).unifiedhovertemplate;

var mainText = !unifiedhovertemplate ? t0 :
Lib.hovertemplateString(unifiedhovertemplate, {}, fullLayout._d3locale,
hovermode === 'y unified' ?
{xa: item0.xa, x: item0.xVal} :
{ya: item0.ya, y: item0.yVal}
);

var mockLayoutIn = {
showlegend: true,
legend: {
title: {text: t0, font: font},
title: {text: mainText, font: font},
font: font,
bgcolor: hoverlabel.bgcolor,
bordercolor: hoverlabel.bordercolor,
Expand Down
5 changes: 4 additions & 1 deletion src/plots/cartesian/axis_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,

handleCategoryOrderDefaults(containerIn, containerOut, coerce, options);

if(axType !== 'category' && !options.noHover) coerce('hoverformat');
if(!options.noHover) {
if(axType !== 'category') coerce('hoverformat');
if(!options.noUnifiedhovertemplate) coerce('unifiedhovertemplate');
}

var dfltColor = coerce('color');
// if axis.color was provided, use it for fonts too; otherwise,
Expand Down
10 changes: 10 additions & 0 deletions src/plots/cartesian/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var colorAttrs = require('../../components/color/attributes');
var dash = require('../../components/drawing/attributes').dash;
var extendFlat = require('../../lib/extend').extendFlat;
var templatedArray = require('../../plot_api/plot_template').templatedArray;
var templateFormatStringDescription = require('../../plots/template_attributes').templateFormatStringDescription;
var descriptionWithDates = require('../../plots/cartesian/axis_format_attributes').descriptionWithDates;

var ONEDAY = require('../../constants/numerical').ONEDAY;
Expand Down Expand Up @@ -975,6 +976,15 @@ module.exports = {
editType: 'none',
description: descriptionWithDates('hover text')
},
unifiedhovertemplate: {
valType: 'string',
dflt: '',
editType: 'none',
description: [
'Template string used for rendering the title that appear on x or y unified hover box.',
templateFormatStringDescription()
].join(' ')
},
// lines and grids
showline: {
valType: 'boolean',
Expand Down
1 change: 1 addition & 0 deletions src/plots/gl3d/layout/axis_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, options) {
noTicklabelposition: true,
noTicklabeloverflow: true,
noInsiderange: true,
noUnifiedhovertemplate: true,
bgColor: options.bgColor,
calendar: options.calendar
},
Expand Down
1 change: 1 addition & 0 deletions src/plots/template_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function templateFormatStringDescription(opts) {
'for details on the date formatting syntax.'
].join(' ');
}
exports.templateFormatStringDescription = templateFormatStringDescription;

function shapeTemplateFormatStringDescription() {
return [
Expand Down
12 changes: 12 additions & 0 deletions test/plot-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -14949,6 +14949,12 @@
"editType": "none",
"valType": "any"
},
"unifiedhovertemplate": {
"description": "Template string used for rendering the title that appear on x or y unified hover box. Variables are inserted using %{variable}, for example \"y: %{y}\". Numbers are formatted using d3-format's syntax %{variable:d3-format}, for example \"Price: %{y:$.2f}\". https://github.com/d3/d3-format/tree/v1.4.5#d3-format for details on the formatting syntax. Dates are formatted using d3-time-format's syntax %{variable|d3-time-format}, for example \"Day: %{2019-01-01|%A}\". https://github.com/d3/d3-time-format/tree/v2.2.3#locale_format for details on the date formatting syntax.",
"dflt": "",
"editType": "none",
"valType": "string"
},
"visible": {
"description": "A single toggle to hide the axis while preserving interaction like dragging. Default is true when a cheater plot is present on the axis, otherwise false",
"editType": "plot",
Expand Down Expand Up @@ -16193,6 +16199,12 @@
"editType": "none",
"valType": "any"
},
"unifiedhovertemplate": {
"description": "Template string used for rendering the title that appear on x or y unified hover box. Variables are inserted using %{variable}, for example \"y: %{y}\". Numbers are formatted using d3-format's syntax %{variable:d3-format}, for example \"Price: %{y:$.2f}\". https://github.com/d3/d3-format/tree/v1.4.5#d3-format for details on the formatting syntax. Dates are formatted using d3-time-format's syntax %{variable|d3-time-format}, for example \"Day: %{2019-01-01|%A}\". https://github.com/d3/d3-time-format/tree/v2.2.3#locale_format for details on the date formatting syntax.",
"dflt": "",
"editType": "none",
"valType": "string"
},
"visible": {
"description": "A single toggle to hide the axis while preserving interaction like dragging. Default is true when a cheater plot is present on the axis, otherwise false",
"editType": "plot",
Expand Down
Loading