How to use the d3-format.formatSpecifier function in d3-format

To help you get started, we’ve selected a few d3-format examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / d3-format / d3-format-tests.ts View on Github external
let localeObj: d3Format.FormatLocaleObject;

// ----------------------------------------------------------------------
// Test Format and FormatPrefix
// ----------------------------------------------------------------------

formatFn = d3Format.format('.0%');

formatFn = d3Format.formatPrefix(',.0', 1e-6);

// ----------------------------------------------------------------------
// Test Format Specifier
// ----------------------------------------------------------------------

specifier = d3Format.formatSpecifier('.0%');

let fill: string = specifier.fill;
let align: '>' | '<' | '^' | '=' = specifier.align;
let sign: '-' | '+' | '(' | ' ' = specifier.sign;
let symbol: '$' | '#' | '' = specifier.symbol;
let zero: boolean = specifier.zero;
let width: number | undefined = specifier.width;
let comma: boolean = specifier.comma;
let precision: number = specifier.precision;
let type: 'e' | 'f' | 'g' | 'r' | 's' | '%' | 'p' | 'b' | 'o' | 'd' | 'x' | 'X' | 'c' | '' | 'n' = specifier.type;

let formatString: string = specifier.toString();


// ----------------------------------------------------------------------
// Test Precision Suggestors
github DefinitelyTyped / DefinitelyTyped / types / d3-format / d3-format-tests.ts View on Github external
let localeObj: d3Format.FormatLocaleObject;

// ----------------------------------------------------------------------
// Test Format and FormatPrefix
// ----------------------------------------------------------------------

formatFn = d3Format.format('.0%');

formatFn = d3Format.formatPrefix(',.0', 1e-6);

// ----------------------------------------------------------------------
// Test Format Specifier
// ----------------------------------------------------------------------

specifier = d3Format.formatSpecifier('.0%');

let fill: string = specifier.fill;
let align: '>' | '<' | '^' | '=' = specifier.align;
let sign: '-' | '+' | '(' | ' ' = specifier.sign;
let symbol: '$' | '#' | '' = specifier.symbol;
let zero: boolean = specifier.zero;
let width: number | undefined = specifier.width;
let comma: boolean = specifier.comma;
let precision: number = specifier.precision;
let type: 'e' | 'f' | 'g' | 'r' | 's' | '%' | 'p' | 'b' | 'o' | 'd' | 'x' | 'X' | 'c' | '' | 'n' = specifier.type;

let formatString: string = specifier.toString();

// ----------------------------------------------------------------------
// Test Precision Suggestors
// ----------------------------------------------------------------------
github vega / vega / packages / vega-encode / src / ticks.js View on Github external
function variablePrecision(specifier) {
  var s = formatSpecifier(specifier || ',');

  if (s.precision == null) {
    s.precision = 12;
    switch (s.type) {
      case '%': s.precision -= 2; break;
      case 'e': s.precision -= 1; break;
    }
    return trimZeroes(
      numberFormat(s),          // number format
      numberFormat('.1f')(1)[1] // decimal point character
    );
  } else {
    return numberFormat(s);
  }
}
github mprove-io / mprove / blockml / src / models / field / check-and-set-implicit-format-number.ts View on Github external
x.fields.forEach(field => {
      if (field.result === enums.FieldExtResultEnum.Number) {
        if (
          typeof field.format_number === 'undefined' ||
          field.format_number === null
        ) {
          // set default
          field.format_number = '';
          field.format_number_line_num = 0;
        } else {
          try {
            let fs = formatSpecifier(field.format_number);
          } catch (e) {
            ErrorsCollector.addError(
              new AmError({
                title: `wrong format_number`,
                message: `format_number value "${
                  field.format_number
                }" is not valid`,
                lines: [
                  {
                    line: field.format_number_line_num,
                    name: x.file,
                    path: x.path
                  }
                ]
              })
            );
github susielu / d3-legend / indexRollupNext.js View on Github external
legend.labelFormat = function (_) {
    if (!arguments.length) return legend.locale().format(specifier);
    specifier = formatSpecifier(_);
    return legend;
  };
github orbiting / styleguide / src / components / Chart / utils.js View on Github external
export const getFormat = (numberFormat, tLabel) => {
  const specifier = formatSpecifier(numberFormat)

  if (specifier.type === 's') {
    return sFormat(tLabel, specifier.precision)
  }
  return swissNumbers.format(specifier)
}
github vega / datalib / src / format.js View on Github external
function linearNumberFormat(domain, count, f) {
  var range = linearRange(domain, count);

  if (f == null) f = ',f';

  switch (f = d3_numberF.formatSpecifier(f), f.type) {
    case 's': {
      var value = Math.max(Math.abs(range[0]), Math.abs(range[1]));
      if (f.precision == null) f.precision = d3_numberF.precisionPrefix(range[2], value);
      return numberF.formatPrefix(f, value);
    }
    case '':
    case 'e':
    case 'g':
    case 'p':
    case 'r': {
      if (f.precision == null) f.precision = d3_numberF.precisionRound(range[2], Math.max(Math.abs(range[0]), Math.abs(range[1]))) - (f.type === 'e');
      break;
    }
    case 'f':
    case '%': {
      if (f.precision == null) f.precision = d3_numberF.precisionFixed(range[2]) - 2 * (f.type === '%');
github susielu / d3-legend / src / symbol.js View on Github external
legend.labelFormat = function(_) {
    if (!arguments.length) return legend.locale().format(specifier)
    specifier = formatSpecifier(_)
    return legend
  }
github susielu / d3-legend / src / size.js View on Github external
legend.labelFormat = function(_) {
    if (!arguments.length) return legend.locale().format(specifier)
    specifier = formatSpecifier(_)
    return legend
  }
github orbiting / styleguide / src / components / Chart / utils.js View on Github external
numberFormat,
  tLabel,
  domain,
  unit = '',
  { ticks: predefinedTicks } = {}
) => {
  const [min, max] = domain
  const step = (max - min) / 2
  const ticks = predefinedTicks || [
    min,
    min < 0 && max > 0 ? 0 : min + step,
    max
  ]
  const format = swissNumbers.format

  const specifier = formatSpecifier(numberFormat)
  let formatter = getFormat(numberFormat, tLabel)
  let regularFormat
  let lastFormat
  if (specifier.type === '%') {
    let fullStep = +(step * 100).toFixed(specifier.precision)
    let fullMax = +(max * 100).toFixed(specifier.precision)
    specifier.precision = precisionFixed(
      fullStep - Math.floor(fullStep) || fullMax - Math.floor(fullMax)
    )
    lastFormat = format(specifier.toString())
    specifier.type = 'f'
    const regularFormatInner = format(specifier.toString())
    regularFormat = value => regularFormatInner(value * 100)
  } else if (specifier.type === 's') {
    const magnitude = d3Max([max - (min > 0 ? min : 0), min].map(Math.abs))
    let pow = formatPow(tLabel, Math.max(0, min) + magnitude / 2)