import * as d3 from 'd3' ; import * as Factory from '../factories/_index' ; import * as Options from '../options/_index' ; import { IPoint } from './IPoint' ; import { Dataset } from './Dataset' ; export class Data { public sets : any ; constructor ( js ) { if ( js ) { this . fromJS ( js ) ; } } fromJS ( js ) { var sets = { } ; for ( var key in js ) { if ( js . hasOwnProperty ( key ) ) { sets [ key ] = new Dataset ( js [ key ] , key ) ; } } this . sets = sets ; } getDatasets ( series , options ) { return series . map ( ( d ) => this . getDatasetValues ( d , options ) ) ; } getDatasetValues ( series , options ) { if ( ! this . sets || ! this . sets [ series . dataset ] . values ) { return [ ] ; } var xKey = options . getAbsKey ( ) ; var fn ; if ( series . key . y0 ) { fn = ( d ) => { return { x : d [ xKey ] , y1 : d [ series . key . y1 ] , y0 : d [ series . key . y0 ] , raw : d } ; } ; } else { fn = ( d ) => { return { x : d [ xKey ] , y1 : d [ series . key . y1 ] , y0 : 0 , raw : d } ; } ; } return this . sets [ series . dataset ] . values . map ( fn ) ; } public static getMinDistance ( data , axis , key = 'x' , range ? ) { return < any > d3 . min ( data . map ( series => { return series . map ( ( d ) => axis . scale ( d [ key ] ) ) . filter ( ( d ) => { return range ? d >= range [ 0 ] && d <= range [ 1 ] : true ; } ) . reduce ( ( prev , d , i , arr ) => { var diff = i > 0 ? d - arr [ i - 1 ] : Number . MAX_VALUE ; return diff < prev ? diff : prev ; } , Number . MAX_VALUE ) ; } ) ) ; } }