import { MarkType , getItemSpace , Mark , ItemSpace , MarkEncodings , ViewSize , SGGroupItem , EncodingContext , ItemIdGenerator , Metadata , MarkEncodingKey , } from '@chart-parts/interfaces' ; import { createItem } from '@chart-parts/scenegraph' ; import { SceneFrame } from '../context/SceneFrame' ; import { processNode } from '../processNode' ; ignore param frame param row param index param table export function createBoundItem ( frame , row , index , table ) { const mark = frame . mark as Mark ; const item = createItemFromMark ( mark , row , index , table , frame ) ; if ( mark . type !== MarkType . Group ) { return item ; } const itemSpace = getItemSpace ( item ) ; const drawRect = getNextDrawRect ( itemSpace , frame . view ) ; const nextView = { width : drawRect . right - drawRect . left , height : drawRect . bottom - drawRect . top , } ; const itemFrame = frame . pushView ( nextView ) . pushBoundDataItem ( row ) ; const items = mark . child ? processNode ( mark . child , itemFrame ) : [ ] ; const groupItem = { ... item , items , x : drawRect . left , y : drawRect . top , x2 : drawRect . right , y2 : drawRect . bottom , } ; return groupItem ; } param mark param row param index param data param channelNames param scales param dataFrame function createItemFromMark ( mark , row , index , data , frame ) { const { type , encodings , name , role , idGenerator } = mark ; const id = `${ frame . parentId }.${ getItemId ( row , index , data , idGenerator ) }` ; const context = getEncodingContext ( id , row , index , frame ) ; let metadata = { } as any ; const metadataEncoding = mark . encodings [ MarkEncodingKey . metadata ] ; if ( metadataEncoding ) { metadata = typeof metadataEncoding === 'function' ? metadataEncoding ( context ) : metadataEncoding ; } const props = { ... transferEncodings ( encodings , context ) , name , role , metadata : { ... metadata , index , id } , channels : frame . channels , } ; return createItem ( type , props ) ; } function getItemId ( row , index , data , idGenerator ) { if ( ! idGenerator ) { return `${ index }` ; } else { return idGenerator ( row , index , data ) ; } } param space param drawRect function getNextDrawRect ( space , viewSize ) { const { width : availableWidth , height : availableHeight } = viewSize ; const { origin : { x = 0 , y = 0 } , shape : { width = availableWidth , height = availableHeight } , } = space ; const top = y ; const left = x ; const right = left + width ; const bottom = top + height ; return { top , left , bottom , right , } ; } function transferEncodings ( encodings , context ) { const props = { } ; Object . keys ( encodings ) . filter ( t => t !== 'items' ) . forEach ( key => { const encoding = encodings [ key ] ; if ( encoding ) { const value = typeof encoding === 'function' ? encoding ( context ) : encoding ; props [ key ] = value ; } } ) ; return props ; } function getEncodingContext ( id , d , index , frame ) { const encodingContext = { id , d , index , view : frame . view , ... frame . data , ... frame . scales , } ; return encodingContext ; }