import { DataField } from './DataField' ; import { ChartDataItem } from './ChartDataItem' ; export class ChartData { public includeEmptyValues : boolean ; private _items : ChartDataItem [ ] ; param items constructor ( items ) { this . _items = items ; } param field public getItems ( field ? ) { if ( field ) { return this . _items . filter ( ( item ) => ( this . includeEmptyValues ? true : item . hasValue ( field ) ) ) ; } return this . _items ; } param field public getItemsWithNonZeroValue ( field ) { return this . _items . filter ( ( i ) => i . getValue ( field ) !== 0 ) ; } param field param value public getItemsWithStringValue ( field , value ) { return this . getItems ( field ) . filter ( ( i ) => i . getValue ( field ) === value ) ; } param index public getItem ( index ) { return this . _items [ index ] ; } param field public getCount ( field ? ) { return this . getItems ( field ) . length ; } public isEmpty ( ) { return this . getCount ( ) === 0 ; } param field public getAverage ( field ? ) { return this . getTotal ( field ) / this . getCount ( field ) ; } param field public getValues ( field ) { return this . _items . filter ( ( item ) => ( this . includeEmptyValues ? true : item . hasValue ( field ) ) ) . map ( ( item ) => item . getValue ( field ) ) ; } param field public getValuesUnique ( field ) { return this . getValues ( field ) . filter ( ( value , index , self ) => self . indexOf ( value ) === index ) ; } param field public getNames ( ... field ) { if ( field ) { return this . _items . filter ( ( item ) => { if ( this . includeEmptyValues ) { return true ; } if ( Array . isArray ( field ) ) { return field . filter ( ( f ) => item . hasValue ( f ) ) . length === field . length ; } else { return item . hasValue ( field as DataField ) ; } } ) . map ( ( item ) => item . name ) ; } return this . _items . map ( ( item ) => item . name ) ; } param field public getTotal ( field ) { return this . _items . filter ( ( i ) => i . hasValue ( field ) ) . map ( ( i ) => i . getValue ( field ) ) . reduce ( ( prev , curr ) => ( prev += curr ) , 0 ) ; } param field param index param fractionDigits public getPercentage ( field , index , fractionDigits = 2 ) { return parseFloat ( ( ( this . _items [ index ] . getValue ( field ) / this . getTotal ( field ) ) * 100 ) . toFixed ( fractionDigits ) ) ; } }