license import embed , { Mode , Result as EmbedRes , VisualizationSpec } from 'vega-embed' ; import { Drawable , VisOptions } from '../types' ; import { getDrawArea , nextFrame , shallowEquals } from './render_utils' ; param data returns doc export async function barchart ( container , data , opts = { } ) { const drawArea = getDrawArea ( container ) ; const values = data ; const options = Object . assign ( { } , defaultOpts , opts ) ; if ( instances . has ( drawArea ) ) { const instanceInfo = instances . get ( drawArea ) ! ; if ( shallowEquals ( options , instanceInfo . lastOptions ) ) { await nextFrame ( ) ; const view = instanceInfo . view ; const changes = view . changeset ( ) . remove ( ( ) => true ) . insert ( values ) ; await view . change ( 'values' , changes ) . runAsync ( ) ; return ; } } const { xLabel , yLabel , xType , yType } = options ; let xAxis = null ; if ( xLabel != null ) { xAxis = { title : xLabel } ; } let yAxis = null ; if ( yLabel != null ) { yAxis = { title : yLabel } ; } const embedOpts = { actions : false , mode : 'vega-lite' as Mode , defaultStyle : false , } ; const spec = { 'width' : options . width || drawArea . clientWidth , 'height' : options . height || drawArea . clientHeight , 'padding' : 0 , 'autosize' : { 'type' : 'fit' , 'contains' : 'padding' , 'resize' : true , } , 'config' : { 'axis' : { 'labelFontSize' : options . fontSize , 'titleFontSize' : options . fontSize , } , 'text' : { 'fontSize' : options . fontSize } , 'legend' : { 'labelFontSize' : options . fontSize , 'titleFontSize' : options . fontSize , } } , 'data' : { 'values' : values , 'name' : 'values' } , 'mark' : 'bar' , 'encoding' : { 'x' : { 'field' : 'index' , 'type' : xType , 'axis' : xAxis } , 'y' : { 'field' : 'value' , 'type' : yType , 'axis' : yAxis } } } ; await nextFrame ( ) ; const embedRes = await embed ( drawArea , spec , embedOpts ) ; instances . set ( drawArea , { view : embedRes . view , lastOptions : options , } ) ; } const defaultOpts = { xLabel : '' , yLabel : '' , xType : 'ordinal' , yType : 'quantitative' , fontSize : 11 , } ; const instances = new Map < HTMLElement , InstanceInfo > ( ) ; interface InstanceInfo { view : any ; lastOptions : VisOptions ; }