import { min , max , resolveValue } from '../../internal' ; import type { EAdvancedSortMethod , ESortMethod , INumberColumnDesc , ITypedDump } from '../../model' ; import ColumnBuilder from './ColumnBuilder' ; import type { IScriptMappingFunctionType } from '../../model/MappingFunction' ; export default class NumberColumnBuilder extends ColumnBuilder < INumberColumnDesc > { constructor ( column ) { super ( 'number' , column ) ; } param { "linear" | "sqrt" | "pow1.1" | "pow2" | "pow3" } type param { [ number , number ] } domain param { [ number , number ] } range mapping ( type , domain , range ? ) { if ( type === 'linear' ) { this . desc . domain = domain ; if ( range ) { this . desc . range = range ; } return this ; } this . desc . map = { type , domain , range : range ?? [ 0 , 1 ] , } ; return this ; } deprecated color ( color ) { return this . colorMapping ( color ) ; } colorMapping ( type ) { this . desc . colorMapping = type ; return this ; } param format numberFormat ( format ) { this . desc . numberFormat = format ; return this ; } param { string } code param { [ number , number ] } domain scripted ( code , domain ) { this . desc . map = { domain , code , type : 'script' } ; return this ; } inheritDoc param { string [ ] | number } labels param { EAdvancedSortMethod } sort asArray ( labels ? , sort ? ) { if ( sort ) { ( this . desc as any ) . sort = sort ; } return super . asArray ( labels ) ; } inheritDoc param { EAdvancedSortMethod } sort asMap ( sort ? ) { if ( sort ) { ( this . desc as any ) . sort = sort ; } return super . asMap ( ) ; } param { ESortMethod } sort asBoxPlot ( sort ? ) { if ( sort ) { ( this . desc as any ) . sort = sort ; } this . desc . type = 'boxplot' ; return this ; } private derive ( data ) { const col = ( this . desc as any ) . column ; const asArray = ( v , extra ) => { const vs = [ ] ; ( Array . isArray ( v ) ? v : [ v ] ) . forEach ( ( vi ) => { if ( typeof vi === 'number' && ! Number . isNaN ( vi ) ) { vs . push ( vi ) ; } if ( vi != null && typeof vi . value === 'number' && ! Number . isNaN ( vi . value ) ) { vs . push ( vi . value ) ; } if ( vi != null && typeof vi [ extra ] === 'number' && ! Number . isNaN ( vi [ extra ] ) ) { vs . push ( vi [ extra ] ) ; } } ) ; return vs ; } ; const minValue = min ( data , ( d ) => { const v = resolveValue ( d , col ) ; const vs = asArray ( v , 'min' ) ; return vs . length === 0 ? Number . POSITIVE_INFINITY : min ( vs ) ; } ) ; const maxValue = max ( data , ( d ) => { const v = resolveValue ( d , col ) ; const vs = asArray ( v , 'max' ) ; return vs . length === 0 ? Number . NEGATIVE_INFINITY : max ( vs ) ; } ) ; return [ minValue , maxValue ] ; } build ( data ) { if ( ! this . desc . map && ! this . desc . domain ) { this . mapping ( 'linear' , this . derive ( data ) ) ; } else { const d = this . desc . domain || this . desc . map ! . domain ; if ( Number . isNaN ( d [ 0 ] ) || Number . isNaN ( d [ 1 ] ) ) { const ext = this . derive ( data ) ; if ( Number . isNaN ( d [ 0 ] ) ) { d [ 0 ] = ext [ 0 ] ; } if ( Number . isNaN ( d [ 1 ] ) ) { d [ 1 ] = ext [ 1 ] ; } } } return super . build ( data ) ; } } param { string } column param { [ number , number ] } domain returns { NumberColumnBuilder } export function buildNumberColumn ( column , domain ? ) { const r = new NumberColumnBuilder ( column ) ; if ( domain ) { r . mapping ( 'linear' , domain ) ; } return r ; }