Skip to content

Commit b053629

Browse files
committed
add support for per-pt hoverlabel setting in pie traces
1 parent 4ec88ef commit b053629

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

src/traces/pie/plot.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,20 @@ module.exports = function plot(gd, cdpie) {
124124
if(hoverinfo.indexOf('value') !== -1) thisText.push(helpers.formatPieValue(pt.v, separators));
125125
if(hoverinfo.indexOf('percent') !== -1) thisText.push(helpers.formatPiePercent(pt.v / cd0.vTotal, separators));
126126

127+
var hoverLabelOpts = trace2.hoverlabel;
128+
127129
Fx.loneHover({
128130
x0: hoverCenterX - rInscribed * cd0.r,
129131
x1: hoverCenterX + rInscribed * cd0.r,
130132
y: hoverCenterY,
131133
text: thisText.join('<br>'),
132134
name: hoverinfo.indexOf('name') !== -1 ? trace2.name : undefined,
133-
color: pt.color,
134-
idealAlign: pt.pxmid[0] < 0 ? 'left' : 'right'
135+
idealAlign: pt.pxmid[0] < 0 ? 'left' : 'right',
136+
color: pt.hbg || hoverLabelOpts.bgcolor || pt.color,
137+
borderColor: pt.hbc || hoverLabelOpts.bordercolor,
138+
fontFamily: pt.htf || hoverLabelOpts.font.family,
139+
fontSize: pt.hts || hoverLabelOpts.font.size,
140+
fontColor: pt.htc || hoverLabelOpts.font.color
135141
}, {
136142
container: fullLayout2._hoverlayer.node(),
137143
outerContainer: fullLayout2._paper.node()

test/jasmine/tests/hover_pie_test.js

+38-6
Original file line numberDiff line numberDiff line change
@@ -187,23 +187,39 @@ describe('pie hovering', function() {
187187

188188
function _hover() {
189189
mouseEvent('mouseover', 223, 143);
190+
delete gd._lastHoverTime;
190191
}
191192

192-
function assertLabel(expected) {
193-
var labels = d3.selectAll('.hovertext .nums .line');
193+
function assertLabel(content, style) {
194+
var g = d3.selectAll('.hovertext');
195+
var lines = g.selectAll('.nums .line');
194196

195-
expect(labels.size()).toBe(expected.length);
197+
expect(lines.size()).toBe(content.length);
196198

197-
labels.each(function(_, i) {
198-
expect(d3.select(this).text()).toBe(expected[i]);
199+
lines.each(function(_, i) {
200+
expect(d3.select(this).text()).toBe(content[i]);
199201
});
202+
203+
if(style) {
204+
var path = g.select('path');
205+
expect(path.style('fill')).toEqual(style[0], 'bgcolor');
206+
expect(path.style('stroke')).toEqual(style[1], 'bordercolor');
207+
208+
var text = g.select('text');
209+
expect(parseInt(text.style('font-size'))).toEqual(style[2], 'font.size');
210+
expect(text.style('font-family').split(',')[0]).toEqual(style[3], 'font.family');
211+
expect(text.style('fill')).toEqual(style[4], 'font.color');
212+
}
200213
}
201214

202215
it('should show the default selected values', function(done) {
203216
Plotly.plot(gd, mockCopy.data, mockCopy.layout)
204217
.then(_hover)
205218
.then(function() {
206-
assertLabel(['4', '5', '33.3%']);
219+
assertLabel(
220+
['4', '5', '33.3%'],
221+
['rgb(31, 119, 180)', 'rgb(255, 255, 255)', 13, 'Arial', 'rgb(255, 255, 255)']
222+
);
207223

208224
return Plotly.restyle(gd, 'text', [['A', 'B', 'C', 'D', 'E']]);
209225
})
@@ -224,7 +240,23 @@ describe('pie hovering', function() {
224240
.then(_hover)
225241
.then(function() {
226242
assertLabel(['4', 'SUP', '5', '33.3%']);
243+
244+
return Plotly.restyle(gd, {
245+
'hoverlabel.bgcolor': [['red', 'green', 'blue']],
246+
'hoverlabel.bordercolor': 'yellow',
247+
'hoverlabel.font.size': [[15, 20, 30]],
248+
'hoverlabel.font.family': 'Roboto',
249+
'hoverlabel.font.color': 'blue'
250+
});
251+
})
252+
.then(_hover)
253+
.then(function() {
254+
assertLabel(
255+
['4', 'SUP', '5', '33.3%'],
256+
['rgb(255, 0, 0)', 'rgb(255, 255, 0)', 15, 'Roboto', 'rgb(0, 0, 255)']
257+
);
227258
})
259+
.catch(fail)
228260
.then(done);
229261
});
230262

0 commit comments

Comments
 (0)