1 |
|
2 | (function (global, factory) {
|
3 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
4 | typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
5 | (factory((global.d3 = global.d3 || {})));
|
6 | }(this, (function (exports) { 'use strict';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function formatDecimal(x, p) {
|
12 | if ((i = (x = p ? x.toExponential(p - 1) : x.toExponential()).indexOf("e")) < 0) return null;
|
13 | var i, coefficient = x.slice(0, i);
|
14 |
|
15 |
|
16 |
|
17 | return [
|
18 | coefficient.length > 1 ? coefficient[0] + coefficient.slice(2) : coefficient,
|
19 | +x.slice(i + 1)
|
20 | ];
|
21 | }
|
22 |
|
23 | function exponent(x) {
|
24 | return x = formatDecimal(Math.abs(x)), x ? x[1] : NaN;
|
25 | }
|
26 |
|
27 | function formatGroup(grouping, thousands) {
|
28 | return function(value, width) {
|
29 | var i = value.length,
|
30 | t = [],
|
31 | j = 0,
|
32 | g = grouping[0],
|
33 | length = 0;
|
34 |
|
35 | while (i > 0 && g > 0) {
|
36 | if (length + g + 1 > width) g = Math.max(1, width - length);
|
37 | t.push(value.substring(i -= g, i + g));
|
38 | if ((length += g + 1) > width) break;
|
39 | g = grouping[j = (j + 1) % grouping.length];
|
40 | }
|
41 |
|
42 | return t.reverse().join(thousands);
|
43 | };
|
44 | }
|
45 |
|
46 | function formatNumerals(numerals) {
|
47 | return function(value) {
|
48 | return value.replace(/[0-9]/g, function(i) {
|
49 | return numerals[+i];
|
50 | });
|
51 | };
|
52 | }
|
53 |
|
54 |
|
55 | var re = /^(?:(.)?([<>=^]))?([+\-( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?(~)?([a-z%])?$/i;
|
56 |
|
57 | function formatSpecifier(specifier) {
|
58 | return new FormatSpecifier(specifier);
|
59 | }
|
60 |
|
61 | formatSpecifier.prototype = FormatSpecifier.prototype;
|
62 |
|
63 | function FormatSpecifier(specifier) {
|
64 | if (!(match = re.exec(specifier))) throw new Error("invalid format: " + specifier);
|
65 | var match;
|
66 | this.fill = match[1] || " ";
|
67 | this.align = match[2] || ">";
|
68 | this.sign = match[3] || "-";
|
69 | this.symbol = match[4] || "";
|
70 | this.zero = !!match[5];
|
71 | this.width = match[6] && +match[6];
|
72 | this.comma = !!match[7];
|
73 | this.precision = match[8] && +match[8].slice(1);
|
74 | this.trim = !!match[9];
|
75 | this.type = match[10] || "";
|
76 | }
|
77 |
|
78 | FormatSpecifier.prototype.toString = function() {
|
79 | return this.fill
|
80 | + this.align
|
81 | + this.sign
|
82 | + this.symbol
|
83 | + (this.zero ? "0" : "")
|
84 | + (this.width == null ? "" : Math.max(1, this.width | 0))
|
85 | + (this.comma ? "," : "")
|
86 | + (this.precision == null ? "" : "." + Math.max(0, this.precision | 0))
|
87 | + (this.trim ? "~" : "")
|
88 | + this.type;
|
89 | };
|
90 |
|
91 |
|
92 | function formatTrim(s) {
|
93 | out: for (var n = s.length, i = 1, i0 = -1, i1; i < n; ++i) {
|
94 | switch (s[i]) {
|
95 | case ".": i0 = i1 = i; break;
|
96 | case "0": if (i0 === 0) i0 = i; i1 = i; break;
|
97 | default: if (i0 > 0) { if (!+s[i]) break out; i0 = 0; } break;
|
98 | }
|
99 | }
|
100 | return i0 > 0 ? s.slice(0, i0) + s.slice(i1 + 1) : s;
|
101 | }
|
102 |
|
103 | var prefixExponent;
|
104 |
|
105 | function formatPrefixAuto(x, p) {
|
106 | var d = formatDecimal(x, p);
|
107 | if (!d) return x + "";
|
108 | var coefficient = d[0],
|
109 | exponent = d[1],
|
110 | i = exponent - (prefixExponent = Math.max(-8, Math.min(8, Math.floor(exponent / 3))) * 3) + 1,
|
111 | n = coefficient.length;
|
112 | return i === n ? coefficient
|
113 | : i > n ? coefficient + new Array(i - n + 1).join("0")
|
114 | : i > 0 ? coefficient.slice(0, i) + "." + coefficient.slice(i)
|
115 | : "0." + new Array(1 - i).join("0") + formatDecimal(x, Math.max(0, p + i - 1))[0];
|
116 | }
|
117 |
|
118 | function formatRounded(x, p) {
|
119 | var d = formatDecimal(x, p);
|
120 | if (!d) return x + "";
|
121 | var coefficient = d[0],
|
122 | exponent = d[1];
|
123 | return exponent < 0 ? "0." + new Array(-exponent).join("0") + coefficient
|
124 | : coefficient.length > exponent + 1 ? coefficient.slice(0, exponent + 1) + "." + coefficient.slice(exponent + 1)
|
125 | : coefficient + new Array(exponent - coefficient.length + 2).join("0");
|
126 | }
|
127 |
|
128 | var formatTypes = {
|
129 | "%": function(x, p) { return (x * 100).toFixed(p); },
|
130 | "b": function(x) { return Math.round(x).toString(2); },
|
131 | "c": function(x) { return x + ""; },
|
132 | "d": function(x) { return Math.round(x).toString(10); },
|
133 | "e": function(x, p) { return x.toExponential(p); },
|
134 | "f": function(x, p) { return x.toFixed(p); },
|
135 | "g": function(x, p) { return x.toPrecision(p); },
|
136 | "o": function(x) { return Math.round(x).toString(8); },
|
137 | "p": function(x, p) { return formatRounded(x * 100, p); },
|
138 | "r": formatRounded,
|
139 | "s": formatPrefixAuto,
|
140 | "X": function(x) { return Math.round(x).toString(16).toUpperCase(); },
|
141 | "x": function(x) { return Math.round(x).toString(16); }
|
142 | };
|
143 |
|
144 | function identity(x) {
|
145 | return x;
|
146 | }
|
147 |
|
148 | var prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"];
|
149 |
|
150 | function formatLocale(locale) {
|
151 | var group = locale.grouping && locale.thousands ? formatGroup(locale.grouping, locale.thousands) : identity,
|
152 | currency = locale.currency,
|
153 | decimal = locale.decimal,
|
154 | numerals = locale.numerals ? formatNumerals(locale.numerals) : identity,
|
155 | percent = locale.percent || "%";
|
156 |
|
157 | function newFormat(specifier) {
|
158 | specifier = formatSpecifier(specifier);
|
159 |
|
160 | var fill = specifier.fill,
|
161 | align = specifier.align,
|
162 | sign = specifier.sign,
|
163 | symbol = specifier.symbol,
|
164 | zero = specifier.zero,
|
165 | width = specifier.width,
|
166 | comma = specifier.comma,
|
167 | precision = specifier.precision,
|
168 | trim = specifier.trim,
|
169 | type = specifier.type;
|
170 |
|
171 |
|
172 | if (type === "n") comma = true, type = "g";
|
173 |
|
174 |
|
175 | else if (!formatTypes[type]) precision == null && (precision = 12), trim = true, type = "g";
|
176 |
|
177 |
|
178 | if (zero || (fill === "0" && align === "=")) zero = true, fill = "0", align = "=";
|
179 |
|
180 |
|
181 |
|
182 | var prefix = symbol === "$" ? currency[0] : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "",
|
183 | suffix = symbol === "$" ? currency[1] : /[%p]/.test(type) ? percent : "";
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | var formatType = formatTypes[type],
|
189 | maybeSuffix = /[defgprs%]/.test(type);
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | precision = precision == null ? 6
|
196 | : /[gprs]/.test(type) ? Math.max(1, Math.min(21, precision))
|
197 | : Math.max(0, Math.min(20, precision));
|
198 |
|
199 | function format(value) {
|
200 | var valuePrefix = prefix,
|
201 | valueSuffix = suffix,
|
202 | i, n, c;
|
203 |
|
204 | if (type === "c") {
|
205 | valueSuffix = formatType(value) + valueSuffix;
|
206 | value = "";
|
207 | } else {
|
208 | value = +value;
|
209 |
|
210 |
|
211 | var valueNegative = value < 0;
|
212 | value = formatType(Math.abs(value), precision);
|
213 |
|
214 |
|
215 | if (trim) value = formatTrim(value);
|
216 |
|
217 |
|
218 | if (valueNegative && +value === 0) valueNegative = false;
|
219 |
|
220 |
|
221 | valuePrefix = (valueNegative ? (sign === "(" ? sign : "-") : sign === "-" || sign === "(" ? "" : sign) + valuePrefix;
|
222 | valueSuffix = (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + valueSuffix + (valueNegative && sign === "(" ? ")" : "");
|
223 |
|
224 |
|
225 |
|
226 | if (maybeSuffix) {
|
227 | i = -1, n = value.length;
|
228 | while (++i < n) {
|
229 | if (c = value.charCodeAt(i), 48 > c || c > 57) {
|
230 | valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix;
|
231 | value = value.slice(0, i);
|
232 | break;
|
233 | }
|
234 | }
|
235 | }
|
236 | }
|
237 |
|
238 |
|
239 | if (comma && !zero) value = group(value, Infinity);
|
240 |
|
241 |
|
242 | var length = valuePrefix.length + value.length + valueSuffix.length,
|
243 | padding = length < width ? new Array(width - length + 1).join(fill) : "";
|
244 |
|
245 |
|
246 | if (comma && zero) value = group(padding + value, padding.length ? width - valueSuffix.length : Infinity), padding = "";
|
247 |
|
248 |
|
249 | switch (align) {
|
250 | case "<": value = valuePrefix + value + valueSuffix + padding; break;
|
251 | case "=": value = valuePrefix + padding + value + valueSuffix; break;
|
252 | case "^": value = padding.slice(0, length = padding.length >> 1) + valuePrefix + value + valueSuffix + padding.slice(length); break;
|
253 | default: value = padding + valuePrefix + value + valueSuffix; break;
|
254 | }
|
255 |
|
256 | return numerals(value);
|
257 | }
|
258 |
|
259 | format.toString = function() {
|
260 | return specifier + "";
|
261 | };
|
262 |
|
263 | return format;
|
264 | }
|
265 |
|
266 | function formatPrefix(specifier, value) {
|
267 | var f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier)),
|
268 | e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3,
|
269 | k = Math.pow(10, -e),
|
270 | prefix = prefixes[8 + e / 3];
|
271 | return function(value) {
|
272 | return f(k * value) + prefix;
|
273 | };
|
274 | }
|
275 |
|
276 | return {
|
277 | format: newFormat,
|
278 | formatPrefix: formatPrefix
|
279 | };
|
280 | }
|
281 |
|
282 | var locale;
|
283 |
|
284 | defaultLocale({
|
285 | decimal: ".",
|
286 | thousands: ",",
|
287 | grouping: [3],
|
288 | currency: ["$", ""]
|
289 | });
|
290 |
|
291 | function defaultLocale(definition) {
|
292 | locale = formatLocale(definition);
|
293 | exports.format = locale.format;
|
294 | exports.formatPrefix = locale.formatPrefix;
|
295 | return locale;
|
296 | }
|
297 |
|
298 | function precisionFixed(step) {
|
299 | return Math.max(0, -exponent(Math.abs(step)));
|
300 | }
|
301 |
|
302 | function precisionPrefix(step, value) {
|
303 | return Math.max(0, Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3 - exponent(Math.abs(step)));
|
304 | }
|
305 |
|
306 | function precisionRound(step, max) {
|
307 | step = Math.abs(step), max = Math.abs(max) - step;
|
308 | return Math.max(0, exponent(max) - exponent(step)) + 1;
|
309 | }
|
310 |
|
311 | exports.formatDefaultLocale = defaultLocale;
|
312 | exports.formatLocale = formatLocale;
|
313 | exports.formatSpecifier = formatSpecifier;
|
314 | exports.precisionFixed = precisionFixed;
|
315 | exports.precisionPrefix = precisionPrefix;
|
316 | exports.precisionRound = precisionRound;
|
317 |
|
318 | Object.defineProperty(exports, '__esModule', { value: true });
|
319 |
|
320 | })));
|