export function transformDomain()

in src/utils.ts [219:315]


export function transformDomain(
    dataView: DataViewCategorical,
    min: DataViewPropertyValue,
    max: DataViewPropertyValue): DataViewCategorical {

    if (!dataView.categories
        || !dataView.values
        || dataView.categories.length === 0
        || dataView.values.length === 0) {

        return dataView;
    }

    if (typeof min !== "number" && typeof max !== "number") {
        return dataView;
    }

    const category: DataViewCategoryColumn = dataView.categories[0];

    const categoryType: ValueTypeDescriptor = category
        ? category.source.type
        : null;

    // Min/Max comparison won't work if category source is Ordinal
    if (AxisHelper.isOrdinal(categoryType)) {
        return;
    }

    const categoryValues: PrimitiveValue[] = category.values,
        categoryObjects: DataViewObjects[] = category.objects;

    if (!categoryValues || !categoryObjects) {
        return dataView;
    }

    const newcategoryValues: PrimitiveValue[] = [],
        newValues: PrimitiveValue[][] = [],
        newObjects: DataViewObjects[] = [];

    if (typeof min !== "number") {
        min = categoryValues[0];
    }
    if (typeof max !== "number") {
        max = categoryValues[categoryValues.length - 1];
    }

    if (min > max) {
        return dataView;
    }

    for (let j: number = 0; j < dataView.values.length; j++) {
        newValues.push([]);
    }

    for (let t: number = 0; t < categoryValues.length; t++) {
        if (categoryValues[t] >= min && categoryValues[t] <= max) {
            newcategoryValues.push(categoryValues[t]);

            if (categoryObjects) {
                newObjects.push(categoryObjects[t]);
            }

            if (dataView.values) {
                for (let k: number = 0; k < dataView.values.length; k++) {
                    newValues[k].push(dataView.values[k].values[t]);
                }
            }
        }
    }

    const resultDataView: DataViewCategorical = Prototype.inherit(dataView),
        resultDataViewValues: DataViewValueColumns
            = resultDataView.values
            = Prototype.inherit(resultDataView.values),
        resultDataViewCategories: DataViewCategoryColumn[]
            = resultDataView.categories
            = Prototype.inherit(dataView.categories),
        resultDataViewCategories0: DataViewCategoryColumn
            = resultDataView.categories[0]
            = Prototype.inherit(resultDataViewCategories[0]);

    resultDataViewCategories0.values = newcategoryValues;

    if (resultDataViewCategories0.objects) {
        resultDataViewCategories0.objects = newObjects;
    }

    for (let t: number = 0; t < dataView.values.length; t++) {
        const measureArray: DataViewValueColumn
            = resultDataViewValues[t]
            = Prototype.inherit(resultDataViewValues[t]);

        measureArray.values = newValues[t];
    }

    return resultDataView;
}