import { Path , path } from 'd3-path' ; import { parsePath , Command } from '../parse' ; import { renderPath } from '../render' ; import { drawCircle } from './drawCircle' ; import { drawCross } from './drawCross' ; import { drawDiamond } from './drawDiamond' ; import { drawSquare } from './drawSquare' ; import { drawTriangleUp , drawTriangleDown , drawTriangleLeft , drawTriangleRight , } from './drawTriangle' ; export interface SymbolRenderer { draw ( context , size ) : Path ; } export interface SymbolRenderers { byArea : SymbolRenderer ; byWidth : SymbolRenderer ; } export interface SymbolMap { [ key ] : SymbolRenderers ; } const getRadius = ( area ) => { return Math . sqrt ( area ) / 2 ; } ; const builtins = { circle : { byArea : { draw : ( ctx , size ) => drawCircle ( ctx , getRadius ( size ) ) , } , byWidth : { draw : ( ctx , size ) => drawCircle ( ctx , size / 2 ) , } , } , cross : { byArea : { draw : ( ctx , size ) => drawCross ( ctx , getRadius ( size ) ) , } , byWidth : { draw : ( ctx , size ) => drawCross ( ctx , size / 2 ) , } , } , diamond : { byArea : { draw : ( ctx , size ) => drawDiamond ( ctx , getRadius ( size ) ) , } , byWidth : { draw : ( ctx , size ) => drawDiamond ( ctx , size / 2 ) , } , } , square : { byArea : { draw : ( ctx , size ) => drawSquare ( ctx , Math . sqrt ( size ) ) , } , byWidth : { draw : ( ctx , size ) => drawSquare ( ctx , size ) , } , } , 'triangle-up' : { byArea : { draw : ( ctx , size ) => drawTriangleUp ( ctx , Math . sqrt ( size ) ) , } , byWidth : { draw : ( ctx , size ) => drawTriangleUp ( ctx , size ) , } , } , 'triangle-down' : { byArea : { draw : ( ctx , size ) => drawTriangleDown ( ctx , Math . sqrt ( size ) ) , } , byWidth : { draw : ( ctx , size ) => drawTriangleDown ( ctx , size ) , } , } , 'triangle-left' : { byArea : { draw : ( ctx , size ) => drawTriangleLeft ( ctx , Math . sqrt ( size ) ) , } , byWidth : { draw : ( ctx , size ) => drawTriangleLeft ( ctx , size ) , } , } , 'triangle-right' : { byArea : { draw : ( ctx , size ) => drawTriangleRight ( ctx , Math . sqrt ( size ) ) , } , byWidth : { draw : ( ctx , size ) => drawTriangleRight ( ctx , size ) , } , } , } ; export function symbols ( symbolName ) { return Object . prototype . hasOwnProperty . call ( builtins , symbolName ) ? builtins [ symbolName ] : customSymbol ( symbolName ) ; } export function symbolsByWidth ( symbolName ) { return Object . prototype . hasOwnProperty . call ( builtins , symbolName ) ? builtins [ symbolName ] : customSymbol ( symbolName ) ; } const custom = { } ; export function customSymbol ( symbolPath ) { if ( ! Object . prototype . hasOwnProperty . call ( custom , symbolPath ) ) { const parsed = parsePath ( symbolPath ) ; custom [ symbolPath ] = { byArea : { draw : ( context , size ) => drawCustom ( context , getRadius ( size ) , parsed ) , } , byWidth : { draw : ( context , size ) => drawCustom ( context , getRadius ( size ) , parsed ) , } , } ; } return custom [ symbolPath ] ; } function drawCustom ( context , size , pathValue ) { if ( ! context ) { context = path ( ) ; } renderPath ( context , pathValue , 0 , 0 , size ) ; return context ; }