How to use the d3-format.formatPrefix 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 formatFn: (n: number) => string;

let specifier: d3Format.FormatSpecifier;

let localeDef: d3Format.FormatLocaleDefinition;

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;
github higlass / higlass / app / scripts / PixiTrack.js View on Github external
function getWidthBasedResolutionText(
  zoomLevel, maxWidth, binsPerDimension, maxZoom
) {
  const resolution = maxWidth / ((2 ** zoomLevel) * binsPerDimension);

  // we can't display a NaN resolution
  if (!Number.isNaN(resolution)) {
    // what is the maximum possible resolution?
    // this will determine how we format the lower resolutions
    const maxResolutionSize = maxWidth / ((2 ** maxZoom) * binsPerDimension);

    const pp = precisionPrefix(maxResolutionSize, resolution);
    const f = formatPrefix(`.${pp}`, resolution);
    const formattedResolution = f(resolution);

    return formattedResolution;
  }
  console.warn('NaN resolution, screen is probably too small.');

  return '';
}
github higlass / higlass / app / scripts / PixiTrack.js View on Github external
function formatResolutionText(resolution, maxResolutionSize) {
  const pp = precisionPrefix(maxResolutionSize, resolution);
  const f = formatPrefix(`.${pp}`, resolution);
  const formattedResolution = f(resolution);

  return formattedResolution;
}
github higlass / higlass / app / scripts / config.js View on Github external
if (track.maxZoom) {
                let formatter = format('.0s');
                let inlineOptions = [];

                for (let i = 0; i <= track.maxZoom; i++) {
                    let maxWidth = track.maxWidth;
                    let binsPerDimension = track.binsPerDimension;
                    let maxZoom = track.maxZoom;

                    let resolution = track.maxWidth / (2 ** i * track.binsPerDimension)

                    let maxResolutionSize = maxWidth / (2 ** maxZoom * binsPerDimension);
                    let minResolution = maxWidth / binsPerDimension;

                    let pp = precisionPrefix(maxResolutionSize, resolution);
                    let f = formatPrefix('.' + pp, resolution);
                    let formattedResolution = f(resolution);

                    //let formattedName =  ;
                    inlineOptions.push({
                        'name': formattedResolution,
                        value: i.toString()
                    });

                    //
                }

                return inlineOptions;
            } else
                return [];
        }
    }
github spotify / reactochart / src / SankeyDiagram.js View on Github external
linkLabelText: (link, graph, props) => {
      const linkValue = link.value || 0;
      const valueText = formatPrefix('.1~f', linkValue)(linkValue);
      const sourceText = getValue(
        props.nodeLabelText,
        link.source,
        graph,
        props,
      );
      const targetText = getValue(
        props.nodeLabelText,
        link.target,
        graph,
        props,
      );
      return `${sourceText}${targetText}: ${valueText}`;
    },
    linkLabelClassName: '',
github d3 / d3-scale / src / tickFormat.js View on Github external
export default function(start, stop, count, specifier) {
  var step = tickStep(start, stop, count),
      precision;
  specifier = formatSpecifier(specifier == null ? ",f" : specifier);
  switch (specifier.type) {
    case "s": {
      var value = Math.max(Math.abs(start), Math.abs(stop));
      if (specifier.precision == null && !isNaN(precision = precisionPrefix(step, value))) specifier.precision = precision;
      return formatPrefix(specifier, value);
    }
    case "":
    case "e":
    case "g":
    case "p":
    case "r": {
      if (specifier.precision == null && !isNaN(precision = precisionRound(step, Math.max(Math.abs(start), Math.abs(stop))))) specifier.precision = precision - (specifier.type === "e");
      break;
    }
    case "f":
    case "%": {
      if (specifier.precision == null && !isNaN(precision = precisionFixed(step))) specifier.precision = precision - (specifier.type === "%") * 2;
      break;
    }
  }
  return format(specifier);
github Vizzuality / trase / frontend / scripts / utils / formatValue.js View on Github external
return '-';
  }
  if (typeof value !== 'number') {
    return value;
  }

  let maximumFractionDigits = NUM_DECIMALS_DEFAULT;
  const dimensionNameLower = dimensionName.toLowerCase();

  if (NUM_DECIMALS[dimensionNameLower] !== undefined) {
    maximumFractionDigits = NUM_DECIMALS[dimensionNameLower];
  }

  const exponentToRoundTo = NUM_EXPONENT_ROUNDING[dimensionNameLower];
  if (exponentToRoundTo) {
    return formatPrefix(',.0', parseFloat(`1e${exponentToRoundTo}`))(value);
  }

  if (maximumFractionDigits === 0 && value < 1 && value > 0) {
    return '< 1';
  }

  return value.toLocaleString('en', {
    minimumFractionDigits: 0,
    maximumFractionDigits
  });
};
github weaveworks / ui-components / src / components / PrometheusGraph / PrometheusGraph.js View on Github external
numeric: number => {
    const step = number / 7;
    const formatNumber =
      number > 10
        ? formatPrefix(`.${precisionPrefix(step, number)}`, number)
        : format(`.${precisionFixed(step)}f`);
    return n => {
      if (n === null) return '---';
      if (n === 0) return '0';
      return formatNumber(n);
    };
  },
  percent: () => {