import { shuffle } from 'lodash' ; import { FullDataSeriesDatum , WithIndex } from '../../chart_types/xy_chart/utils/fit_function' ; import { DataSeries , DataSeriesDatum , XYChartSeriesIdentifier } from '../../chart_types/xy_chart/utils/series' ; import { SeriesType } from '../../specs' ; import { mergePartial } from '../../utils/common' ; import { MockSeriesSpec } from '../specs' ; import { getRandomNumberGenerator } from '../utils' ; import { fitFunctionData } from './data' ; const rng = getRandomNumberGenerator ( ) ; interface DomainRange { min ? : number ; max ? : number ; fractionDigits ? : number ; inclusive ? : boolean ; } internal export class MockDataSeries { private static readonly base : DataSeries = { specId : 'spec1' , seriesKeys : [ 'spec1' ] , yAccessor : 'y' , splitAccessors : new Map ( ) , key : 'spec1' , data : [ ] , groupId : 'group1' , seriesType : SeriesType . Bar , stackMode : undefined , spec : MockSeriesSpec . bar ( ) , isStacked : false , insertIndex : 0 , isFiltered : false , } ; static default ( partial ? ) { return mergePartial < DataSeries > ( MockDataSeries . base , partial ) ; } static fitFunction ( options = { shuffle : true , ordinal : false } ) { const ordinalData = options . ordinal ? fitFunctionData . map ( ( d ) => ( { ... d , x : String . fromCharCode ( 97 + ( d . x as number ) ) } ) ) : fitFunctionData ; const data = options . shuffle && ! options . ordinal ? shuffle ( ordinalData ) : ordinalData ; return { ... MockDataSeries . base , data } ; } static fromData ( data , seriesIdentifier ? ) { return { ... MockDataSeries . base , ... seriesIdentifier , data , } ; } static random ( options , includeMarks = false ) { const data = new Array ( options ?. count ?? 10 ) . fill ( 0 ) . map ( ( ) => MockDataSeriesDatum . random ( options , includeMarks ) ) ; return { ... MockDataSeries . base , data , } ; } static empty ( ) { return [ ] ; } } internal export class MockDataSeriesDatum { private static readonly base : DataSeriesDatum = { x : 1 , y1 : 1 , y0 : null , mark : null , initialY1 : null , initialY0 : null , datum : undefined , } ; static default ( partial ? ) { const merged = mergePartial < DataSeriesDatum > ( MockDataSeriesDatum . base , partial ) ; if ( merged . initialY1 === null ) { merged . initialY1 = merged . y1 ; } if ( merged . initialY0 === null ) { merged . initialY0 = merged . y0 ; } return merged ; } /**
     * Fill datum with minimal values, default missing required values to `null`
     */ static simple ( { x , y1 = null , y0 = null , mark = null , filled , } ) { return { x , y1 , y0 , mark , initialY1 : y1 , initialY0 : y0 , datum : { x , y1 , y0 } , ... ( filled && filled ) , } ; } /**
     * returns "full" datum with minimal values, default missing required values to `null`
     *
     * "full" - means x and y1 values are `non-nullable`
     */ static full ( { fittingIndex = 0 , ... datum } ) { return { ... ( MockDataSeriesDatum . simple ( datum ) as WithIndex < FullDataSeriesDatum > ) , fittingIndex } ; } static ordinal ( partial ? ) { return mergePartial < DataSeriesDatum > ( { ... MockDataSeriesDatum . base , x : 'a' } , partial ) ; } /** Psuedo-random values in a specified domain */ static random ( options , includeMark = false ) { return MockDataSeriesDatum . simple ( { x : rng ( options ?. x ?. min , options ?. x ?. max , options . x ?. fractionDigits , options . x ?. inclusive ) , y1 : rng ( options ?. y ?. min , options ?. y ?. max , options . y ?. fractionDigits , options . y ?. inclusive ) , ... ( includeMark && { mark : rng ( options ?. mark ?. min , options ?. mark ?. max , options . mark ?. fractionDigits , options . mark ?. inclusive ) , } ) , } ) ; } }