-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add new formatting options by replacing d3.format
method with more recent d3-format
module
#5125
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
Changes from 20 commits
5092c19
cd0d392
c7ecdc7
320b270
3ea8d90
a5fd000
6b93e61
25a30a8
05b221c
a5a94cd
e88a134
d5d282d
68fe2b7
17e73b6
785f128
dc0d9cb
a425925
11a75d9
dc94854
bdb69fc
693ebd7
1efc41e
878034b
80d37d2
518029a
32898a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Add new formatting options by replacing `d3.format` method with more recent `d3-format` module [[#5615](https://github.com/plotly/plotly.js/pull/5615)] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
FORMAT_LINK: 'https://github.com/d3/d3-3.x-api-reference/blob/master/Formatting.md#d3_format', | ||
FORMAT_LINK: 'https://github.com/d3/d3-format/tree/v1.4.5#d3-format', | ||
DATE_FORMAT_LINK: 'https://github.com/d3/d3-time-format/tree/v2.2.3#locale_format' | ||
}; |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -2,15 +2,43 @@ | |||||||
|
||||||||
var d3 = require('@plotly/d3'); | ||||||||
var utcFormat = require('d3-time-format').utcFormat; | ||||||||
var d3Format = require('d3-format').format; | ||||||||
var isNumeric = require('fast-isnumeric'); | ||||||||
|
||||||||
var numConstants = require('../constants/numerical'); | ||||||||
var MAX_SAFE = numConstants.FP_SAFE; | ||||||||
var MIN_SAFE = -MAX_SAFE; | ||||||||
var BADNUM = numConstants.BADNUM; | ||||||||
|
||||||||
var lib = module.exports = { | ||||||||
_numberFormat: d3.format // simply to test d3.format before switching to d3-format | ||||||||
var lib = module.exports = {}; | ||||||||
|
||||||||
lib.adjustFormat = function adjustFormat(formatStr) { | ||||||||
if( | ||||||||
!formatStr || | ||||||||
/^[0123456789].[0123456789]f/.test(formatStr) || | ||||||||
/.[0123456789]%/.test(formatStr) | ||||||||
) return formatStr; | ||||||||
|
||||||||
if(formatStr === '0.f') return '~f'; | ||||||||
if(/^[0123456789]%/.test(formatStr)) return '~%'; | ||||||||
if(/^[0123456789]s/.test(formatStr)) return '~s'; | ||||||||
|
||||||||
// try adding tilde to the start of format in order to trim | ||||||||
if(!(/^[~,.0$]/.test(formatStr)) && /[&fps]/.test(formatStr)) return '~' + formatStr; | ||||||||
|
||||||||
return formatStr; | ||||||||
}; | ||||||||
|
||||||||
lib.noFormat = function(value) { return String(value); }; | ||||||||
|
||||||||
lib.numberFormat = function(formatStr) { | ||||||||
try { | ||||||||
formatStr = d3Format(lib.adjustFormat(formatStr)); | ||||||||
} catch(e) { | ||||||||
return lib.noFormat; | ||||||||
} | ||||||||
|
||||||||
return formatStr; | ||||||||
}; | ||||||||
|
||||||||
lib.nestedProperty = require('./nested_property'); | ||||||||
|
@@ -1120,7 +1148,7 @@ function templateFormatString(string, labels, d3locale) { | |||||||
if(format) { | ||||||||
var fmt; | ||||||||
if(format[0] === ':') { | ||||||||
fmt = d3locale ? d3locale.numberFormat : d3.format; | ||||||||
fmt = d3locale ? d3locale.numberFormat : lib.numberFormat; | ||||||||
value = fmt(format.replace(TEMPLATE_STRING_FORMAT_SEPARATOR, ''))(value); | ||||||||
} | ||||||||
|
||||||||
|
@@ -1319,6 +1347,10 @@ lib.bigFont = function(size) { | |||||||
return Math.round(1.2 * size); | ||||||||
}; | ||||||||
|
||||||||
lib.beginCap = function(str) { | ||||||||
return str.charAt(0).toUpperCase() + str.slice(1); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 723 to 725 in bdb69fc
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 1efc41e |
||||||||
}; | ||||||||
|
||||||||
var firefoxVersion = lib.getFirefoxVersion(); | ||||||||
// see https://bugzilla.mozilla.org/show_bug.cgi?id=1684973 | ||||||||
var isProblematicFirefox = firefoxVersion !== null && firefoxVersion < 86; | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
var d3 = require('@plotly/d3'); | ||
var timeFormatLocale = require('d3-time-format').timeFormatLocale; | ||
var formatLocale = require('d3-format').formatLocale; | ||
var isNumeric = require('fast-isnumeric'); | ||
|
||
var Registry = require('../registry'); | ||
|
@@ -702,7 +703,17 @@ function getFormatter(formatObj, separators) { | |
formatObj.thousands = separators.charAt(1); | ||
|
||
return { | ||
numberFormat: d3.locale(formatObj).numberFormat, | ||
numberFormat: function(formatStr) { | ||
try { | ||
formatStr = formatLocale(formatObj).format( | ||
Lib.adjustFormat(formatStr) | ||
); | ||
} catch(e) { | ||
return Lib.noFormat; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this catching invalid format strings? Do we want to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 878034b |
||
} | ||
|
||
return formatStr; | ||
}, | ||
timeFormat: timeFormatLocale(formatObj).utcFormat | ||
}; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.