Skip to content

Commit 7983568

Browse files
committed
implement hoverlabel.align to loneHover traces
1 parent 6cd54ae commit 7983568

File tree

7 files changed

+64
-1
lines changed

7 files changed

+64
-1
lines changed

src/components/fx/hover.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ exports.loneHover = function loneHover(hoverItem, opts) {
125125
fontSize: hoverItem.fontSize,
126126
fontColor: hoverItem.fontColor,
127127
nameLength: hoverItem.nameLength,
128+
textAlign: hoverItem.textAlign,
128129

129130
// filler to make createHoverText happy
130131
trace: hoverItem.trace || {
@@ -182,6 +183,7 @@ exports.multiHovers = function multiHovers(hoverItems, opts) {
182183
fontSize: hoverItem.fontSize,
183184
fontColor: hoverItem.fontColor,
184185
nameLength: hoverItem.nameLength,
186+
textAlign: hoverItem.textAlign,
185187

186188
// filler to make createHoverText happy
187189
trace: hoverItem.trace || {

src/plots/gl2d/scene2d.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,8 @@ proto.draw = function() {
687687
fontFamily: Fx.castHoverOption(trace, ptNumber, 'font.family'),
688688
fontSize: Fx.castHoverOption(trace, ptNumber, 'font.size'),
689689
fontColor: Fx.castHoverOption(trace, ptNumber, 'font.color'),
690-
nameLength: Fx.castHoverOption(trace, ptNumber, 'namelength')
690+
nameLength: Fx.castHoverOption(trace, ptNumber, 'namelength'),
691+
textAlign: Fx.castHoverOption(trace, ptNumber, 'align')
691692
}, {
692693
container: this.svgContainer,
693694
gd: this.graphDiv

src/plots/gl3d/scene.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ function render(scene) {
170170
fontSize: Fx.castHoverOption(traceNow, ptNumber, 'font.size'),
171171
fontColor: Fx.castHoverOption(traceNow, ptNumber, 'font.color'),
172172
nameLength: Fx.castHoverOption(traceNow, ptNumber, 'namelength'),
173+
textAlign: Fx.castHoverOption(traceNow, ptNumber, 'align'),
173174
hovertemplate: Lib.castOption(traceNow, ptNumber, 'hovertemplate'),
174175
hovertemplateLabels: Lib.extendFlat({}, pointData, labels),
175176
eventData: [pointData]

src/traces/pie/plot.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ function attachFxHandlers(sliceTop, gd, cd) {
380380
fontSize: helpers.castOption(hoverFont.size, pt.pts),
381381
fontColor: helpers.castOption(hoverFont.color, pt.pts),
382382
nameLength: helpers.castOption(hoverLabel.namelength, pt.pts),
383+
textAlign: helpers.castOption(hoverLabel.align, pt.pts),
383384
hovertemplate: helpers.castOption(trace2.hovertemplate, pt.pts),
384385
hovertemplateLabels: pt,
385386
eventData: [eventData(pt, trace2)]

src/traces/sankey/plot.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ module.exports = function plot(gd, calcData) {
212212
fontSize: castHoverOption(obj, 'font.size'),
213213
fontColor: castHoverOption(obj, 'font.color'),
214214
nameLength: castHoverOption(obj, 'namelength'),
215+
textAlign: castHoverOption(obj, 'align'),
215216
idealAlign: d3.event.x < hoverCenter[0] ? 'right' : 'left',
216217

217218
hovertemplate: obj.hovertemplate,
@@ -301,6 +302,7 @@ module.exports = function plot(gd, calcData) {
301302
fontSize: castHoverOption(obj, 'font.size'),
302303
fontColor: castHoverOption(obj, 'font.color'),
303304
nameLength: castHoverOption(obj, 'namelength'),
305+
textAlign: castHoverOption(obj, 'align'),
304306
idealAlign: 'left',
305307

306308
hovertemplate: obj.hovertemplate,

src/traces/sunburst/plot.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ function attachFxHandlers(sliceTop, gd, cd) {
594594
fontSize: _cast('hoverlabel.font.size'),
595595
fontColor: _cast('hoverlabel.font.color'),
596596
nameLength: _cast('hoverlabel.namelength'),
597+
textAlign: _cast('hoverlabel.align'),
597598
hovertemplate: hovertemplate,
598599
hovertemplateLabels: hoverPt,
599600
eventData: [makeEventData(pt, traceNow)]

test/jasmine/tests/pie_test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,61 @@ describe('pie hovering', function() {
11871187
.catch(failTest)
11881188
.then(done);
11891189
});
1190+
1191+
it('should honor *hoverlabel.align*', function(done) {
1192+
function _assert(msg, exp) {
1193+
var tx = d3.select('g.hovertext').select('text');
1194+
expect(tx.attr('text-anchor')).toBe(exp.textAnchor, 'text anchor|' + msg);
1195+
expect(Number(tx.attr('x'))).toBeCloseTo(exp.posX, 2, 'x position|' + msg);
1196+
}
1197+
1198+
function _hoverLeft() {
1199+
mouseEvent('mouseover', 100, 200);
1200+
Lib.clearThrottle();
1201+
}
1202+
1203+
function _hoverRight() {
1204+
mouseEvent('mouseover', 300, 200);
1205+
Lib.clearThrottle();
1206+
}
1207+
1208+
Plotly.plot(gd, [{
1209+
type: 'pie',
1210+
labels: ['a', 'b']
1211+
}], {
1212+
showlegend: false,
1213+
margin: {l: 0, t: 0, b: 0, r: 0},
1214+
width: 400,
1215+
height: 400
1216+
})
1217+
.then(_hoverLeft)
1218+
.then(function() { _assert('base left sector', {textAnchor: 'start', posX: 9}); })
1219+
.then(_hoverRight)
1220+
.then(function() { _assert('base right sector', {textAnchor: 'end', posX: -9}); })
1221+
.then(function() {
1222+
return Plotly.relayout(gd, 'hoverlabel.align', 'left');
1223+
})
1224+
.then(_hoverLeft)
1225+
.then(function() { _assert('align:left left sector', {textAnchor: 'start', posX: 9}); })
1226+
.then(_hoverRight)
1227+
.then(function() { _assert('align:left right sector', {textAnchor: 'start', posX: -37.45}); })
1228+
.then(function() {
1229+
return Plotly.restyle(gd, 'hoverlabel.align', 'right');
1230+
})
1231+
.then(_hoverLeft)
1232+
.then(function() { _assert('align:right left sector', {textAnchor: 'end', posX: 37.45}); })
1233+
.then(_hoverRight)
1234+
.then(function() { _assert('align:right right sector', {textAnchor: 'end', posX: -9}); })
1235+
.then(function() {
1236+
return Plotly.restyle(gd, 'hoverlabel.align', [['left', 'right']]);
1237+
})
1238+
.then(_hoverLeft)
1239+
.then(function() { _assert('arrayOk align:right left sector', {textAnchor: 'end', posX: 37.45}); })
1240+
.then(_hoverRight)
1241+
.then(function() { _assert('arrayOk align:left right sector', {textAnchor: 'start', posX: -37.45}); })
1242+
.catch(failTest)
1243+
.then(done);
1244+
});
11901245
});
11911246

11921247
describe('should fit labels within graph div', function() {

0 commit comments

Comments
 (0)