Skip to content

Commit 4148cd4

Browse files
committed
add 'hovertext' attribute to pie traces
1 parent b3448f0 commit 4148cd4

File tree

4 files changed

+77
-33
lines changed

4 files changed

+77
-33
lines changed

src/traces/pie/attributes.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,27 @@ module.exports = {
7979

8080
text: {
8181
valType: 'data_array',
82-
description: 'Sets text elements associated with each sector.'
82+
description: [
83+
'Sets text elements associated with each sector.',
84+
'If trace `textinfo` contains a *text* flag, these elements will seen',
85+
'on the chart.',
86+
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
87+
'these elements will be seen in the hover labels.'
88+
].join(' ')
89+
},
90+
hovertext: {
91+
valType: 'string',
92+
role: 'info',
93+
dflt: '',
94+
arrayOk: true,
95+
description: [
96+
'Sets hover text elements associated with each sector.',
97+
'If a single string, the same string appears for',
98+
'all data points.',
99+
'If an array of string, the items are mapped in order of',
100+
'this trace\'s sectors.',
101+
'To be seen, trace `hoverinfo` must contain a *text* flag.'
102+
].join(' ')
83103
},
84104

85105
// 'see eg:'

src/traces/pie/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
4545

4646
var textData = coerce('text');
4747
var textInfo = coerce('textinfo', Array.isArray(textData) ? 'text+percent' : 'percent');
48+
coerce('hovertext');
4849

4950
coerce('hoverinfo', (layout._dataLength === 1) ? 'label+text+value+percent' : undefined);
5051

src/traces/pie/plot.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,16 @@ module.exports = function plot(gd, cdpie) {
110110
thisText = [];
111111

112112
if(hoverinfo.indexOf('label') !== -1) thisText.push(pt.label);
113-
if(trace2.text && trace2.text[pt.i] && hoverinfo.indexOf('text') !== -1) {
114-
thisText.push(trace2.text[pt.i]);
113+
if(hoverinfo.indexOf('text') !== -1) {
114+
if(trace2.hovertext) {
115+
thisText.push(
116+
Array.isArray(trace2.hovertext) ?
117+
trace2.hovertext[pt.i] :
118+
trace2.hovertext
119+
);
120+
} else if(trace2.text && trace2.text[pt.i]) {
121+
thisText.push(trace2.text[pt.i]);
122+
}
115123
}
116124
if(hoverinfo.indexOf('value') !== -1) thisText.push(helpers.formatPieValue(pt.v, separators));
117125
if(hoverinfo.indexOf('percent') !== -1) thisText.push(helpers.formatPiePercent(pt.v / cd0.vTotal, separators));

test/jasmine/tests/hover_pie_test.js

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var customMatchers = require('../assets/custom_matchers');
66
var createGraphDiv = require('../assets/create_graph_div');
77
var destroyGraphDiv = require('../assets/destroy_graph_div');
88

9+
var d3 = require('d3');
910
var click = require('../assets/click');
1011
var getClientPosition = require('../assets/get_client_position');
1112
var mouseEvent = require('../assets/mouse_event');
@@ -175,9 +176,7 @@ describe('pie hovering', function() {
175176
});
176177

177178
describe('labels', function() {
178-
179-
var gd,
180-
mockCopy;
179+
var gd, mockCopy;
181180

182181
beforeEach(function() {
183182
gd = createGraphDiv();
@@ -186,44 +185,60 @@ describe('pie hovering', function() {
186185

187186
afterEach(destroyGraphDiv);
188187

189-
it('should show the default selected values', function(done) {
190-
191-
var expected = ['4', '5', '33.3%'];
192-
193-
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() {
188+
function _hover() {
189+
mouseEvent('mouseover', 223, 143);
190+
}
194191

195-
mouseEvent('mouseover', 223, 143);
192+
function assertLabel(expected) {
193+
var labels = d3.selectAll('.hovertext .nums .line');
196194

197-
var labels = Plotly.d3.selectAll('.hovertext .nums .line');
195+
expect(labels[0].length).toBe(expected.length);
198196

199-
expect(labels[0].length).toBe(3);
197+
labels.each(function(_, i) {
198+
expect(d3.select(this).text()).toBe(expected[i]);
199+
});
200+
}
200201

201-
labels.each(function(_, i) {
202-
expect(Plotly.d3.select(this).text()).toBe(expected[i]);
203-
});
204-
}).then(done);
202+
it('should show the default selected values', function(done) {
203+
Plotly.plot(gd, mockCopy.data, mockCopy.layout)
204+
.then(_hover)
205+
.then(function() {
206+
assertLabel(['4', '5', '33.3%']);
207+
208+
return Plotly.restyle(gd, 'text', [['A', 'B', 'C', 'D', 'E']]);
209+
})
210+
.then(_hover)
211+
.then(function() {
212+
assertLabel(['4', 'E', '5', '33.3%']);
213+
214+
return Plotly.restyle(gd, 'hovertext', [[
215+
'Apple', 'Banana', 'Clementine', 'Dragon Fruit', 'Eggplant'
216+
]]);
217+
})
218+
.then(_hover)
219+
.then(function() {
220+
assertLabel(['4', 'Eggplant', '5', '33.3%']);
221+
222+
return Plotly.restyle(gd, 'hovertext', 'SUP');
223+
})
224+
.then(_hover)
225+
.then(function() {
226+
assertLabel(['4', 'SUP', '5', '33.3%']);
227+
})
228+
.then(done);
205229
});
206230

207231
it('should show the correct separators for values', function(done) {
208-
209-
var expected = ['0', '12|345|678@91', '99@9%'];
210-
211232
mockCopy.layout.separators = '@|';
212233
mockCopy.data[0].values[0] = 12345678.912;
213234
mockCopy.data[0].values[1] = 10000;
214235

215-
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() {
216-
217-
mouseEvent('mouseover', 223, 143);
218-
219-
var labels = Plotly.d3.selectAll('.hovertext .nums .line');
220-
221-
expect(labels[0].length).toBe(3);
222-
223-
labels.each(function(_, i) {
224-
expect(Plotly.d3.select(this).text()).toBe(expected[i]);
225-
});
226-
}).then(done);
236+
Plotly.plot(gd, mockCopy.data, mockCopy.layout)
237+
.then(_hover)
238+
.then(function() {
239+
assertLabel(['0', '12|345|678@91', '99@9%']);
240+
})
241+
.then(done);
227242
});
228243
});
229244
});

0 commit comments

Comments
 (0)