import * as React from 'react'; import { useMemo } from 'react'; import * as hex from 'd3-hexbin'; import * as d3v7 from 'd3v7'; import { PieChart } from './PieChart'; import { EHexbinOptions } from './interfaces'; import { cutHex } from './utils'; import { selectionColorDark } from '../../utils'; import { VIS_NEUTRAL_COLOR, VIS_UNSELECTED_COLOR } from '../general/constants'; export interface SingleHexProps { hexbinOption: EHexbinOptions; hexData: hex.HexbinBin<[number, number, string, string]>; d3Hexbin: hex.Hexbin<[number, number]>; isSizeScale: boolean; radiusScale: d3v7.ScaleLinear | null; isOpacityScale: boolean; opacityScale: d3v7.ScaleLinear | null; hexRadius: number; colorScale: d3v7.ScaleOrdinal; selected?: { [key: string]: boolean }; isCategorySelected: boolean; } export function SingleHex({ hexbinOption, hexData, d3Hexbin, isSizeScale, radiusScale, isOpacityScale, opacityScale, hexRadius, colorScale, selected = {}, isCategorySelected, }: SingleHexProps) { const { catMap, catMapKeys, catMapVals } = useMemo(() => { const currMap = {}; hexData.forEach((point: [number, number, string, string]) => { currMap[point[2]] = currMap[point[2]] ? currMap[point[2]] + 1 : 1; }); return { catMap: currMap, catMapKeys: Object.keys(currMap), catMapVals: Object.values(currMap) }; }, [hexData]); const isSelected = useMemo(() => { return hexData.find((point: [number, number, string, string]) => selected[point[3]] !== true) === undefined; }, [hexData, selected]); const topCategory = useMemo(() => { let highestVal = 0; let highestCategory = ''; for (const i in catMap) { if (catMap[i] > highestVal) { highestVal = catMap[i]; highestCategory = i; } } return highestCategory; }, [catMap]); const hexDivisor = hexData.length / 6; let counter = 0; return ( {hexbinOption === EHexbinOptions.BINS && isCategorySelected ? catMapKeys.sort().map((key) => { const currPath = cutHex( d3Hexbin.hexagon(isSizeScale ? radiusScale(hexData.length) : hexRadius), isSizeScale ? radiusScale(hexData.length) : hexRadius, counter, Math.ceil(catMap[key] / hexDivisor), ); counter += Math.ceil(catMap[key] / hexDivisor); return ( ); }) : null} {hexbinOption === EHexbinOptions.COLOR || !isCategorySelected ? ( ) : null} {hexbinOption === EHexbinOptions.PIE && isCategorySelected ? ( <> {isOpacityScale ? ( ) : null} ) : null} ); }