Skip to content

Commit 5f25643

Browse files
committed
Pie: Use numSeparate on hover and text labels for values
1 parent c011555 commit 5f25643

File tree

4 files changed

+72
-10
lines changed

4 files changed

+72
-10
lines changed

src/traces/pie/calc.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,15 @@ module.exports = function calc(gd, trace) {
107107
hasText = trace.textinfo.indexOf('text') !== -1,
108108
hasValue = trace.textinfo.indexOf('value') !== -1,
109109
hasPercent = trace.textinfo.indexOf('percent') !== -1,
110+
separators = fullLayout.separators,
110111
thisText;
111112

112113
for(i = 0; i < cd.length; i++) {
113114
pt = cd[i];
114115
thisText = hasLabel ? [pt.label] : [];
115116
if(hasText && trace.text[pt.i]) thisText.push(trace.text[pt.i]);
116-
if(hasValue) thisText.push(helpers.formatPieValue(pt.v));
117-
if(hasPercent) thisText.push(helpers.formatPiePercent(pt.v / vTotal));
117+
if(hasValue) thisText.push(helpers.formatPieValue(pt.v, separators));
118+
if(hasPercent) thisText.push(helpers.formatPiePercent(pt.v / vTotal, separators));
118119
pt.text = thisText.join('<br>');
119120
}
120121
}

src/traces/pie/helpers.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88

99
'use strict';
1010

11-
exports.formatPiePercent = function formatPiePercent(v) {
11+
var Lib = require('../../lib');
12+
13+
exports.formatPiePercent = function formatPiePercent(v, separators) {
1214
var vRounded = (v * 100).toPrecision(3);
13-
if(vRounded.indexOf('.') !== -1) return vRounded.replace(/[.]?0+$/,'') + '%';
14-
return vRounded + '%';
15+
if(vRounded.lastIndexOf('.') !== -1) {
16+
vRounded = vRounded.replace(/[.]?0+$/, '');
17+
}
18+
return Lib.numSeparate(vRounded, separators) + '%';
1519
};
1620

17-
exports.formatPieValue = function formatPieValue(v) {
21+
exports.formatPieValue = function formatPieValue(v, separators) {
1822
var vRounded = v.toPrecision(10);
19-
if(vRounded.indexOf('.') !== -1) return vRounded.replace(/[.]?0+$/,'');
20-
return vRounded;
23+
if(vRounded.lastIndexOf('.') !== -1) {
24+
vRounded = vRounded.replace(/[.]?0+$/, '');
25+
}
26+
return Lib.numSeparate(vRounded, separators);
2127
};

src/traces/pie/plot.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ module.exports = function plot(gd, cdpie) {
103103
var rInscribed = getInscribedRadiusFraction(pt, cd0),
104104
hoverCenterX = cx + pt.pxmid[0] * (1 - rInscribed),
105105
hoverCenterY = cy + pt.pxmid[1] * (1 - rInscribed),
106+
separators = fullLayout.separators,
106107
thisText = [];
108+
107109
if(hoverinfo.indexOf('label') !== -1) thisText.push(pt.label);
108110
if(trace2.text && trace2.text[pt.i] && hoverinfo.indexOf('text') !== -1) {
109111
thisText.push(trace2.text[pt.i]);
110112
}
111-
if(hoverinfo.indexOf('value') !== -1) thisText.push(helpers.formatPieValue(pt.v));
112-
if(hoverinfo.indexOf('percent') !== -1) thisText.push(helpers.formatPiePercent(pt.v / cd0.vTotal));
113+
if(hoverinfo.indexOf('value') !== -1) thisText.push(helpers.formatPieValue(pt.v, separators));
114+
if(hoverinfo.indexOf('percent') !== -1) thisText.push(helpers.formatPiePercent(pt.v / cd0.vTotal, separators));
113115

114116
Fx.loneHover({
115117
x0: hoverCenterX - rInscribed * cd0.r,

test/jasmine/tests/hover_pie_test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,57 @@ describe('pie hovering', function() {
111111
}, 100);
112112
});
113113
});
114+
115+
describe('labels', function() {
116+
117+
var gd,
118+
mockCopy;
119+
120+
beforeEach(function() {
121+
gd = createGraphDiv();
122+
mockCopy = Lib.extendDeep({}, mock);
123+
});
124+
125+
afterEach(destroyGraphDiv);
126+
127+
it('should show the default selected values', function(done) {
128+
129+
var expected = ['4', '5', '33.3%'];
130+
131+
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() {
132+
133+
mouseEvent('mouseover', 230, 150);
134+
135+
var labels = Plotly.d3.selectAll('.hovertext .nums .line');
136+
137+
expect(labels[0].length).toBe(3);
138+
139+
labels.each(function(_, i) {
140+
expect(Plotly.d3.select(this).text()).toBe(expected[i]);
141+
});
142+
}).then(done);
143+
});
144+
145+
it('should show the correct separators for values', function(done) {
146+
147+
var expected = ['0', '12|345|678@91', '99@9%'];
148+
149+
mockCopy.layout.separators = '@|';
150+
mockCopy.data[0].values[0] = 12345678.912;
151+
mockCopy.data[0].values[1] = 10000;
152+
153+
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() {
154+
155+
mouseEvent('mouseover', 230, 150);
156+
157+
var labels = Plotly.d3.selectAll('.hovertext .nums .line');
158+
159+
expect(labels[0].length).toBe(3);
160+
161+
labels.each(function(_, i) {
162+
expect(Plotly.d3.select(this).text()).toBe(expected[i]);
163+
});
164+
}).then(done);
165+
});
166+
});
114167
});

0 commit comments

Comments
 (0)