// @flow import React, {Component} from 'react'; import type {TickScale, RadarPoint, RadarVariable} from './types'; import RadarAxis from './RadarAxis'; import RadarCircle from './RadarCircle'; import RadarRings from './RadarRings'; type Props = { variables: Array, width: number, height: number, padding: number, domainMax: number, style?: {}, onHover?: (point: RadarPoint | null) => void, highlighted: ?RadarPoint, scales: {[variableKey: string]: TickScale}, backgroundScale: TickScale, offsetAngles: {[variableKey: string]: number}, voronoiDiagram: any, radius: number, highlightedPoint: ?{setKey: string, points: Array}, regularPoints: Array<{setKey: string, points: Array}>, colors: {[setKey: string]: string}, }; const defaultRadarStyle = { numRings: 4, axisColor: '#4B5563', ringColor: '#4B5563', }; function getHovered( event: MouseEvent, height: number, width: number, padding: number, radius: number, voronoiDiagram: any, ) { const innerHeight = height - padding * 2; const innerWidth = width - padding * 2; const diameter = radius * 2; let {offsetX: clientX, offsetY: clientY} = event; clientX -= padding; clientY -= padding; clientX -= (innerWidth - diameter) / 2; clientY -= (innerHeight - diameter) / 2; const site = voronoiDiagram.find(clientX, clientY, radius / 2); if (!site) { return null; } const {data} = site; return data; } export default class RadarWrapper extends Component { props: Props; hoverMap = null; componentDidMount() { if (this.hoverMap) { this.hoverMap.addEventListener('mousemove', (event: MouseEvent) => { const {onHover} = this.props; if (!onHover) { return; } const {padding, height, width, radius, voronoiDiagram} = this.props; onHover( getHovered(event, height, width, padding, radius, voronoiDiagram), ); }); } } render() { const { width, height, padding, radius, style, highlighted, scales, variables, offsetAngles, domainMax, highlightedPoint, regularPoints, backgroundScale, colors, } = this.props; const diameter = radius * 2; const {axisColor, ringColor, numRings} = {...defaultRadarStyle, ...style}; const innerHeight = height - padding * 2; const innerWidth = width - padding * 2; const ticks = backgroundScale.ticks(numRings).slice(1); const tickFormat = backgroundScale.tickFormat(numRings); return ( { this.hoverMap = c; }} > {variables.map(({key, label}) => { return ( ); })} {regularPoints.map(({setKey, points}) => { return ( ); })} { highlightedPoint ? : null } ); } }