All files / components/Utils CountHistogram.jsx

100% Statements 21/21
100% Branches 13/13
100% Functions 4/4
100% Lines 21/21

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 152 153 154 155 156 157 158 159 160 161 162                        168x 75x             93x 93x   93x 93x     522x 522x     93x 93x 93x 93x 93x   93x                                                                                       197x 197x 197x   197x                                                                                                   460x 460x 460x                                                
import { calculateBarHeight } from "./countHistogramUtils";
 
// Stryker disable all - Come back and refactor code to make it more testable and then re-enable mutation testing
export default function CountHistogram({
  data,
  s,
  width = 600,
  height = 400,
  xLabel = "Value Range",
  yLabel = "Count",
  testid = "count-histogram",
}) {
  if (!data || data.length === 0) {
    return (
      <div data-testid={`${testid}-empty`}>
        <p>No data to display</p>
      </div>
    );
  }
 
  const m = Math.max(...data);
  const numBins = Math.floor(m / s) + 1;
 
  const bins = new Array(numBins).fill(0);
  data.forEach((value) => {
    // binIndex is always < numBins: value <= m, so
    // Math.floor(value / s) <= Math.floor(m / s) = numBins - 1
    const binIndex = Math.floor(value / s);
    bins[binIndex]++;
  });
 
  const maxCount = Math.max(...bins);
  const margin = { top: 20, right: 20, bottom: 40, left: 50 };
  const chartWidth = width - margin.left - margin.right;
  const chartHeight = height - margin.top - margin.bottom;
  const barWidth = chartWidth / numBins;
 
  return (
    <svg
      data-testid={testid}
      width={width}
      height={height}
      style={{ border: "1px solid #ccc" }}
    >
      {/* Y-axis */}
      <line
        x1={margin.left}
        y1={margin.top}
        x2={margin.left}
        y2={height - margin.bottom}
        stroke="black"
        strokeWidth="2"
      />
      {/* X-axis */}
      <line
        x1={margin.left}
        y1={height - margin.bottom}
        x2={width - margin.right}
        y2={height - margin.bottom}
        stroke="black"
        strokeWidth="2"
      />
 
      {/* Y-axis label */}
      <text
        x={15}
        y={height / 2}
        textAnchor="middle"
        transform={`rotate(-90 15 ${height / 2})`}
        fontSize="12"
      >
        {yLabel}
      </text>
 
      {/* X-axis label */}
      <text x={width / 2} y={height - 5} textAnchor="middle" fontSize="12">
        {xLabel}
      </text>
 
      {/* Bars */}
      {bins.map((count, i) => {
        const barHeight = calculateBarHeight(count, maxCount, chartHeight);
        const x = margin.left + i * barWidth;
        const y = height - margin.bottom - barHeight;
 
        return (
          <g key={i}>
            <rect
              x={x}
              y={y}
              width={barWidth}
              height={barHeight}
              fill="#4A90E2"
              stroke="#2E5C8A"
              strokeWidth="1"
              data-testid={`histogram-bar-${i}`}
            />
            {/* X-axis tick labels - only show for every few bins to avoid crowding */}
            {i % Math.ceil(numBins / 8) === 0 && (
              <>
                <line
                  x1={x}
                  y1={height - margin.bottom}
                  x2={x}
                  y2={height - margin.bottom + 5}
                  stroke="black"
                  strokeWidth="1"
                />
                <text
                  x={x}
                  y={height - margin.bottom + 20}
                  textAnchor="middle"
                  fontSize="10"
                >
                  {i * s}
                </text>
              </>
            )}
          </g>
        );
      })}
 
      {/* End label for rightmost bin */}
      <text
        x={width - margin.right}
        y={height - margin.bottom + 20}
        textAnchor="end"
        fontSize="10"
      >
        {(numBins - 1) * s + s}
      </text>
 
      {/* Y-axis tick marks and labels */}
      {maxCount > 0 &&
        Array.from({ length: 5 }).map((_, i) => {
          const tickValue = Math.round((i / 4) * maxCount);
          const y = height - margin.bottom - (i / 4) * chartHeight;
          return (
            <g key={`tick-${i}`}>
              <line
                x1={margin.left - 5}
                y1={y}
                x2={margin.left}
                y2={y}
                stroke="black"
                strokeWidth="1"
              />
              <text
                x={margin.left - 10}
                y={y + 3}
                textAnchor="end"
                fontSize="10"
              >
                {tickValue}
              </text>
            </g>
          );
        })}
    </svg>
  );
}