import { SpecQueryModel , SpecQueryModelGroup , } from 'compassql/build/src/model' ; import { FieldQuery , isFieldQuery } from 'compassql/build/src/query/encoding' ; import { ExtendedGroupBy } from 'compassql/build/src/query/groupby' ; import { getTopResultTreeItem , isResultTree } from 'compassql/build/src/result' ; import { toMap } from 'compassql/build/src/util' ; import { NamedData } from 'vega-lite/build/src/data' ; import { TopLevelFacetedUnitSpec } from 'vega-lite/build/src/spec' ; import { fromFieldQuery , ShelfFieldDef } from '../shelf' ; export interface PlotFieldInfo { fieldDef : ShelfFieldDef ; isEnumeratedWildcardField : boolean ; } export interface ResultPlot { fieldInfos : PlotFieldInfo [ ] ; spec : TopLevelFacetedUnitSpec ; } export interface ResultPlotWithKey { plot : ResultPlot ; groupByKey : string ; } export function fromSpecQueryModelGroup ( modelGroup , data ) { return modelGroup . items . map ( item => { if ( isResultTree < SpecQueryModel > ( item ) ) { return plotWithKey ( data , getTopResultTreeItem ( item ) , modelGroup . groupBy ) ; } return plotWithKey ( data , item as SpecQueryModel , modelGroup . groupBy ) ; } ) ; } function plotWithKey ( data , specQ , groupBy ) { const wildcardFieldIndex = toMap ( specQ . wildcardIndex . encodingIndicesByProperty . get ( 'field' ) || [ ] ) ; const fieldInfos = specQ . getEncodings ( ) . filter ( isFieldQuery ) . map ( ( fieldQ , index ) : PlotFieldInfo => { return { fieldDef : fromFieldQuery ( fieldQ ) , isEnumeratedWildcardField : index in wildcardFieldIndex } ; } ) ; const spec = { data , ... specQ . toSpec ( ) as TopLevelFacetedUnitSpec } ; const groupByKey = specQ . toShorthand ( groupBy ) ; return { plot : { fieldInfos , spec } , groupByKey } ; }