Skip to content

Commit ad88139

Browse files
authored
Merge pull request #848 from satotake/feature-separatethousands
Feature separatethousands
2 parents acff0da + f2e710f commit ad88139

File tree

11 files changed

+43
-4
lines changed

11 files changed

+43
-4
lines changed

src/components/colorbar/attributes.js

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ module.exports = {
167167
showtickprefix: axesAttrs.showtickprefix,
168168
ticksuffix: axesAttrs.ticksuffix,
169169
showticksuffix: axesAttrs.showticksuffix,
170+
separatethousands: axesAttrs.separatethousands,
170171
exponentformat: axesAttrs.exponentformat,
171172
showexponent: axesAttrs.showexponent,
172173
title: {

src/components/colorbar/draw.js

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ module.exports = function draw(gd, id) {
158158
tickangle: opts.tickangle,
159159
tickformat: opts.tickformat,
160160
exponentformat: opts.exponentformat,
161+
separatethousands: opts.separatethousands,
161162
showexponent: opts.showexponent,
162163
showtickprefix: opts.showtickprefix,
163164
tickprefix: opts.tickprefix,

src/lib/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -581,15 +581,21 @@ lib.objectFromPath = function(path, value) {
581581
* // returns '2016'
582582
*
583583
* @example
584+
* lib.numSeparate(3000, '.,', true);
585+
* // returns '3,000'
586+
*
587+
* @example
584588
* lib.numSeparate(1234.56, '|,')
585589
* // returns '1,234|56'
586590
*
587591
* @param {string|number} value the value to be converted
588592
* @param {string} separators string of decimal, then thousands separators
593+
* @param {boolean} separatethousands boolean, 4-digit integers are separated if true
589594
*
590595
* @return {string} the value that has been separated
591596
*/
592-
lib.numSeparate = function(value, separators) {
597+
lib.numSeparate = function(value, separators, separatethousands) {
598+
if(!separatethousands) separatethousands = false;
593599

594600
if(typeof separators !== 'string' || separators.length === 0) {
595601
throw new Error('Separator string required for formatting!');
@@ -608,7 +614,7 @@ lib.numSeparate = function(value, separators) {
608614
x2 = x.length > 1 ? decimalSep + x[1] : '';
609615

610616
// Years are ignored for thousands separators
611-
if(thouSep && (x.length > 1 || x1.length > 4)) {
617+
if(thouSep && (x.length > 1 || x1.length > 4 || separatethousands)) {
612618
while(thousandsRe.test(x1)) {
613619
x1 = x1.replace(thousandsRe, '$1' + thouSep + '$2');
614620
}

src/plots/cartesian/axes.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,8 @@ function numFormat(v, ax, fmtoverride, hover) {
10921092
tickRound = ax._tickround,
10931093
exponentFormat = fmtoverride || ax.exponentformat || 'B',
10941094
exponent = ax._tickexponent,
1095-
tickformat = ax.tickformat;
1095+
tickformat = ax.tickformat,
1096+
separatethousands = ax.separatethousands;
10961097

10971098
// special case for hover: set exponent just for this value, and
10981099
// add a couple more digits of precision over tick labels
@@ -1156,7 +1157,7 @@ function numFormat(v, ax, fmtoverride, hover) {
11561157
if(dp) v = v.substr(0, dp + tickRound).replace(/\.?0+$/, '');
11571158
}
11581159
// insert appropriate decimal point and thousands separator
1159-
v = Lib.numSeparate(v, ax._gd._fullLayout.separators);
1160+
v = Lib.numSeparate(v, ax._gd._fullLayout.separators, separatethousands);
11601161
}
11611162

11621163
// add exponent

src/plots/cartesian/layout_attributes.js

+8
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,14 @@ module.exports = {
310310
'If *B*, 1B.'
311311
].join(' ')
312312
},
313+
separatethousands: {
314+
valType: 'boolean',
315+
dflt: false,
316+
role: 'style',
317+
description: [
318+
'If "true", even 4-digit integers are separated'
319+
].join(' ')
320+
},
313321
tickformat: {
314322
valType: 'string',
315323
dflt: '',

src/plots/cartesian/tick_label_defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module.exports = function handleTickLabelDefaults(containerIn, containerOut, coe
4343
if(!tickFormat && axType !== 'date') {
4444
coerce('showexponent', showAttrDflt);
4545
coerce('exponentformat');
46+
coerce('separatethousands');
4647
}
4748
}
4849
}

src/plots/gl3d/layout/axis_attributes.js

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ module.exports = {
9898
showticksuffix: axesAttrs.showticksuffix,
9999
showexponent: axesAttrs.showexponent,
100100
exponentformat: axesAttrs.exponentformat,
101+
separatethousands: axesAttrs.separatethousands,
101102
tickformat: axesAttrs.tickformat,
102103
hoverformat: axesAttrs.hoverformat,
103104
// lines and grids

src/plots/ternary/layout/axis_attributes.js

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = {
3535
ticksuffix: axesAttrs.ticksuffix,
3636
showexponent: axesAttrs.showexponent,
3737
exponentformat: axesAttrs.exponentformat,
38+
separatethousands: axesAttrs.separatethousands,
3839
tickfont: axesAttrs.tickfont,
3940
tickangle: axesAttrs.tickangle,
4041
tickformat: axesAttrs.tickformat,
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"data": [
3+
{
4+
"x": [1000, 2000, 3000, 4000],
5+
"y": [1000, 1500, 2000, 10000],
6+
"type": "scatter"
7+
}
8+
],
9+
"layout": {
10+
"yaxis": {
11+
"exponentformat": "none",
12+
"separatethousands": true
13+
}
14+
}
15+
}

test/jasmine/tests/lib_test.js

+4
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,10 @@ describe('Test lib.js:', function() {
13151315
expect(Lib.numSeparate(2016, '.,')).toBe('2016');
13161316
});
13171317

1318+
it('should work even for 4-digit integer if third argument is true', function() {
1319+
expect(Lib.numSeparate(3000, '.,', true)).toBe('3,000');
1320+
});
1321+
13181322
it('should work for multiple thousands', function() {
13191323
expect(Lib.numSeparate(1000000000, '.,')).toBe('1,000,000,000');
13201324
});

0 commit comments

Comments
 (0)