Skip to content

Commit 5092c19

Browse files
committed
apply latest d3 number format instead of d3 v3 format
- centralize and wrap require d3 format in lib - adjust old and new formats
1 parent ec01107 commit 5092c19

File tree

11 files changed

+64
-19
lines changed

11 files changed

+64
-19
lines changed

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"d3-force": "^1.2.1",
8585
"d3-geo": "^1.12.1",
8686
"d3-geo-projection": "^2.9.0",
87+
"d3-format": "^1.4.5",
8788
"d3-hierarchy": "^1.1.9",
8889
"d3-interpolate": "^1.4.0",
8990
"d3-time-format": "^2.2.3",

src/components/drawing/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict';
22

33
var d3 = require('@plotly/d3');
4+
var Lib = require('../../lib');
5+
var numberFormat = Lib.numberFormat;
46
var isNumeric = require('fast-isnumeric');
57
var tinycolor = require('tinycolor2');
68

79
var Registry = require('../../registry');
810
var Color = require('../color');
911
var Colorscale = require('../colorscale');
10-
var Lib = require('../../lib');
1112
var strTranslate = Lib.strTranslate;
1213
var svgTextUtils = require('../../lib/svg_text_utils');
1314

@@ -275,7 +276,7 @@ function makePointPath(symbolNumber, r) {
275276

276277
var HORZGRADIENT = {x1: 1, x2: 0, y1: 0, y2: 0};
277278
var VERTGRADIENT = {x1: 0, x2: 0, y1: 1, y2: 0};
278-
var stopFormatter = d3.format('~.1f');
279+
var stopFormatter = numberFormat('~f');
279280
var gradientInfo = {
280281
radial: {node: 'radialGradient'},
281282
radialreversed: {node: 'radialGradient', reversed: true},

src/constants/docs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

33
module.exports = {
4-
FORMAT_LINK: 'https://github.com/d3/d3-3.x-api-reference/blob/master/Formatting.md#d3_format',
4+
FORMAT_LINK: 'https://github.com/d3/d3-format#format',
55
DATE_FORMAT_LINK: 'https://github.com/d3/d3-time-format#locale_format'
66
};

src/lib/index.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@ var BADNUM = numConstants.BADNUM;
1111

1212
var lib = module.exports = {};
1313

14+
lib.adjustFormat = function(a) {
15+
if(!a) return a;
16+
var b = a;
17+
if(b === '0.f') return '~f';
18+
if(/^[0123456789].[0123456789]f/.test(b)) return a;
19+
if(/.[0123456789]%/.test(b)) return a;
20+
if(/^[0123456789]%/.test(b)) return '~%';
21+
if(/^[0123456789]s/.test(b)) return '~s';
22+
if(!(/^[~,.0$]/.test(b)) && /[&fps]/.test(b)) {
23+
// try adding tilde to the start of format in order to trim
24+
b = '~' + b;
25+
}
26+
return b;
27+
};
28+
29+
var d3Format = require('d3-format').format;
30+
var numberFormat = function(a) {
31+
var b = lib.adjustFormat(a);
32+
33+
// console.log('"' + a + '" > "' + b + '"');
34+
35+
return d3Format(b);
36+
};
37+
lib.numberFormat = numberFormat;
38+
1439
lib.nestedProperty = require('./nested_property');
1540
lib.keyedContainer = require('./keyed_container');
1641
lib.relativeAttr = require('./relative_attr');
@@ -1118,7 +1143,7 @@ function templateFormatString(string, labels, d3locale) {
11181143
if(format) {
11191144
var fmt;
11201145
if(format[0] === ':') {
1121-
fmt = d3locale ? d3locale.numberFormat : d3.format;
1146+
fmt = d3locale ? d3locale.numberFormat : numberFormat;
11221147
value = fmt(format.replace(TEMPLATE_STRING_FORMAT_SEPARATOR, ''))(value);
11231148
}
11241149

src/plots/cartesian/dragbox.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
22

33
var d3 = require('@plotly/d3');
4+
var Lib = require('../../lib');
5+
var numberFormat = Lib.numberFormat;
46
var tinycolor = require('tinycolor2');
57
var supportsPassive = require('has-passive-events');
68

79
var Registry = require('../../registry');
8-
var Lib = require('../../lib');
910
var strTranslate = Lib.strTranslate;
1011
var svgTextUtils = require('../../lib/svg_text_utils');
1112
var Color = require('../../components/color');
@@ -1029,11 +1030,11 @@ function getEndText(ax, end) {
10291030
return initialVal;
10301031
} else if(ax.type === 'log') {
10311032
dig = Math.ceil(Math.max(0, -Math.log(diff) / Math.LN10)) + 3;
1032-
return d3.format('.' + dig + 'g')(Math.pow(10, initialVal));
1033+
return numberFormat('.' + dig + 'g')(Math.pow(10, initialVal));
10331034
} else { // linear numeric (or category... but just show numbers here)
10341035
dig = Math.floor(Math.log(Math.abs(initialVal)) / Math.LN10) -
10351036
Math.floor(Math.log(diff) / Math.LN10) + 4;
1036-
return d3.format('.' + String(dig) + 'g')(initialVal);
1037+
return numberFormat('.' + String(dig) + 'g')(initialVal);
10371038
}
10381039
}
10391040

src/plots/cartesian/set_convert.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
var d3 = require('@plotly/d3');
44
var utcFormat = require('d3-time-format').utcFormat;
5+
var Lib = require('../../lib');
6+
var numberFormat = Lib.numberFormat;
57
var isNumeric = require('fast-isnumeric');
68

7-
var Lib = require('../../lib');
89
var cleanNumber = Lib.cleanNumber;
910
var ms2DateTime = Lib.ms2DateTime;
1011
var dateTime2ms = Lib.dateTime2ms;
@@ -953,7 +954,7 @@ module.exports = function setConvert(ax, fullLayout) {
953954
// occasionally we need _numFormat to pass through
954955
// even though it won't be needed by this axis
955956
ax._separators = fullLayout.separators;
956-
ax._numFormat = locale ? locale.numberFormat : d3.format;
957+
ax._numFormat = locale ? locale.numberFormat : numberFormat;
957958

958959
// and for bar charts and box plots: reset forced minimum tick spacing
959960
delete ax._minDtick;

src/plots/plots.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var d3 = require('@plotly/d3');
44
var timeFormatLocale = require('d3-time-format').timeFormatLocale;
5+
var formatLocale = require('d3-format').formatLocale;
56
var isNumeric = require('fast-isnumeric');
67

78
var Registry = require('../registry');
@@ -702,7 +703,13 @@ function getFormatter(formatObj, separators) {
702703
formatObj.thousands = separators.charAt(1);
703704

704705
return {
705-
numberFormat: d3.locale(formatObj).numberFormat,
706+
numberFormat: function(a) {
707+
var b = Lib.adjustFormat(a);
708+
709+
// console.log('"' + a + '" > "' + b + '"');
710+
711+
return formatLocale(formatObj).format(b);
712+
},
706713
timeFormat: timeFormatLocale(formatObj).utcFormat
707714
};
708715
}

src/traces/parcoords/parcoords.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use strict';
22

33
var d3 = require('@plotly/d3');
4+
var Lib = require('../../lib');
5+
var numberFormat = Lib.numberFormat;
46
var rgba = require('color-rgba');
57

68
var Axes = require('../../plots/cartesian/axes');
7-
var Lib = require('../../lib');
89
var strRotate = Lib.strRotate;
910
var strTranslate = Lib.strTranslate;
1011
var svgTextUtils = require('../../lib/svg_text_utils');
@@ -79,7 +80,7 @@ function domainScale(height, padding, dimension, tickvals, ticktext) {
7980
var extent = dimensionExtent(dimension);
8081
if(tickvals) {
8182
return d3.scale.ordinal()
82-
.domain(tickvals.map(toText(d3.format(dimension.tickformat), ticktext)))
83+
.domain(tickvals.map(toText(numberFormat(dimension.tickformat), ticktext)))
8384
.range(tickvals
8485
.map(function(d) {
8586
var unitVal = (d - extent[0]) / (extent[1] - extent[0]);
@@ -266,7 +267,7 @@ function viewModel(state, callbacks, model) {
266267

267268
// ensure ticktext and tickvals have same length
268269
if(!Array.isArray(ticktext) || !ticktext.length) {
269-
ticktext = tickvals.map(d3.format(dimension.tickformat));
270+
ticktext = tickvals.map(numberFormat(dimension.tickformat));
270271
} else if(ticktext.length > tickvals.length) {
271272
ticktext = ticktext.slice(0, tickvals.length);
272273
} else if(tickvals.length > ticktext.length) {

src/traces/sankey/plot.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use strict';
22

33
var d3 = require('@plotly/d3');
4+
var Lib = require('../../lib');
5+
var numberFormat = Lib.numberFormat;
46
var render = require('./render');
57
var Fx = require('../../components/fx');
68
var Color = require('../../components/color');
7-
var Lib = require('../../lib');
89
var cn = require('./constants').cn;
910

1011
var _ = Lib._;
@@ -192,7 +193,7 @@ module.exports = function plot(gd, calcData) {
192193
link.fullData = link.trace;
193194
obj = d.link.trace.link;
194195
var hoverCenter = hoverCenterPosition(link);
195-
var hovertemplateLabels = {valueLabel: d3.format(d.valueFormat)(link.value) + d.valueSuffix};
196+
var hovertemplateLabels = {valueLabel: numberFormat(d.valueFormat)(link.value) + d.valueSuffix};
196197

197198
hoverItems.push({
198199
x: hoverCenter[0],
@@ -202,7 +203,7 @@ module.exports = function plot(gd, calcData) {
202203
link.label || '',
203204
sourceLabel + link.source.label,
204205
targetLabel + link.target.label,
205-
link.concentrationscale ? concentrationLabel + d3.format('%0.2f')(link.flow.labelConcentration) : ''
206+
link.concentrationscale ? concentrationLabel + numberFormat('%0.2f')(link.flow.labelConcentration) : ''
206207
].filter(renderableValuePresent).join('<br>'),
207208
color: castHoverOption(obj, 'bgcolor') || Color.addOpacity(link.color, 1),
208209
borderColor: castHoverOption(obj, 'bordercolor'),
@@ -281,7 +282,7 @@ module.exports = function plot(gd, calcData) {
281282
var hoverCenterX1 = boundingBox.right + 2 - rootBBox.left;
282283
var hoverCenterY = boundingBox.top + boundingBox.height / 4 - rootBBox.top;
283284

284-
var hovertemplateLabels = {valueLabel: d3.format(d.valueFormat)(d.node.value) + d.valueSuffix};
285+
var hovertemplateLabels = {valueLabel: numberFormat(d.valueFormat)(d.node.value) + d.valueSuffix};
285286
d.node.fullData = d.node.trace;
286287

287288
gd._fullLayout._calcInverseTransform(gd);
@@ -292,7 +293,7 @@ module.exports = function plot(gd, calcData) {
292293
x0: scaleX * hoverCenterX0,
293294
x1: scaleX * hoverCenterX1,
294295
y: scaleY * hoverCenterY,
295-
name: d3.format(d.valueFormat)(d.node.value) + d.valueSuffix,
296+
name: numberFormat(d.valueFormat)(d.node.value) + d.valueSuffix,
296297
text: [
297298
d.node.label,
298299
incomingLabel + d.node.targetLinks.length,

src/traces/table/plot.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
var c = require('./constants');
44
var d3 = require('@plotly/d3');
5+
var Lib = require('../../lib');
6+
var numberFormat = Lib.numberFormat;
57
var gup = require('../../lib/gup');
68
var Drawing = require('../../components/drawing');
79
var svgUtil = require('../../lib/svg_text_utils');
@@ -528,7 +530,7 @@ function populateCellText(cellText, tableControlView, allColumnBlock, gd) {
528530
var suffix = latex ? '' : gridPick(d.calcdata.cells.suffix, col, row) || '';
529531
var format = latex ? null : gridPick(d.calcdata.cells.format, col, row) || null;
530532

531-
var prefixSuffixedText = prefix + (format ? d3.format(format)(d.value) : d.value) + suffix;
533+
var prefixSuffixedText = prefix + (format ? numberFormat(format)(d.value) : d.value) + suffix;
532534

533535
var hasWrapSplitCharacter;
534536
d.wrappingNeeded = !d.wrapped && !userBrokenText && !latex && (hasWrapSplitCharacter = hasWrapCharacter(prefixSuffixedText));

0 commit comments

Comments
 (0)