From 67a11d71ec3df4c0ea49d84f331d14feaaf3be9c Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Fri, 4 Jan 2019 15:55:53 -0500 Subject: [PATCH 1/4] adding hovertemplates to polar bar and scatter and ternary scatter --- src/traces/barpolar/attributes.js | 5 +++ src/traces/barpolar/constants.js | 14 ++++++++ src/traces/barpolar/defaults.js | 1 + src/traces/barpolar/hover.js | 1 + src/traces/scatterpolar/attributes.js | 5 +++ src/traces/scatterpolar/constants.js | 14 ++++++++ src/traces/scatterpolar/defaults.js | 1 + src/traces/scatterpolar/hover.js | 45 +++++++++++++------------ src/traces/scatterpolargl/attributes.js | 1 + src/traces/scatterpolargl/defaults.js | 1 + src/traces/scatterternary/attributes.js | 5 +++ src/traces/scatterternary/constants.js | 14 ++++++++ src/traces/scatterternary/defaults.js | 1 + src/traces/scatterternary/hover.js | 17 +++++----- 14 files changed, 95 insertions(+), 30 deletions(-) create mode 100644 src/traces/barpolar/constants.js create mode 100644 src/traces/scatterpolar/constants.js create mode 100644 src/traces/scatterternary/constants.js diff --git a/src/traces/barpolar/attributes.js b/src/traces/barpolar/attributes.js index 2c9fd9ad38a..69db9fc9af4 100644 --- a/src/traces/barpolar/attributes.js +++ b/src/traces/barpolar/attributes.js @@ -8,6 +8,8 @@ 'use strict'; +var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes'); +var constants = require('./constants'); var extendFlat = require('../../lib/extend').extendFlat; var scatterPolarAttrs = require('../scatterpolar/attributes'); var barAttrs = require('../bar/attributes'); @@ -69,6 +71,9 @@ module.exports = { marker: barAttrs.marker, hoverinfo: scatterPolarAttrs.hoverinfo, + hovertemplate: hovertemplateAttrs({}, { + keys: constants.eventDataKeys + }), selected: barAttrs.selected, unselected: barAttrs.unselected diff --git a/src/traces/barpolar/constants.js b/src/traces/barpolar/constants.js new file mode 100644 index 00000000000..89c9c2ba941 --- /dev/null +++ b/src/traces/barpolar/constants.js @@ -0,0 +1,14 @@ +/** +* Copyright 2012-2019, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + + +'use strict'; + +module.exports = { + eventDataKeys: [] +}; diff --git a/src/traces/barpolar/defaults.js b/src/traces/barpolar/defaults.js index 7b612931771..6619d60056c 100644 --- a/src/traces/barpolar/defaults.js +++ b/src/traces/barpolar/defaults.js @@ -33,6 +33,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout coerce('width'); coerce('text'); + coerce('hovertemplate'); // coerce('hovertext'); // var textPosition = coerce('textposition'); diff --git a/src/traces/barpolar/hover.js b/src/traces/barpolar/hover.js index b6ea706b59f..2719f16725e 100644 --- a/src/traces/barpolar/hover.js +++ b/src/traces/barpolar/hover.js @@ -61,6 +61,7 @@ module.exports = function hoverPoints(pointData, xval, yval) { var _cdi = Lib.extendFlat({}, cdi, {r: cdi.s, theta: cdi.p}); fillHoverText(cdi, trace, pointData); makeHoverPointText(_cdi, trace, subplot, pointData); + pointData.hovertemplate = trace.hovertemplate; pointData.color = getTraceColor(trace, cdi); pointData.xLabelVal = pointData.yLabelVal = undefined; diff --git a/src/traces/scatterpolar/attributes.js b/src/traces/scatterpolar/attributes.js index cd7a5b1fb8a..a15693136dd 100644 --- a/src/traces/scatterpolar/attributes.js +++ b/src/traces/scatterpolar/attributes.js @@ -8,6 +8,8 @@ 'use strict'; +var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes'); +var constants = require('./constants'); var extendFlat = require('../../lib/extend').extendFlat; var scatterAttrs = require('../scatter/attributes'); var plotAttrs = require('../../plots/attributes'); @@ -130,6 +132,9 @@ module.exports = { flags: ['r', 'theta', 'text', 'name'] }), hoveron: scatterAttrs.hoveron, + hovertemplate: hovertemplateAttrs({}, { + keys: constants.eventDataKeys + }), selected: scatterAttrs.selected, unselected: scatterAttrs.unselected diff --git a/src/traces/scatterpolar/constants.js b/src/traces/scatterpolar/constants.js new file mode 100644 index 00000000000..89c9c2ba941 --- /dev/null +++ b/src/traces/scatterpolar/constants.js @@ -0,0 +1,14 @@ +/** +* Copyright 2012-2019, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + + +'use strict'; + +module.exports = { + eventDataKeys: [] +}; diff --git a/src/traces/scatterpolar/defaults.js b/src/traces/scatterpolar/defaults.js index 5b34b17e95d..192cd77b66a 100644 --- a/src/traces/scatterpolar/defaults.js +++ b/src/traces/scatterpolar/defaults.js @@ -35,6 +35,7 @@ function supplyDefaults(traceIn, traceOut, defaultColor, layout) { coerce('mode', len < PTS_LINESONLY ? 'lines+markers' : 'lines'); coerce('text'); coerce('hovertext'); + coerce('hovertemplate'); if(subTypes.hasLines(traceOut)) { handleLineDefaults(traceIn, traceOut, defaultColor, layout, coerce); diff --git a/src/traces/scatterpolar/hover.js b/src/traces/scatterpolar/hover.js index e20791af0b5..ea52df6b1ee 100644 --- a/src/traces/scatterpolar/hover.js +++ b/src/traces/scatterpolar/hover.js @@ -32,41 +32,44 @@ function hoverPoints(pointData, xval, yval, hovermode) { newPointData.xLabelVal = undefined; newPointData.yLabelVal = undefined; makeHoverPointText(cdi, trace, subplot, newPointData); - + newPointData.hovertemplate = trace.hovertemplate; return scatterPointData; } function makeHoverPointText(cdi, trace, subplot, pointData) { + var radialAxis = subplot.radialAxis; var angularAxis = subplot.angularAxis; - var hoverinfo = cdi.hi || trace.hoverinfo; - var parts = hoverinfo.split('+'); - var text = []; - radialAxis._hovertitle = 'r'; angularAxis._hovertitle = 'θ'; + var hoverinfo = cdi.hi || trace.hoverinfo; + var text = []; function textPart(ax, val) { text.push(ax._hovertitle + ': ' + Axes.tickText(ax, val, 'hover').text); } - if(parts.indexOf('all') !== -1) parts = ['r', 'theta', 'text']; - if(parts.indexOf('r') !== -1) { - textPart(radialAxis, radialAxis.c2l(cdi.r)); - } - if(parts.indexOf('theta') !== -1) { - var theta = cdi.theta; - textPart( - angularAxis, - angularAxis.thetaunit === 'degrees' ? Lib.rad2deg(theta) : theta - ); - } - if(parts.indexOf('text') !== -1 && pointData.text) { - text.push(pointData.text); - delete pointData.text; - } + if(!trace.hovertemplate) { + var parts = hoverinfo.split('+'); + + if(parts.indexOf('all') !== -1) parts = ['r', 'theta', 'text']; + if(parts.indexOf('r') !== -1) { + textPart(radialAxis, radialAxis.c2l(cdi.r)); + } + if(parts.indexOf('theta') !== -1) { + var theta = cdi.theta; + textPart( + angularAxis, + angularAxis.thetaunit === 'degrees' ? Lib.rad2deg(theta) : theta + ); + } + if(parts.indexOf('text') !== -1 && pointData.text) { + text.push(pointData.text); + delete pointData.text; + } - pointData.extraText = text.join('
'); + pointData.extraText = text.join('
'); + } } module.exports = { diff --git a/src/traces/scatterpolargl/attributes.js b/src/traces/scatterpolargl/attributes.js index 840deec22b5..14ebb4568da 100644 --- a/src/traces/scatterpolargl/attributes.js +++ b/src/traces/scatterpolargl/attributes.js @@ -23,6 +23,7 @@ module.exports = { text: scatterPolarAttrs.text, hovertext: scatterPolarAttrs.hovertext, + hovertemplate: scatterPolarAttrs.hovertemplate, line: scatterGlAttrs.line, connectgaps: scatterGlAttrs.connectgaps, diff --git a/src/traces/scatterpolargl/defaults.js b/src/traces/scatterpolargl/defaults.js index c68c5732f65..d65beaa946d 100644 --- a/src/traces/scatterpolargl/defaults.js +++ b/src/traces/scatterpolargl/defaults.js @@ -35,6 +35,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout coerce('mode', len < PTS_LINESONLY ? 'lines+markers' : 'lines'); coerce('text'); coerce('hovertext'); + coerce('hovertemplate'); if(subTypes.hasLines(traceOut)) { handleLineDefaults(traceIn, traceOut, defaultColor, layout, coerce); diff --git a/src/traces/scatterternary/attributes.js b/src/traces/scatterternary/attributes.js index 9326d58d202..8ac68df9d91 100644 --- a/src/traces/scatterternary/attributes.js +++ b/src/traces/scatterternary/attributes.js @@ -8,6 +8,8 @@ 'use strict'; +var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes'); +var constants = require('./constants'); var scatterAttrs = require('../scatter/attributes'); var plotAttrs = require('../../plots/attributes'); var colorAttributes = require('../../components/colorscale/attributes'); @@ -147,4 +149,7 @@ module.exports = { flags: ['a', 'b', 'c', 'text', 'name'] }), hoveron: scatterAttrs.hoveron, + hovertemplate: hovertemplateAttrs({}, { + keys: constants.eventDataKeys + }), }; diff --git a/src/traces/scatterternary/constants.js b/src/traces/scatterternary/constants.js new file mode 100644 index 00000000000..89c9c2ba941 --- /dev/null +++ b/src/traces/scatterternary/constants.js @@ -0,0 +1,14 @@ +/** +* Copyright 2012-2019, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + + +'use strict'; + +module.exports = { + eventDataKeys: [] +}; diff --git a/src/traces/scatterternary/defaults.js b/src/traces/scatterternary/defaults.js index eb57b8df677..df7ba6ba875 100644 --- a/src/traces/scatterternary/defaults.js +++ b/src/traces/scatterternary/defaults.js @@ -61,6 +61,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout coerce('text'); coerce('hovertext'); + coerce('hovertemplate'); var defaultMode = len < constants.PTS_LINESONLY ? 'lines+markers' : 'lines'; coerce('mode', defaultMode); diff --git a/src/traces/scatterternary/hover.js b/src/traces/scatterternary/hover.js index 37711ff893f..eaf0ad26421 100644 --- a/src/traces/scatterternary/hover.js +++ b/src/traces/scatterternary/hover.js @@ -52,19 +52,18 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) { var trace = newPointData.trace; var ternary = newPointData.subplot; var hoverinfo = cdi.hi || trace.hoverinfo; - var parts = hoverinfo.split('+'); var text = []; - function textPart(ax, val) { text.push(ax._hovertitle + ': ' + Axes.tickText(ax, val, 'hover').text); } - - if(parts.indexOf('all') !== -1) parts = ['a', 'b', 'c']; - if(parts.indexOf('a') !== -1) textPart(ternary.aaxis, cdi.a); - if(parts.indexOf('b') !== -1) textPart(ternary.baxis, cdi.b); - if(parts.indexOf('c') !== -1) textPart(ternary.caxis, cdi.c); - + if(!trace.hovertemplate) { + var parts = hoverinfo.split('+'); + if(parts.indexOf('all') !== -1) parts = ['a', 'b', 'c']; + if(parts.indexOf('a') !== -1) textPart(ternary.aaxis, cdi.a); + if(parts.indexOf('b') !== -1) textPart(ternary.baxis, cdi.b); + if(parts.indexOf('c') !== -1) textPart(ternary.caxis, cdi.c); + } newPointData.extraText = text.join('
'); - + newPointData.hovertemplate = trace.hovertemplate; return scatterPointData; }; From 78bc2cfb13424b4f5669d233b705da134c9d1487 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Mon, 7 Jan 2019 09:17:14 -0500 Subject: [PATCH 2/4] PR feedback --- src/traces/barpolar/attributes.js | 5 +---- src/traces/barpolar/constants.js | 14 -------------- src/traces/scatterpolar/attributes.js | 5 +---- src/traces/scatterpolar/constants.js | 14 -------------- src/traces/scatterpolar/defaults.js | 2 +- src/traces/scatterpolargl/defaults.js | 2 +- src/traces/scatterternary/attributes.js | 5 +---- src/traces/scatterternary/constants.js | 14 -------------- src/traces/scatterternary/defaults.js | 2 +- 9 files changed, 6 insertions(+), 57 deletions(-) delete mode 100644 src/traces/barpolar/constants.js delete mode 100644 src/traces/scatterpolar/constants.js delete mode 100644 src/traces/scatterternary/constants.js diff --git a/src/traces/barpolar/attributes.js b/src/traces/barpolar/attributes.js index 69db9fc9af4..0bc594a2878 100644 --- a/src/traces/barpolar/attributes.js +++ b/src/traces/barpolar/attributes.js @@ -9,7 +9,6 @@ 'use strict'; var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes'); -var constants = require('./constants'); var extendFlat = require('../../lib/extend').extendFlat; var scatterPolarAttrs = require('../scatterpolar/attributes'); var barAttrs = require('../bar/attributes'); @@ -71,9 +70,7 @@ module.exports = { marker: barAttrs.marker, hoverinfo: scatterPolarAttrs.hoverinfo, - hovertemplate: hovertemplateAttrs({}, { - keys: constants.eventDataKeys - }), + hovertemplate: hovertemplateAttrs(), selected: barAttrs.selected, unselected: barAttrs.unselected diff --git a/src/traces/barpolar/constants.js b/src/traces/barpolar/constants.js deleted file mode 100644 index 89c9c2ba941..00000000000 --- a/src/traces/barpolar/constants.js +++ /dev/null @@ -1,14 +0,0 @@ -/** -* Copyright 2012-2019, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - - -'use strict'; - -module.exports = { - eventDataKeys: [] -}; diff --git a/src/traces/scatterpolar/attributes.js b/src/traces/scatterpolar/attributes.js index a15693136dd..d1a2b86fce5 100644 --- a/src/traces/scatterpolar/attributes.js +++ b/src/traces/scatterpolar/attributes.js @@ -9,7 +9,6 @@ 'use strict'; var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes'); -var constants = require('./constants'); var extendFlat = require('../../lib/extend').extendFlat; var scatterAttrs = require('../scatter/attributes'); var plotAttrs = require('../../plots/attributes'); @@ -132,9 +131,7 @@ module.exports = { flags: ['r', 'theta', 'text', 'name'] }), hoveron: scatterAttrs.hoveron, - hovertemplate: hovertemplateAttrs({}, { - keys: constants.eventDataKeys - }), + hovertemplate: hovertemplateAttrs(), selected: scatterAttrs.selected, unselected: scatterAttrs.unselected diff --git a/src/traces/scatterpolar/constants.js b/src/traces/scatterpolar/constants.js deleted file mode 100644 index 89c9c2ba941..00000000000 --- a/src/traces/scatterpolar/constants.js +++ /dev/null @@ -1,14 +0,0 @@ -/** -* Copyright 2012-2019, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - - -'use strict'; - -module.exports = { - eventDataKeys: [] -}; diff --git a/src/traces/scatterpolar/defaults.js b/src/traces/scatterpolar/defaults.js index 192cd77b66a..81f7178549a 100644 --- a/src/traces/scatterpolar/defaults.js +++ b/src/traces/scatterpolar/defaults.js @@ -35,7 +35,7 @@ function supplyDefaults(traceIn, traceOut, defaultColor, layout) { coerce('mode', len < PTS_LINESONLY ? 'lines+markers' : 'lines'); coerce('text'); coerce('hovertext'); - coerce('hovertemplate'); + if(traceOut.hoveron !== 'fills') coerce('hovertemplate'); if(subTypes.hasLines(traceOut)) { handleLineDefaults(traceIn, traceOut, defaultColor, layout, coerce); diff --git a/src/traces/scatterpolargl/defaults.js b/src/traces/scatterpolargl/defaults.js index d65beaa946d..3f534d7a207 100644 --- a/src/traces/scatterpolargl/defaults.js +++ b/src/traces/scatterpolargl/defaults.js @@ -35,7 +35,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout coerce('mode', len < PTS_LINESONLY ? 'lines+markers' : 'lines'); coerce('text'); coerce('hovertext'); - coerce('hovertemplate'); + if(traceOut.hoveron !== 'fills') coerce('hovertemplate'); if(subTypes.hasLines(traceOut)) { handleLineDefaults(traceIn, traceOut, defaultColor, layout, coerce); diff --git a/src/traces/scatterternary/attributes.js b/src/traces/scatterternary/attributes.js index 8ac68df9d91..439259f7897 100644 --- a/src/traces/scatterternary/attributes.js +++ b/src/traces/scatterternary/attributes.js @@ -9,7 +9,6 @@ 'use strict'; var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes'); -var constants = require('./constants'); var scatterAttrs = require('../scatter/attributes'); var plotAttrs = require('../../plots/attributes'); var colorAttributes = require('../../components/colorscale/attributes'); @@ -149,7 +148,5 @@ module.exports = { flags: ['a', 'b', 'c', 'text', 'name'] }), hoveron: scatterAttrs.hoveron, - hovertemplate: hovertemplateAttrs({}, { - keys: constants.eventDataKeys - }), + hovertemplate: hovertemplateAttrs(), }; diff --git a/src/traces/scatterternary/constants.js b/src/traces/scatterternary/constants.js deleted file mode 100644 index 89c9c2ba941..00000000000 --- a/src/traces/scatterternary/constants.js +++ /dev/null @@ -1,14 +0,0 @@ -/** -* Copyright 2012-2019, Plotly, Inc. -* All rights reserved. -* -* This source code is licensed under the MIT license found in the -* LICENSE file in the root directory of this source tree. -*/ - - -'use strict'; - -module.exports = { - eventDataKeys: [] -}; diff --git a/src/traces/scatterternary/defaults.js b/src/traces/scatterternary/defaults.js index df7ba6ba875..fce16c7cdd8 100644 --- a/src/traces/scatterternary/defaults.js +++ b/src/traces/scatterternary/defaults.js @@ -61,7 +61,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout coerce('text'); coerce('hovertext'); - coerce('hovertemplate'); + if(traceOut.hoveron !== 'fills') coerce('hovertemplate'); var defaultMode = len < constants.PTS_LINESONLY ? 'lines+markers' : 'lines'; coerce('mode', defaultMode); From 1d989fbd32f42e14436a4918a4cd4a46e3abccfe Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Mon, 7 Jan 2019 09:58:29 -0500 Subject: [PATCH 3/4] scatterpolar tests --- test/jasmine/tests/scatterpolar_test.js | 8 ++++++++ test/jasmine/tests/scatterpolargl_test.js | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/test/jasmine/tests/scatterpolar_test.js b/test/jasmine/tests/scatterpolar_test.js index ebbd574c753..7d99919c375 100644 --- a/test/jasmine/tests/scatterpolar_test.js +++ b/test/jasmine/tests/scatterpolar_test.js @@ -109,6 +109,14 @@ describe('Test scatterpolar hover:', function() { desc: 'base', nums: 'r: 4.022892\nθ: 128.342°', name: 'Trial 3' + }, { + desc: 'with hovertemplate', + patch: function(fig) { + fig.data[2].hovertemplate = 'template %{r} %{theta}'; + return fig; + }, + nums: 'template 4.02289202968 128.342009045', + name: 'Trial 3' }, { desc: '(no labels - out of sector)', patch: function(fig) { diff --git a/test/jasmine/tests/scatterpolargl_test.js b/test/jasmine/tests/scatterpolargl_test.js index a19d245285e..f56bdfe34d9 100644 --- a/test/jasmine/tests/scatterpolargl_test.js +++ b/test/jasmine/tests/scatterpolargl_test.js @@ -44,6 +44,14 @@ describe('Test scatterpolargl hover:', function() { desc: 'base', nums: 'r: 3.886013\nθ: 125.2822°', name: 'Trial 3' + }, { + desc: 'with hovertemplate', + patch: function(fig) { + fig.data[2].hovertemplate = 'template %{r} %{theta}'; + return fig; + }, + nums: 'template 3.88601339194 125.282157112', + name: 'Trial 3' }, { desc: '(no labels - out of sector)', patch: function(fig) { From 8e45b14d9fecba125c71d3e18f2b518516262153 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Mon, 7 Jan 2019 11:03:47 -0500 Subject: [PATCH 4/4] barpolar and scatterternary tests --- test/jasmine/tests/barpolar_test.js | 16 ++++++++++++++++ test/jasmine/tests/scatterternary_test.js | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/test/jasmine/tests/barpolar_test.js b/test/jasmine/tests/barpolar_test.js index 46c8c46f2d6..76bf9bc943d 100644 --- a/test/jasmine/tests/barpolar_test.js +++ b/test/jasmine/tests/barpolar_test.js @@ -115,6 +115,22 @@ describe('Test barpolar hover:', function() { extraText: 'r: 1
θ: 0°', color: '#1f77b4' } + }, { + desc: 'hovertemplate', + traces: [{ + r: [1, 2, 3], + theta: [0, 90, 180], + hovertemplate: 'tpl', + }], + xval: 1, + yval: 0, + exp: { + index: 0, + x: 263.33, + y: 200, + hovertemplate: 'tpl', + color: '#1f77b4' + } }, { desc: 'with custom text scalar', traces: [{ diff --git a/test/jasmine/tests/scatterternary_test.js b/test/jasmine/tests/scatterternary_test.js index d5e10ae289d..068da83dc1d 100644 --- a/test/jasmine/tests/scatterternary_test.js +++ b/test/jasmine/tests/scatterternary_test.js @@ -402,6 +402,22 @@ describe('scatterternary hover', function() { .then(done); }); + it('should pass along hovertemplate on hover', function(done) { + var xval = 0.42; + var yval = 0.37; + var hovermode = 'closest'; + var scatterPointData; + Plotly.restyle(gd, { + hovertemplate: 'tpl' + }) + .then(function() { + scatterPointData = _hover(gd, xval, yval, hovermode); + expect(scatterPointData[0].hovertemplate).toEqual('tpl'); + }) + .catch(failTest) + .then(done); + }); + }); describe('Test scatterternary *cliponaxis*', function() {