import { Dimensions } from '../../../../utils/dimensions' ; import { Theme } from '../../../../utils/themes/theme' ; import { GoalSpec } from '../../specs' ; import { BulletViewModel , PickFunction , ShapeViewModel } from '../types/viewmodel_types' ; internal export function shapeViewModel ( spec , theme , chartDimensions ) { const { width , height } = chartDimensions ; const { chartMargins : margin } = theme ; const innerWidth = width - margin . left - margin . right ; const innerHeight = height - margin . top - margin . bottom ; const chartCenter = { x : margin . left + innerWidth / 2 , y : margin . top + innerHeight / 2 , } ; const { subtype , base , target , actual , bands , ticks , bandFillColor , tickValueFormatter , labelMajor , labelMinor , centralMajor , centralMinor , bandLabels , angleStart , angleEnd , } = spec ; const [ lowestValue , highestValue ] = [ base , ... ( target ? [ target ] : [ ] ) , actual , ... bands , ... ticks ] . reduce ( ( [ min , max ] , value ) => [ Math . min ( min , value ) , Math . max ( max , value ) ] , [ Infinity , - Infinity ] ) ; const aboveBaseCount = bands . filter ( ( b ) => b > base ) . length ; const belowBaseCount = bands . filter ( ( b ) => b <= base ) . length ; const callbackArgs = { base , target , actual , highestValue , lowestValue , aboveBaseCount , belowBaseCount , } ; const bulletViewModel = { subtype , base , target , actual , bands : bands . map ( ( value , index ) => ( { value , fillColor : bandFillColor ( { value , index , ... callbackArgs } ) , text : bandLabels , } ) ) , ticks : ticks . map ( ( value , index ) => ( { value , text : tickValueFormatter ( { value , index , ... callbackArgs } ) , } ) ) , labelMajor : typeof labelMajor === 'string' ? labelMajor : labelMajor ( { value : NaN , index : 0 , ... callbackArgs } ) , labelMinor : typeof labelMinor === 'string' ? labelMinor : labelMinor ( { value : NaN , index : 0 , ... callbackArgs } ) , centralMajor : typeof centralMajor === 'string' ? centralMajor : centralMajor ( { value : NaN , index : 0 , ... callbackArgs } ) , centralMinor : typeof centralMinor === 'string' ? centralMinor : centralMinor ( { value : NaN , index : 0 , ... callbackArgs } ) , highestValue , lowestValue , aboveBaseCount , belowBaseCount , angleStart , angleEnd , tooltipValueFormatter : ( ) => '' , } ; const pickQuads = ( x , y ) => - innerWidth / 2 <= x && x <= innerWidth / 2 && - innerHeight / 2 <= y && y <= innerHeight / 2 ? [ bulletViewModel ] : [ ] ; return { theme : theme . goal , chartCenter , bulletViewModel , pickQuads , } ; }