import { Color } from '@thematic/color' ; import { Text , Circle , Rect , Line , Area , Arc , Node , Link , Process , Flow , SVGMark , SignaledSVGSpec , RectSpec , AreaSpec , ArcSpec , LineSpec , LinkSpec , FlowSpec , CircleSpec , NodeSpec , ProcessSpec , TextSpec , MarkConfig , SelectionState , Rule , RuleSpec , } from '../types' ; const identity = ( datum ) => datum ; function selectionStateString ( selectionState , datum ) { const state = typeof selectionState === 'function' ? selectionState ( datum ) : selectionState ; switch ( state ) { case SelectionState . Hovered : return 'hovered' ; case SelectionState . Selected : return 'selected' ; case SelectionState . Suppressed : return 'suppressed' ; case SelectionState . Hidden : return 'hidden' ; case SelectionState . NoData : return 'nodata' ; default : return 'normal' ; } } function getMarkValue ( spec , property , markConfig ? , datum ? ) { const selectionState = markConfig && markConfig . selectionState ; const scaleBindings = markConfig && markConfig . scaleBindings ; const overrides = markConfig && markConfig . overrides ; const generalAccessor = markConfig && markConfig . accessor ; const selectionProp = selectionStateString ( selectionState ! , datum ) ; const mark = ( spec as any ) [ selectionProp ] || spec ; if ( overrides && overrides [ property ] ) { return overrides [ property ] ; } if ( scaleBindings && scaleBindings [ property ] ) { const { scale , accessor = generalAccessor || identity } = scaleBindings [ property ] ; const value = accessor ( datum ) ; return scale ( value ) ; } return ( mark as any ) [ property ] ; } function SVGMarkImpl ( spec , markConfig ? ) { return { fill : ( datum ? ) => { const color = getMarkValue ( spec , 'fill' , markConfig , datum ) ; const hex = typeof color === 'string' ? color : color . hex ( ) ; const opacity = getMarkValue ( spec , 'fillOpacity' , markConfig , datum ) ; return new Color ( hex , opacity ) ; } , fillOpacity : ( datum ? ) => { return getMarkValue ( spec , 'fillOpacity' , markConfig , datum ) ; } , stroke : ( datum ? ) => { const color = getMarkValue ( spec , 'stroke' , markConfig , datum ) ; const hex = typeof color === 'string' ? color : color . hex ( ) ; const opacity = getMarkValue ( spec , 'strokeOpacity' , markConfig , datum ) ; return new Color ( hex , opacity ) ; } , strokeOpacity : ( datum ? ) => { return getMarkValue ( spec , 'strokeOpacity' , markConfig , datum ) ; } , strokeWidth : ( datum ? ) => { return getMarkValue ( spec , 'strokeWidth' , markConfig , datum ) ; } , } ; } export function RectImpl ( spec , markConfig ? ) { return SVGMarkImpl ( spec , markConfig ) ; } export function AreaImpl ( spec , markConfig ? ) { return RectImpl ( spec , markConfig ) ; } export function ArcImpl ( spec , markConfig ? ) { return SVGMarkImpl ( spec , markConfig ) ; } export function LineImpl ( spec , markConfig ? ) { return SVGMarkImpl ( spec , markConfig ) ; } export function RuleImpl ( spec , markConfig ? ) { return SVGMarkImpl ( spec , markConfig ) ; } export function LinkImpl ( spec , markConfig ? ) { return LineImpl ( spec , markConfig ) ; } export function FlowImpl ( spec , markConfig ? ) { return LineImpl ( spec , markConfig ) ; } export function CircleImpl ( spec , markConfig ? ) { return { ... SVGMarkImpl ( spec , markConfig ) , radius : ( datum ? ) => getMarkValue ( spec , 'radius' , markConfig , datum ) , } ; } export function NodeImpl ( spec , markConfig ? ) { return CircleImpl ( spec , markConfig ) ; } export function ProcessImpl ( spec , markConfig ? ) { return CircleImpl ( spec , markConfig ) ; } export function TextImpl ( spec , markConfig ? ) { return { ... SVGMarkImpl ( spec , markConfig ) , fontWeight : ( datum ? ) => getMarkValue ( spec , 'fontWeight' , markConfig , datum ) , fontFamily : ( datum ? ) => getMarkValue ( spec , 'fontFamily' , markConfig , datum ) , fontSize : ( datum ? ) => getMarkValue ( spec , 'fontSize' , markConfig , datum ) , } ; }