Skip to content

Feature separatethousands #848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 15, 2016
10 changes: 8 additions & 2 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,15 +581,21 @@ lib.objectFromPath = function(path, value) {
* // returns '2016'
*
* @example
* lib.numSeparate(3000, '.,', true);
* // returns '3,000'
*
* @example
* lib.numSeparate(1234.56, '|,')
* // returns '1,234|56'
*
* @param {string|number} value the value to be converted
* @param {string} separators string of decimal, then thousands separators
* @param {boolean} separatethousands boolean, 4-digit integers are separated if true
*
* @return {string} the value that has been separated
*/
lib.numSeparate = function(value, separators) {
lib.numSeparate = function(value, separators, separatethousands) {
if(!separatethousands) separatethousands = false;

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

// Years are ignored for thousands separators
if(thouSep && (x.length > 1 || x1.length > 4)) {
if(thouSep && (x.length > 1 || x1.length > 4 || separatethousands)) {
while(thousandsRe.test(x1)) {
x1 = x1.replace(thousandsRe, '$1' + thouSep + '$2');
}
Expand Down
5 changes: 3 additions & 2 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,8 @@ function numFormat(v, ax, fmtoverride, hover) {
tickRound = ax._tickround,
exponentFormat = fmtoverride || ax.exponentformat || 'B',
exponent = ax._tickexponent,
tickformat = ax.tickformat;
tickformat = ax.tickformat,
separatethousands = ax.separatethousands;

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

// add exponent
Expand Down
8 changes: 8 additions & 0 deletions src/plots/cartesian/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@ module.exports = {
'If *B*, 1B.'
].join(' ')
},
separatethousands: {
valType: 'boolean',
dflt: false,
role: 'style',
description: [
'even 4-digit integers are separated if true'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can make this description a little bit more like the other descriptions found in this file? For instance,

  • Make sure the description starts with a capital letter
  • We prefer starting with If <something>, ... rather than ending with ... , if <something>

These descriptions make up our schema pages, so we need to be as clear as possible.

].join(' ')
},
tickformat: {
valType: 'string',
dflt: '',
Expand Down
1 change: 1 addition & 0 deletions src/plots/cartesian/tick_label_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = function handleTickLabelDefaults(containerIn, containerOut, coe
if(!tickFormat && axType !== 'date') {
coerce('showexponent', showAttrDflt);
coerce('exponentformat');
coerce('separatethousands');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lib.coerce fails if a passed prop (here separatethousands) isn't part of the attribute object used to defined the coerce wrapper function above.

So, we need to make sure that separatethousands is part of all attribute objects that pass through here. I believe this mean adding seperatethousands in:

Moreover, to enable seperatethousands in color bars, you'll need to pass to the mock axis here.

}
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/jasmine/tests/lib_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,10 @@ describe('Test lib.js:', function() {
expect(Lib.numSeparate(2016, '.,')).toBe('2016');
});

it('should work even for 4-digit integer if third argument is true', function() {
expect(Lib.numSeparate(3000, '.,', true)).toBe('3,000');
});

it('should work for multiple thousands', function() {
expect(Lib.numSeparate(1000000000, '.,')).toBe('1,000,000,000');
});
Expand Down