Skip to content

Commit 10f08da

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 a232c90 commit 10f08da

File tree

12 files changed

+64
-20
lines changed

12 files changed

+64
-20
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
@@ -72,6 +72,7 @@
7272
"country-regex": "^1.1.0",
7373
"d3": "^3.5.17",
7474
"d3-force": "^1.2.1",
75+
"d3-format": "^1.4.5",
7576
"d3-hierarchy": "^1.1.9",
7677
"d3-interpolate": "^1.4.0",
7778
"d3-time-format": "^2.2.3",

src/components/drawing/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
'use strict';
1111

1212
var d3 = require('d3');
13+
var Lib = require('../../lib');
14+
var numberFormat = Lib.numberFormat;
1315
var isNumeric = require('fast-isnumeric');
1416
var tinycolor = require('tinycolor2');
1517

1618
var Registry = require('../../registry');
1719
var Color = require('../color');
1820
var Colorscale = require('../colorscale');
19-
var Lib = require('../../lib');
2021
var strTranslate = Lib.strTranslate;
2122
var svgTextUtils = require('../../lib/svg_text_utils');
2223

@@ -284,7 +285,7 @@ function makePointPath(symbolNumber, r) {
284285

285286
var HORZGRADIENT = {x1: 1, x2: 0, y1: 0, y2: 0};
286287
var VERTGRADIENT = {x1: 0, x2: 0, y1: 1, y2: 0};
287-
var stopFormatter = d3.format('~.1f');
288+
var stopFormatter = numberFormat('~f');
288289
var gradientInfo = {
289290
radial: {node: 'radialGradient'},
290291
radialreversed: {node: 'radialGradient', reversed: true},

src/constants/docs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
'use strict';
1010

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

src/lib/index.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
'use strict';
1010

11-
var d3 = require('d3');
1211
var utcFormat = require('d3-time-format').utcFormat;
1312
var isNumeric = require('fast-isnumeric');
1413

@@ -18,6 +17,31 @@ var BADNUM = numConstants.BADNUM;
1817

1918
var lib = module.exports = {};
2019

20+
lib.adjustFormat = function(a) {
21+
if(!a) return a;
22+
var b = a;
23+
if(b === '0.f') return '~f';
24+
if(/^[0123456789].[0123456789]f/.test(b)) return a;
25+
if(/.[0123456789]%/.test(b)) return a;
26+
if(/^[0123456789]%/.test(b)) return '~%';
27+
if(/^[0123456789]s/.test(b)) return '~s';
28+
if(!(/^[~,.0$]/.test(b)) && /[&fps]/.test(b)) {
29+
// try adding tilde to the start of format in order to trim
30+
b = '~' + b;
31+
}
32+
return b;
33+
};
34+
35+
var d3Format = require('d3-format').format;
36+
var numberFormat = function(a) {
37+
var b = lib.adjustFormat(a);
38+
39+
// console.log('"' + a + '" > "' + b + '"');
40+
41+
return d3Format(b);
42+
};
43+
lib.numberFormat = numberFormat;
44+
2145
lib.nestedProperty = require('./nested_property');
2246
lib.keyedContainer = require('./keyed_container');
2347
lib.relativeAttr = require('./relative_attr');
@@ -1085,7 +1109,7 @@ function templateFormatString(string, labels, d3locale) {
10851109
if(format) {
10861110
var fmt;
10871111
if(format[0] === ':') {
1088-
fmt = d3locale ? d3locale.numberFormat : d3.format;
1112+
fmt = d3locale ? d3locale.numberFormat : numberFormat;
10891113
value = fmt(format.replace(TEMPLATE_STRING_FORMAT_SEPARATOR, ''))(value);
10901114
}
10911115

src/plots/cartesian/dragbox.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
'use strict';
1010

1111
var d3 = require('d3');
12+
var Lib = require('../../lib');
13+
var numberFormat = Lib.numberFormat;
1214
var tinycolor = require('tinycolor2');
1315
var supportsPassive = require('has-passive-events');
1416

1517
var Registry = require('../../registry');
16-
var Lib = require('../../lib');
1718
var strTranslate = Lib.strTranslate;
1819
var svgTextUtils = require('../../lib/svg_text_utils');
1920
var Color = require('../../components/color');
@@ -984,11 +985,11 @@ function getEndText(ax, end) {
984985
return initialVal;
985986
} else if(ax.type === 'log') {
986987
dig = Math.ceil(Math.max(0, -Math.log(diff) / Math.LN10)) + 3;
987-
return d3.format('.' + dig + 'g')(Math.pow(10, initialVal));
988+
return numberFormat('.' + dig + 'g')(Math.pow(10, initialVal));
988989
} else { // linear numeric (or category... but just show numbers here)
989990
dig = Math.floor(Math.log(Math.abs(initialVal)) / Math.LN10) -
990991
Math.floor(Math.log(diff) / Math.LN10) + 4;
991-
return d3.format('.' + String(dig) + 'g')(initialVal);
992+
return numberFormat('.' + String(dig) + 'g')(initialVal);
992993
}
993994
}
994995

src/plots/cartesian/set_convert.js

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

1111
var d3 = require('d3');
1212
var utcFormat = require('d3-time-format').utcFormat;
13+
var Lib = require('../../lib');
14+
var numberFormat = Lib.numberFormat;
1315
var isNumeric = require('fast-isnumeric');
1416

15-
var Lib = require('../../lib');
1617
var cleanNumber = Lib.cleanNumber;
1718
var ms2DateTime = Lib.ms2DateTime;
1819
var dateTime2ms = Lib.dateTime2ms;
@@ -965,7 +966,7 @@ module.exports = function setConvert(ax, fullLayout) {
965966
// occasionally we need _numFormat to pass through
966967
// even though it won't be needed by this axis
967968
ax._separators = fullLayout.separators;
968-
ax._numFormat = locale ? locale.numberFormat : d3.format;
969+
ax._numFormat = locale ? locale.numberFormat : numberFormat;
969970

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

src/plots/plots.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
var d3 = require('d3');
1212
var timeFormatLocale = require('d3-time-format').timeFormatLocale;
13+
var formatLocale = require('d3-format').formatLocale;
1314
var isNumeric = require('fast-isnumeric');
1415

1516
var Registry = require('../registry');
@@ -725,7 +726,13 @@ function getFormatter(formatObj, separators) {
725726
formatObj.thousands = separators.charAt(1);
726727

727728
return {
728-
numberFormat: d3.locale(formatObj).numberFormat,
729+
numberFormat: function(a) {
730+
var b = Lib.adjustFormat(a);
731+
732+
// console.log('"' + a + '" > "' + b + '"');
733+
734+
return formatLocale(formatObj).format(b);
735+
},
729736
timeFormat: timeFormatLocale(formatObj).utcFormat
730737
};
731738
}

src/traces/parcoords/parcoords.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
'use strict';
1010

1111
var d3 = require('d3');
12+
var Lib = require('../../lib');
13+
var numberFormat = Lib.numberFormat;
1214
var rgba = require('color-rgba');
1315

1416
var Axes = require('../../plots/cartesian/axes');
15-
var Lib = require('../../lib');
1617
var strRotate = Lib.strRotate;
1718
var strTranslate = Lib.strTranslate;
1819
var svgTextUtils = require('../../lib/svg_text_utils');
@@ -87,7 +88,7 @@ function domainScale(height, padding, dimension, tickvals, ticktext) {
8788
var extent = dimensionExtent(dimension);
8889
if(tickvals) {
8990
return d3.scale.ordinal()
90-
.domain(tickvals.map(toText(d3.format(dimension.tickformat), ticktext)))
91+
.domain(tickvals.map(toText(numberFormat(dimension.tickformat), ticktext)))
9192
.range(tickvals
9293
.map(function(d) {
9394
var unitVal = (d - extent[0]) / (extent[1] - extent[0]);
@@ -274,7 +275,7 @@ function viewModel(state, callbacks, model) {
274275

275276
// ensure ticktext and tickvals have same length
276277
if(!Array.isArray(ticktext) || !ticktext.length) {
277-
ticktext = tickvals.map(d3.format(dimension.tickformat));
278+
ticktext = tickvals.map(numberFormat(dimension.tickformat));
278279
} else if(ticktext.length > tickvals.length) {
279280
ticktext = ticktext.slice(0, tickvals.length);
280281
} else if(tickvals.length > ticktext.length) {

src/traces/sankey/plot.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
'use strict';
1010

1111
var d3 = require('d3');
12+
var Lib = require('../../lib');
13+
var numberFormat = Lib.numberFormat;
1214
var render = require('./render');
1315
var Fx = require('../../components/fx');
1416
var Color = require('../../components/color');
15-
var Lib = require('../../lib');
1617
var cn = require('./constants').cn;
1718

1819
var _ = Lib._;
@@ -200,7 +201,7 @@ module.exports = function plot(gd, calcData) {
200201
link.fullData = link.trace;
201202
obj = d.link.trace.link;
202203
var hoverCenter = hoverCenterPosition(link);
203-
var hovertemplateLabels = {valueLabel: d3.format(d.valueFormat)(link.value) + d.valueSuffix};
204+
var hovertemplateLabels = {valueLabel: numberFormat(d.valueFormat)(link.value) + d.valueSuffix};
204205

205206
hoverItems.push({
206207
x: hoverCenter[0],
@@ -210,7 +211,7 @@ module.exports = function plot(gd, calcData) {
210211
link.label || '',
211212
sourceLabel + link.source.label,
212213
targetLabel + link.target.label,
213-
link.concentrationscale ? concentrationLabel + d3.format('%0.2f')(link.flow.labelConcentration) : ''
214+
link.concentrationscale ? concentrationLabel + numberFormat('%0.2f')(link.flow.labelConcentration) : ''
214215
].filter(renderableValuePresent).join('<br>'),
215216
color: castHoverOption(obj, 'bgcolor') || Color.addOpacity(link.color, 1),
216217
borderColor: castHoverOption(obj, 'bordercolor'),
@@ -289,14 +290,14 @@ module.exports = function plot(gd, calcData) {
289290
var hoverCenterX1 = boundingBox.right + 2 - rootBBox.left;
290291
var hoverCenterY = boundingBox.top + boundingBox.height / 4 - rootBBox.top;
291292

292-
var hovertemplateLabels = {valueLabel: d3.format(d.valueFormat)(d.node.value) + d.valueSuffix};
293+
var hovertemplateLabels = {valueLabel: numberFormat(d.valueFormat)(d.node.value) + d.valueSuffix};
293294
d.node.fullData = d.node.trace;
294295

295296
var tooltip = Fx.loneHover({
296297
x0: hoverCenterX0,
297298
x1: hoverCenterX1,
298299
y: hoverCenterY,
299-
name: d3.format(d.valueFormat)(d.node.value) + d.valueSuffix,
300+
name: numberFormat(d.valueFormat)(d.node.value) + d.valueSuffix,
300301
text: [
301302
d.node.label,
302303
incomingLabel + d.node.targetLinks.length,

src/traces/table/plot.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
var c = require('./constants');
1212
var d3 = require('d3');
13+
var Lib = require('../../lib');
14+
var numberFormat = Lib.numberFormat;
1315
var gup = require('../../lib/gup');
1416
var Drawing = require('../../components/drawing');
1517
var svgUtil = require('../../lib/svg_text_utils');
@@ -536,7 +538,7 @@ function populateCellText(cellText, tableControlView, allColumnBlock, gd) {
536538
var suffix = latex ? '' : gridPick(d.calcdata.cells.suffix, col, row) || '';
537539
var format = latex ? null : gridPick(d.calcdata.cells.format, col, row) || null;
538540

539-
var prefixSuffixedText = prefix + (format ? d3.format(format)(d.value) : d.value) + suffix;
541+
var prefixSuffixedText = prefix + (format ? numberFormat(format)(d.value) : d.value) + suffix;
540542

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

test/image/baselines/tick-percent.png

-953 Bytes
Loading

0 commit comments

Comments
 (0)