import { SGRectItem } from '@chart-parts/interfaces' ; import { path , Path } from 'd3-path' ; import { Xform } from './util' ; export class Rectangle { public constructor ( public x = d => d . x || 0 , public y = d => d . y || 0 , public width = d => d . width || 0 , public height = d => d . height || 0 , public cornerRadius = d => d . cornerRadius || 0 ) { } param d param x0 param y0 param context public build ( d , x0 ? , y0 ? , context ? ) { const { x , y , width , height , cornerRadius } = this ; const x1 = x0 != null ? x0 : + x ( d ) ; const y1 = y0 != null ? y0 : + y ( d ) ; const w = + width ( d ) ; const h = + height ( d ) ; const cr = + cornerRadius ( d ) ; if ( ! context ) { context = path ( ) ; } if ( cr <= 0 ) { context . rect ( x1 , y1 , w , h ) ; } else { const x2 = x1 + w ; const y2 = y1 + h ; context . moveTo ( x1 + cr , y1 ) ; context . lineTo ( x2 - cr , y1 ) ; context . quadraticCurveTo ( x2 , y1 , x2 , y1 + cr ) ; context . lineTo ( x2 , y2 - cr ) ; context . quadraticCurveTo ( x2 , y2 , x2 - cr , y2 ) ; context . lineTo ( x1 + cr , y2 ) ; context . quadraticCurveTo ( x1 , y2 , x1 , y2 - cr ) ; context . lineTo ( x1 , y1 + cr ) ; context . quadraticCurveTo ( x1 , y1 , x1 + cr , y1 ) ; context . closePath ( ) ; } return context ; } }