| 1 | // https://d3js.org/d3-format/ v3.1.2 Copyright 2010-2026 Mike Bostock
|
|---|
| 2 | (function (global, factory) {
|
|---|
| 3 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|---|
| 4 | typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|---|
| 5 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}));
|
|---|
| 6 | })(this, (function (exports) { 'use strict';
|
|---|
| 7 |
|
|---|
| 8 | function formatDecimal(x) {
|
|---|
| 9 | return Math.abs(x = Math.round(x)) >= 1e21
|
|---|
| 10 | ? x.toLocaleString("en").replace(/,/g, "")
|
|---|
| 11 | : x.toString(10);
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | // Computes the decimal coefficient and exponent of the specified number x with
|
|---|
| 15 | // significant digits p, where x is positive and p is in [1, 21] or undefined.
|
|---|
| 16 | // For example, formatDecimalParts(1.23) returns ["123", 0].
|
|---|
| 17 | function formatDecimalParts(x, p) {
|
|---|
| 18 | if (!isFinite(x) || x === 0) return null; // NaN, ±Infinity, ±0
|
|---|
| 19 | var i = (x = p ? x.toExponential(p - 1) : x.toExponential()).indexOf("e"), coefficient = x.slice(0, i);
|
|---|
| 20 |
|
|---|
| 21 | // The string returned by toExponential either has the form \d\.\d+e[-+]\d+
|
|---|
| 22 | // (e.g., 1.2e+3) or the form \de[-+]\d+ (e.g., 1e+3).
|
|---|
| 23 | return [
|
|---|
| 24 | coefficient.length > 1 ? coefficient[0] + coefficient.slice(2) : coefficient,
|
|---|
| 25 | +x.slice(i + 1)
|
|---|
| 26 | ];
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | function exponent(x) {
|
|---|
| 30 | return x = formatDecimalParts(Math.abs(x)), x ? x[1] : NaN;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | function formatGroup(grouping, thousands) {
|
|---|
| 34 | return function(value, width) {
|
|---|
| 35 | var i = value.length,
|
|---|
| 36 | t = [],
|
|---|
| 37 | j = 0,
|
|---|
| 38 | g = grouping[0],
|
|---|
| 39 | length = 0;
|
|---|
| 40 |
|
|---|
| 41 | while (i > 0 && g > 0) {
|
|---|
| 42 | if (length + g + 1 > width) g = Math.max(1, width - length);
|
|---|
| 43 | t.push(value.substring(i -= g, i + g));
|
|---|
| 44 | if ((length += g + 1) > width) break;
|
|---|
| 45 | g = grouping[j = (j + 1) % grouping.length];
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | return t.reverse().join(thousands);
|
|---|
| 49 | };
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | function formatNumerals(numerals) {
|
|---|
| 53 | return function(value) {
|
|---|
| 54 | return value.replace(/[0-9]/g, function(i) {
|
|---|
| 55 | return numerals[+i];
|
|---|
| 56 | });
|
|---|
| 57 | };
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | // [[fill]align][sign][symbol][0][width][,][.precision][~][type]
|
|---|
| 61 | var re = /^(?:(.)?([<>=^]))?([+\-( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?(~)?([a-z%])?$/i;
|
|---|
| 62 |
|
|---|
| 63 | function formatSpecifier(specifier) {
|
|---|
| 64 | if (!(match = re.exec(specifier))) throw new Error("invalid format: " + specifier);
|
|---|
| 65 | var match;
|
|---|
| 66 | return new FormatSpecifier({
|
|---|
| 67 | fill: match[1],
|
|---|
| 68 | align: match[2],
|
|---|
| 69 | sign: match[3],
|
|---|
| 70 | symbol: match[4],
|
|---|
| 71 | zero: match[5],
|
|---|
| 72 | width: match[6],
|
|---|
| 73 | comma: match[7],
|
|---|
| 74 | precision: match[8] && match[8].slice(1),
|
|---|
| 75 | trim: match[9],
|
|---|
| 76 | type: match[10]
|
|---|
| 77 | });
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | formatSpecifier.prototype = FormatSpecifier.prototype; // instanceof
|
|---|
| 81 |
|
|---|
| 82 | function FormatSpecifier(specifier) {
|
|---|
| 83 | this.fill = specifier.fill === undefined ? " " : specifier.fill + "";
|
|---|
| 84 | this.align = specifier.align === undefined ? ">" : specifier.align + "";
|
|---|
| 85 | this.sign = specifier.sign === undefined ? "-" : specifier.sign + "";
|
|---|
| 86 | this.symbol = specifier.symbol === undefined ? "" : specifier.symbol + "";
|
|---|
| 87 | this.zero = !!specifier.zero;
|
|---|
| 88 | this.width = specifier.width === undefined ? undefined : +specifier.width;
|
|---|
| 89 | this.comma = !!specifier.comma;
|
|---|
| 90 | this.precision = specifier.precision === undefined ? undefined : +specifier.precision;
|
|---|
| 91 | this.trim = !!specifier.trim;
|
|---|
| 92 | this.type = specifier.type === undefined ? "" : specifier.type + "";
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | FormatSpecifier.prototype.toString = function() {
|
|---|
| 96 | return this.fill
|
|---|
| 97 | + this.align
|
|---|
| 98 | + this.sign
|
|---|
| 99 | + this.symbol
|
|---|
| 100 | + (this.zero ? "0" : "")
|
|---|
| 101 | + (this.width === undefined ? "" : Math.max(1, this.width | 0))
|
|---|
| 102 | + (this.comma ? "," : "")
|
|---|
| 103 | + (this.precision === undefined ? "" : "." + Math.max(0, this.precision | 0))
|
|---|
| 104 | + (this.trim ? "~" : "")
|
|---|
| 105 | + this.type;
|
|---|
| 106 | };
|
|---|
| 107 |
|
|---|
| 108 | // Trims insignificant zeros, e.g., replaces 1.2000k with 1.2k.
|
|---|
| 109 | function formatTrim(s) {
|
|---|
| 110 | out: for (var n = s.length, i = 1, i0 = -1, i1; i < n; ++i) {
|
|---|
| 111 | switch (s[i]) {
|
|---|
| 112 | case ".": i0 = i1 = i; break;
|
|---|
| 113 | case "0": if (i0 === 0) i0 = i; i1 = i; break;
|
|---|
| 114 | default: if (!+s[i]) break out; if (i0 > 0) i0 = 0; break;
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 | return i0 > 0 ? s.slice(0, i0) + s.slice(i1 + 1) : s;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | var prefixExponent;
|
|---|
| 121 |
|
|---|
| 122 | function formatPrefixAuto(x, p) {
|
|---|
| 123 | var d = formatDecimalParts(x, p);
|
|---|
| 124 | if (!d) return prefixExponent = undefined, x.toPrecision(p);
|
|---|
| 125 | var coefficient = d[0],
|
|---|
| 126 | exponent = d[1],
|
|---|
| 127 | i = exponent - (prefixExponent = Math.max(-8, Math.min(8, Math.floor(exponent / 3))) * 3) + 1,
|
|---|
| 128 | n = coefficient.length;
|
|---|
| 129 | return i === n ? coefficient
|
|---|
| 130 | : i > n ? coefficient + new Array(i - n + 1).join("0")
|
|---|
| 131 | : i > 0 ? coefficient.slice(0, i) + "." + coefficient.slice(i)
|
|---|
| 132 | : "0." + new Array(1 - i).join("0") + formatDecimalParts(x, Math.max(0, p + i - 1))[0]; // less than 1y!
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | function formatRounded(x, p) {
|
|---|
| 136 | var d = formatDecimalParts(x, p);
|
|---|
| 137 | if (!d) return x + "";
|
|---|
| 138 | var coefficient = d[0],
|
|---|
| 139 | exponent = d[1];
|
|---|
| 140 | return exponent < 0 ? "0." + new Array(-exponent).join("0") + coefficient
|
|---|
| 141 | : coefficient.length > exponent + 1 ? coefficient.slice(0, exponent + 1) + "." + coefficient.slice(exponent + 1)
|
|---|
| 142 | : coefficient + new Array(exponent - coefficient.length + 2).join("0");
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | var formatTypes = {
|
|---|
| 146 | "%": (x, p) => (x * 100).toFixed(p),
|
|---|
| 147 | "b": (x) => Math.round(x).toString(2),
|
|---|
| 148 | "c": (x) => x + "",
|
|---|
| 149 | "d": formatDecimal,
|
|---|
| 150 | "e": (x, p) => x.toExponential(p),
|
|---|
| 151 | "f": (x, p) => x.toFixed(p),
|
|---|
| 152 | "g": (x, p) => x.toPrecision(p),
|
|---|
| 153 | "o": (x) => Math.round(x).toString(8),
|
|---|
| 154 | "p": (x, p) => formatRounded(x * 100, p),
|
|---|
| 155 | "r": formatRounded,
|
|---|
| 156 | "s": formatPrefixAuto,
|
|---|
| 157 | "X": (x) => Math.round(x).toString(16).toUpperCase(),
|
|---|
| 158 | "x": (x) => Math.round(x).toString(16)
|
|---|
| 159 | };
|
|---|
| 160 |
|
|---|
| 161 | function identity(x) {
|
|---|
| 162 | return x;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | var map = Array.prototype.map,
|
|---|
| 166 | prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"];
|
|---|
| 167 |
|
|---|
| 168 | function formatLocale(locale) {
|
|---|
| 169 | var group = locale.grouping === undefined || locale.thousands === undefined ? identity : formatGroup(map.call(locale.grouping, Number), locale.thousands + ""),
|
|---|
| 170 | currencyPrefix = locale.currency === undefined ? "" : locale.currency[0] + "",
|
|---|
| 171 | currencySuffix = locale.currency === undefined ? "" : locale.currency[1] + "",
|
|---|
| 172 | decimal = locale.decimal === undefined ? "." : locale.decimal + "",
|
|---|
| 173 | numerals = locale.numerals === undefined ? identity : formatNumerals(map.call(locale.numerals, String)),
|
|---|
| 174 | percent = locale.percent === undefined ? "%" : locale.percent + "",
|
|---|
| 175 | minus = locale.minus === undefined ? "−" : locale.minus + "",
|
|---|
| 176 | nan = locale.nan === undefined ? "NaN" : locale.nan + "";
|
|---|
| 177 |
|
|---|
| 178 | function newFormat(specifier, options) {
|
|---|
| 179 | specifier = formatSpecifier(specifier);
|
|---|
| 180 |
|
|---|
| 181 | var fill = specifier.fill,
|
|---|
| 182 | align = specifier.align,
|
|---|
| 183 | sign = specifier.sign,
|
|---|
| 184 | symbol = specifier.symbol,
|
|---|
| 185 | zero = specifier.zero,
|
|---|
| 186 | width = specifier.width,
|
|---|
| 187 | comma = specifier.comma,
|
|---|
| 188 | precision = specifier.precision,
|
|---|
| 189 | trim = specifier.trim,
|
|---|
| 190 | type = specifier.type;
|
|---|
| 191 |
|
|---|
| 192 | // The "n" type is an alias for ",g".
|
|---|
| 193 | if (type === "n") comma = true, type = "g";
|
|---|
| 194 |
|
|---|
| 195 | // The "" type, and any invalid type, is an alias for ".12~g".
|
|---|
| 196 | else if (!formatTypes[type]) precision === undefined && (precision = 12), trim = true, type = "g";
|
|---|
| 197 |
|
|---|
| 198 | // If zero fill is specified, padding goes after sign and before digits.
|
|---|
| 199 | if (zero || (fill === "0" && align === "=")) zero = true, fill = "0", align = "=";
|
|---|
| 200 |
|
|---|
| 201 | // Compute the prefix and suffix.
|
|---|
| 202 | // For SI-prefix, the suffix is lazily computed.
|
|---|
| 203 | var prefix = (options && options.prefix !== undefined ? options.prefix : "") + (symbol === "$" ? currencyPrefix : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : ""),
|
|---|
| 204 | suffix = (symbol === "$" ? currencySuffix : /[%p]/.test(type) ? percent : "") + (options && options.suffix !== undefined ? options.suffix : "");
|
|---|
| 205 |
|
|---|
| 206 | // What format function should we use?
|
|---|
| 207 | // Is this an integer type?
|
|---|
| 208 | // Can this type generate exponential notation?
|
|---|
| 209 | var formatType = formatTypes[type],
|
|---|
| 210 | maybeSuffix = /[defgprs%]/.test(type);
|
|---|
| 211 |
|
|---|
| 212 | // Set the default precision if not specified,
|
|---|
| 213 | // or clamp the specified precision to the supported range.
|
|---|
| 214 | // For significant precision, it must be in [1, 21].
|
|---|
| 215 | // For fixed precision, it must be in [0, 20].
|
|---|
| 216 | precision = precision === undefined ? 6
|
|---|
| 217 | : /[gprs]/.test(type) ? Math.max(1, Math.min(21, precision))
|
|---|
| 218 | : Math.max(0, Math.min(20, precision));
|
|---|
| 219 |
|
|---|
| 220 | function format(value) {
|
|---|
| 221 | var valuePrefix = prefix,
|
|---|
| 222 | valueSuffix = suffix,
|
|---|
| 223 | i, n, c;
|
|---|
| 224 |
|
|---|
| 225 | if (type === "c") {
|
|---|
| 226 | valueSuffix = formatType(value) + valueSuffix;
|
|---|
| 227 | value = "";
|
|---|
| 228 | } else {
|
|---|
| 229 | value = +value;
|
|---|
| 230 |
|
|---|
| 231 | // Determine the sign. -0 is not less than 0, but 1 / -0 is!
|
|---|
| 232 | var valueNegative = value < 0 || 1 / value < 0;
|
|---|
| 233 |
|
|---|
| 234 | // Perform the initial formatting.
|
|---|
| 235 | value = isNaN(value) ? nan : formatType(Math.abs(value), precision);
|
|---|
| 236 |
|
|---|
| 237 | // Trim insignificant zeros.
|
|---|
| 238 | if (trim) value = formatTrim(value);
|
|---|
| 239 |
|
|---|
| 240 | // If a negative value rounds to zero after formatting, and no explicit positive sign is requested, hide the sign.
|
|---|
| 241 | if (valueNegative && +value === 0 && sign !== "+") valueNegative = false;
|
|---|
| 242 |
|
|---|
| 243 | // Compute the prefix and suffix.
|
|---|
| 244 | valuePrefix = (valueNegative ? (sign === "(" ? sign : minus) : sign === "-" || sign === "(" ? "" : sign) + valuePrefix;
|
|---|
| 245 | valueSuffix = (type === "s" && !isNaN(value) && prefixExponent !== undefined ? prefixes[8 + prefixExponent / 3] : "") + valueSuffix + (valueNegative && sign === "(" ? ")" : "");
|
|---|
| 246 |
|
|---|
| 247 | // Break the formatted value into the integer “value” part that can be
|
|---|
| 248 | // grouped, and fractional or exponential “suffix” part that is not.
|
|---|
| 249 | if (maybeSuffix) {
|
|---|
| 250 | i = -1, n = value.length;
|
|---|
| 251 | while (++i < n) {
|
|---|
| 252 | if (c = value.charCodeAt(i), 48 > c || c > 57) {
|
|---|
| 253 | valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix;
|
|---|
| 254 | value = value.slice(0, i);
|
|---|
| 255 | break;
|
|---|
| 256 | }
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | // If the fill character is not "0", grouping is applied before padding.
|
|---|
| 262 | if (comma && !zero) value = group(value, Infinity);
|
|---|
| 263 |
|
|---|
| 264 | // Compute the padding.
|
|---|
| 265 | var length = valuePrefix.length + value.length + valueSuffix.length,
|
|---|
| 266 | padding = length < width ? new Array(width - length + 1).join(fill) : "";
|
|---|
| 267 |
|
|---|
| 268 | // If the fill character is "0", grouping is applied after padding.
|
|---|
| 269 | if (comma && zero) value = group(padding + value, padding.length ? width - valueSuffix.length : Infinity), padding = "";
|
|---|
| 270 |
|
|---|
| 271 | // Reconstruct the final output based on the desired alignment.
|
|---|
| 272 | switch (align) {
|
|---|
| 273 | case "<": value = valuePrefix + value + valueSuffix + padding; break;
|
|---|
| 274 | case "=": value = valuePrefix + padding + value + valueSuffix; break;
|
|---|
| 275 | case "^": value = padding.slice(0, length = padding.length >> 1) + valuePrefix + value + valueSuffix + padding.slice(length); break;
|
|---|
| 276 | default: value = padding + valuePrefix + value + valueSuffix; break;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | return numerals(value);
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | format.toString = function() {
|
|---|
| 283 | return specifier + "";
|
|---|
| 284 | };
|
|---|
| 285 |
|
|---|
| 286 | return format;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | function formatPrefix(specifier, value) {
|
|---|
| 290 | var e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3,
|
|---|
| 291 | k = Math.pow(10, -e),
|
|---|
| 292 | f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier), {suffix: prefixes[8 + e / 3]});
|
|---|
| 293 | return function(value) {
|
|---|
| 294 | return f(k * value);
|
|---|
| 295 | };
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | return {
|
|---|
| 299 | format: newFormat,
|
|---|
| 300 | formatPrefix: formatPrefix
|
|---|
| 301 | };
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | var locale;
|
|---|
| 305 | exports.format = void 0;
|
|---|
| 306 | exports.formatPrefix = void 0;
|
|---|
| 307 |
|
|---|
| 308 | defaultLocale({
|
|---|
| 309 | thousands: ",",
|
|---|
| 310 | grouping: [3],
|
|---|
| 311 | currency: ["$", ""]
|
|---|
| 312 | });
|
|---|
| 313 |
|
|---|
| 314 | function defaultLocale(definition) {
|
|---|
| 315 | locale = formatLocale(definition);
|
|---|
| 316 | exports.format = locale.format;
|
|---|
| 317 | exports.formatPrefix = locale.formatPrefix;
|
|---|
| 318 | return locale;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | function precisionFixed(step) {
|
|---|
| 322 | return Math.max(0, -exponent(Math.abs(step)));
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | function precisionPrefix(step, value) {
|
|---|
| 326 | return Math.max(0, Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3 - exponent(Math.abs(step)));
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | function precisionRound(step, max) {
|
|---|
| 330 | step = Math.abs(step), max = Math.abs(max) - step;
|
|---|
| 331 | return Math.max(0, exponent(max) - exponent(step)) + 1;
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | exports.FormatSpecifier = FormatSpecifier;
|
|---|
| 335 | exports.formatDefaultLocale = defaultLocale;
|
|---|
| 336 | exports.formatLocale = formatLocale;
|
|---|
| 337 | exports.formatSpecifier = formatSpecifier;
|
|---|
| 338 | exports.precisionFixed = precisionFixed;
|
|---|
| 339 | exports.precisionPrefix = precisionPrefix;
|
|---|
| 340 | exports.precisionRound = precisionRound;
|
|---|
| 341 |
|
|---|
| 342 | }));
|
|---|