All files / components/Statistics CommonsAveragesOverTimeGraph.jsx

100% Statements 28/28
100% Branches 7/7
100% Functions 13/13
100% Lines 24/24

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    3x     96x 96x       8x   25x 25x 7x 7x 7x 7x 7x 64x     150x   7x                             35x                                 19x               14x 70x 11x 14x 14x     25x 25x                                                                       14x                                                  
import React from "react";
 
const COLORS = ["#005ea8", "#a23b00", "#287a37", "#783fa0"];
 
function monthNumber(period) {
  const [year, month] = period.split("-").map(Number);
  return year * 12 + month;
}
 
export default function CommonsAveragesOverTimeGraph({ rows }) {
  if (rows.length === 0) return null;
 
  const periods = [...new Set(rows.map((row) => row.period))].sort();
  const codes = [...new Set(rows.map((row) => row.diningCommonsCode))].sort();
  const firstMonth = monthNumber(periods[0]);
  const monthSpan = monthNumber(periods[periods.length - 1]) - firstMonth;
  const width = Math.max(600, periods.length * 90);
  const plotWidth = width - 100;
  const x = (period) =>
    monthSpan === 0
      ? 60 + plotWidth / 2
      : 60 + ((monthNumber(period) - firstMonth) / monthSpan) * plotWidth;
  const y = (score) => 260 - ((score - 1) / 4) * 220;
 
  return (
    <figure>
      <div className="overflow-auto">
        <svg
          role="img"
          aria-label="Average review scores by dining commons over time"
          viewBox={`0 0 ${width} 320`}
          style={{ width: "100%", minWidth: width }}
        >
          <desc>
            Monthly average scores on a scale of 1 to 5. Points show months with
            reviews; missing months have no score. Exact values are in the table
            below.
          </desc>
          {[1, 2, 3, 4, 5].map((score) => (
            <g key={score}>
              <line
                x1="60"
                x2={width - 40}
                y1={y(score)}
                y2={y(score)}
                stroke="#dee2e6"
              />
              <text x="45" y={y(score) + 5} textAnchor="end">
                {score}
              </text>
            </g>
          ))}
          <text x="60" y="20">
            Average score
          </text>
          {periods.map((period) => (
            <text key={period} x={x(period)} y="285" textAnchor="middle">
              {period}
            </text>
          ))}
          <text x={60 + plotWidth / 2} y="310" textAnchor="middle">
            Month served
          </text>
          {codes.map((code, index) => {
            const points = rows
              .filter((row) => row.diningCommonsCode === code)
              .sort((a, b) => a.period.localeCompare(b.period));
            const color = COLORS[index % COLORS.length];
            return (
              <g key={code}>
                {points.map((row, pointIndex) => {
                  const previous = points[pointIndex - 1];
                  return (
                    <React.Fragment key={row.period}>
                      {previous &&
                        monthNumber(row.period) -
                          monthNumber(previous.period) ===
                          1 && (
                          <line
                            x1={x(previous.period)}
                            y1={y(previous.averageStars)}
                            x2={x(row.period)}
                            y2={y(row.averageStars)}
                            stroke={color}
                            strokeWidth="2"
                            strokeDasharray={`${index + 2} ${index}`}
                          />
                        )}
                      <circle
                        cx={x(row.period)}
                        cy={y(row.averageStars)}
                        r="5"
                        fill={color}
                      >
                        <title>
                          {`${code}, ${row.period}: ${row.averageStars.toFixed(2)} (${row.reviewCount} reviews)`}
                        </title>
                      </circle>
                    </React.Fragment>
                  );
                })}
              </g>
            );
          })}
        </svg>
      </div>
      <figcaption>
        {codes.map((code, index) => (
          <span key={code} className="d-inline-block me-3">
            <svg className="me-1" width="30" height="12" aria-hidden="true">
              <line
                x1="0"
                y1="6"
                x2="30"
                y2="6"
                stroke={COLORS[index % COLORS.length]}
                strokeWidth="2"
                strokeDasharray={`${index + 2} ${index}`}
              />
              <circle
                cx="15"
                cy="6"
                r="4"
                fill={COLORS[index % COLORS.length]}
              />
            </svg>
            {code}
          </span>
        ))}
      </figcaption>
    </figure>
  );
}