All files / components/hooks/gapChart useGapChartRenderer.ts

0% Statements 0/30
0% Branches 0/40
0% Functions 0/4
0% Lines 0/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151                                                                                                                                                                                                                                                                                                             
import { useMemo } from "react";
import { ScaleBand, ScaleLinear, ScaleTime } from "d3";
 
interface DataItem {
  label: string;
  code?: string;
  value1: number;
  value2: number;
  difference: number;
  date: string;
}
 
interface UseGapChartRendererProps {
  processedDataSet: DataItem[];
  xScale: ScaleLinear<number, number> | ScaleTime<number, number>;
  yScale: ScaleBand<string>;
  getColor: (label: string) => string;
  getShapeColor: (type: "value1" | "value2" | "gap", label: string) => string;
  colorMode: "label" | "shape";
  highlightItems: string[];
  shapeValue1: "circle" | "square" | "triangle";
  shapeValue2: "circle" | "square" | "triangle";
  hoveredYItem: string | null;
  animationState?: {
    entering: Set<string>;
    exiting: Set<string>;
    updating: Set<string>;
  };
  getItemOpacity?: (label: string, defaultOpacity: number) => number;
  getItemTransform?: (label: string) => string;
  shouldTransition?: (label: string) => boolean;
}
 
export const useGapChartRenderer = ({
  processedDataSet,
  xScale,
  yScale,
  getColor,
  getShapeColor,
  colorMode,
  highlightItems,
  shapeValue1,
  shapeValue2,
  hoveredYItem,
  animationState,
  getItemOpacity,
  getItemTransform,
  shouldTransition,
}: UseGapChartRendererProps) => {
  const renderData = useMemo(() => {
    const elements = processedDataSet.map((d, i) => {
      const y = yScale(d.label) || 0;
      const barHeight = yScale.bandwidth();
 
      // Get colors based on color mode
      const gapColor = colorMode === "shape" ? getShapeColor("gap", d.label) : getColor(d.label);
      const value1Color =
        colorMode === "shape" ? getShapeColor("value1", d.label) : getColor(d.label);
      const value2Color =
        colorMode === "shape" ? getShapeColor("value2", d.label) : getColor(d.label);
 
      // Calculate positions
      const x1 = xScale(Math.min(d.value1, d.value2));
      const x2 = xScale(Math.max(d.value1, d.value2));
      const barWidth = x2 - x1;
 
      // Determine if highlighted
      const isHighlighted = highlightItems.length === 0 || highlightItems.includes(d.label);
 
      // Determine opacity based on hover state and animation
      const baseBarOpacity =
        hoveredYItem !== null
          ? hoveredYItem === d.label
            ? isHighlighted
              ? 0.7
              : 0.3
            : 0.3
          : isHighlighted
            ? 0.7
            : 0.3;
 
      const baseMarkerOpacity =
        hoveredYItem !== null ? (hoveredYItem === d.label ? 1 : 0.3) : isHighlighted ? 1 : 0.3;
 
      // Apply animation opacity if available
      const barOpacity = getItemOpacity ? getItemOpacity(d.label, baseBarOpacity) : baseBarOpacity;
      const markerOpacity = getItemOpacity
        ? getItemOpacity(d.label, baseMarkerOpacity)
        : baseMarkerOpacity;
      const itemTransform = getItemTransform ? getItemTransform(d.label) : "scale(1)";
      const hasTransition = shouldTransition ? shouldTransition(d.label) : false;
 
      return {
        d,
        i,
        y,
        barHeight,
        gapColor,
        value1Color,
        value2Color,
        x1,
        x2,
        barWidth,
        barOpacity,
        markerOpacity,
        itemTransform,
        hasTransition,
      };
    });
 
    // Separate shapes by type for layering
    const squares: typeof elements = [];
    const nonSquares: typeof elements = [];
 
    elements.forEach(element => {
      const hasSquareValue1 = shapeValue1 === "square";
      const hasSquareValue2 = shapeValue2 === "square";
 
      if (hasSquareValue1 || hasSquareValue2) {
        squares.push(element);
      }
      if (!hasSquareValue1 || !hasSquareValue2) {
        nonSquares.push(element);
      }
    });
 
    return {
      elements,
      squares,
      nonSquares,
    };
  }, [
    processedDataSet,
    xScale,
    yScale,
    getColor,
    getShapeColor,
    colorMode,
    highlightItems,
    shapeValue1,
    shapeValue2,
    hoveredYItem,
    animationState,
    getItemOpacity,
    getItemTransform,
    shouldTransition,
  ]);
 
  return renderData;
};