本文整理匯總了TypeScript中d3-format.format函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript format函數的具體用法?TypeScript format怎麽用?TypeScript format使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了format函數的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: formatValue
export function formatValue(value: number, options: { numDecimalPlaces?: number, unit?: string }): string {
const noTrailingZeroes = true
const numDecimalPlaces = defaultTo(options.numDecimalPlaces, 2)
const unit = defaultTo(options.unit, "")
const isNoSpaceUnit = unit[0] === "%"
let output: string = value.toString()
const absValue = Math.abs(value)
if (!isNoSpaceUnit && absValue >= 1e6) {
if (!isFinite(absValue))
output = "Infinity"
else if (absValue >= 1e12)
output = formatValue(value / 1e12, extend({}, options, { unit: "trillion", numDecimalPlaces: 2 }))
else if (absValue >= 1e9)
output = formatValue(value / 1e9, extend({}, options, { unit: "billion", numDecimalPlaces: 2 }))
else if (absValue >= 1e6)
output = formatValue(value / 1e6, extend({}, options, { unit: "million", numDecimalPlaces: 2 }))
} else {
const targetDigits = Math.pow(10, -numDecimalPlaces)
if (value !== 0 && Math.abs(value) < targetDigits) {
if (value < 0)
output = `>-${targetDigits}`
else
output = `<${targetDigits}`
} else {
const rounded = precisionRound(value, numDecimalPlaces)
output = format(`,`)(rounded)
}
if (noTrailingZeroes) {
// Convert e.g. 2.200 to 2.2
const m = output.match(/(.*?[0-9,-]+.[0-9,]*?)0*$/)
if (m) output = m[1]
if (output[output.length - 1] === ".")
output = output.slice(0, output.length - 1)
}
}
if (unit === "$" || unit === "ÂŁ")
output = unit + output
else if (isNoSpaceUnit) {
output = output + unit
} else if (unit.length > 0) {
output = output + " " + unit
}
return output
}
示例2:
let num: number;
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;
示例3:
let num: number;
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);
d3Format.format('.0%')(10);
d3Format.format('.0%')(numeric);
d3Format.formatPrefix(',.0', 1e-6)(10);
d3Format.formatPrefix(',.0', 1e-6)(numeric);
// ----------------------------------------------------------------------
// Test Format Specifier
// ----------------------------------------------------------------------
specifier = d3Format.formatSpecifier('.0%');
示例4:
const tickArguments: any[] = leftAxis.tickArguments();
// tickValues(...) ----------------------------------------------------------------
topAxis = topAxis.tickValues([1, 3, 5, 7]);
bottomAxis = bottomAxis.tickValues(['strongly negative', 'strongly positive']);
leftAxis = leftAxis.tickValues(null);
const tickValues: Date[] | null = rightAxis.tickValues();
// tickFormat(...) ----------------------------------------------------------------
topAxis = topAxis.tickFormat(format(',.0f'));
topAxis = topAxis.tickFormat(null);
const formatFn: ((domainValue: string, index: number) => string) | null = bottomAxis.tickFormat();
bottomAxis.tickFormat((d, i) => '#' + i);
bottomAxis.tickFormat(d => d + '!');
// tickSize(...) ----------------------------------------------------------------
rightAxis = rightAxis.tickSize(5);
num = rightAxis.tickSize();
// tickSizeInner(...) ----------------------------------------------------------------
rightAxis = rightAxis.tickSizeInner(3);
num = rightAxis.tickSizeInner();
示例5: format
// Libraries
import {format} from 'd3-format'
import {Table, ColumnType, LineInterpolation} from '@influxdata/vis'
// Types
import {XYViewGeom, Axis} from 'src/types'
const MAX_DECIMALS = 3
const formatSmallNumber = format(`.${MAX_DECIMALS}~f`) // e.g. "0.032"
const formatLargeNumber = format(`.${MAX_DECIMALS}~s`) // e.g. "2.452M"
export const formatNumber = (t: number): string => {
if (t >= -1 && t <= 1) {
return formatSmallNumber(t)
}
return formatLargeNumber(t)
}
/*
A geom may be stored as "line", "step", "monotoneX", "bar", or "stacked", but
we currently only support the "line", "step", and "monotoneX" geoms.
*/
export const resolveGeom = (geom: XYViewGeom) => {
if (geom === XYViewGeom.Step || geom === XYViewGeom.MonotoneX) {
return geom
}
return XYViewGeom.Line
示例6:
.map(row => {
return [
row[0],
this.secondColumnFormat_exists() ? d3Format(this.secondColumnFormat())(row[1]) : row[1],
this.thirdColumnFormat_exists() ? d3Format(this.thirdColumnFormat())(row[2]) : row[2]
];
})