export function createFormatter()

in src/axisHelper.ts [261:332]


export function createFormatter(
    scaleDomain: any[],
    dataDomain: any[],
    dataType: any,
    isScalar: boolean,
    formatString: string,
    bestTickCount: number,
    tickValues: any[],
    getValueFn: any,
    useTickIntervalForDisplayUnits: boolean = false,
    axisDisplayUnits?: number,
    axisPrecision?: number): IValueFormatter {

    let formatter: IValueFormatter;
    if (dataType.dateTime) {
        if (isScalar) {
            let value: Date = new Date(scaleDomain[0]);
            let value2: Date = new Date(scaleDomain[1]);
            // datetime with only one value needs to pass the same value
            // (from the original dataDomain value, not the adjusted scaleDomain)
            // so formatting works correctly.
            if (bestTickCount === 1) {
                value = value2 = new Date(dataDomain[0]);
            }

            // this will ignore the formatString and create one based on the smallest non-zero portion of the values supplied.
            formatter = valueFormatter.create({
                format: formatString,
                value: value,
                value2: value2,
                tickCount: bestTickCount,
            });
        }
        else {
            // Use the model formatString for ordinal datetime
            formatter = valueFormatter.createDefaultFormatter(formatString, true);
        }
    }
    else {
        if (useTickIntervalForDisplayUnits && isScalar && tickValues.length > 1) {
            let value1: number = axisDisplayUnits
                ? axisDisplayUnits
                : tickValues[1] - tickValues[0];

            let options: ValueFormatterOptions = {
                format: formatString,
                value: value1,
                value2: 0, // force tickInterval or display unit to be used
                allowFormatBeautification: true,
            };

            if (axisPrecision) {
                options.precision = axisPrecision;
            } else {
                options.precision = AxisHelper.calculateAxisPrecision(
                    tickValues[0],
                    tickValues[1],
                    axisDisplayUnits,
                    formatString);
            }

            formatter = valueFormatter.create(options);
        }
        else {
            // do not use display units, just the basic value formatter
            // datetime is handled above, so we are ordinal and either boolean, numeric, or text.
            formatter = valueFormatter.createDefaultFormatter(formatString, true);
        }
    }

    return formatter;
}