Skip to content
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

Feature separatethousands #848

Merged
merged 12 commits into from
Aug 15, 2016
1 change: 1 addition & 0 deletions src/components/colorbar/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ module.exports = {
showtickprefix: axesAttrs.showtickprefix,
ticksuffix: axesAttrs.ticksuffix,
showticksuffix: axesAttrs.showticksuffix,
separatethousands: axesAttrs.separatethousands,
exponentformat: axesAttrs.exponentformat,
showexponent: axesAttrs.showexponent,
title: {
Expand Down
1 change: 1 addition & 0 deletions src/components/colorbar/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ module.exports = function draw(gd, id) {
tickangle: opts.tickangle,
tickformat: opts.tickformat,
exponentformat: opts.exponentformat,
separatethousands: opts.separatethousands,
showexponent: opts.showexponent,
showtickprefix: opts.showtickprefix,
tickprefix: opts.tickprefix,
Expand Down
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: [
'If "true", even 4-digit integers are separated'
].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
1 change: 1 addition & 0 deletions src/plots/gl3d/layout/axis_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ module.exports = {
showticksuffix: axesAttrs.showticksuffix,
showexponent: axesAttrs.showexponent,
exponentformat: axesAttrs.exponentformat,
separatethousands: axesAttrs.separatethousands,
tickformat: axesAttrs.tickformat,
hoverformat: axesAttrs.hoverformat,
// lines and grids
Expand Down
1 change: 1 addition & 0 deletions src/plots/ternary/layout/axis_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = {
ticksuffix: axesAttrs.ticksuffix,
showexponent: axesAttrs.showexponent,
exponentformat: axesAttrs.exponentformat,
separatethousands: axesAttrs.separatethousands,
tickfont: axesAttrs.tickfont,
tickangle: axesAttrs.tickangle,
tickformat: axesAttrs.tickformat,
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions test/image/mocks/axislabel_separatethousands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"data": [
{
"x": [1000, 2000, 3000, 4000],
"y": [1000, 1500, 2000, 10000],
"type": "scatter"
}
],
"layout": {
"yaxis": {
"exponentformat": "none",
"separatethousands": true
}
}
}
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