All files / components/Statistics CommonsAveragesOverTimeChart.jsx

100% Statements 12/12
100% Branches 3/3
100% Functions 6/6
100% Lines 11/11

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                            8x 8x 32x 32x   8x 10x               5x               5x 26x   5x                   15x                          
import React from "react";
import {
  CartesianGrid,
  Legend,
  Line,
  LineChart,
  ResponsiveContainer,
  Tooltip,
  XAxis,
  YAxis,
} from "recharts";
 
// eslint-disable-next-line react-refresh/only-export-components -- pure helper exported for tests
export function pivotRows(rows) {
  const byPeriod = {};
  rows.forEach(({ diningCommonsCode, period, averageStars }) => {
    byPeriod[period] = byPeriod[period] || { period };
    byPeriod[period][diningCommonsCode] = averageStars;
  });
  return Object.values(byPeriod).sort((a, b) =>
    a.period.localeCompare(b.period),
  );
}
 
export default function CommonsAveragesOverTimeChart({
  rows,
  testIdPrefix = "CommonsAveragesOverTimeChart",
}) {
  const COLORS = [
    "#1f77b4",
    "#d62728",
    "#2ca02c",
    "#ff7f0e",
    "#9467bd",
    "#8c564b",
  ];
  const data = pivotRows(rows);
  const codes = [...new Set(rows.map((row) => row.diningCommonsCode))];
 
  return (
    <div data-testid={testIdPrefix} style={{ width: "100%", height: 400 }}>
      <ResponsiveContainer width="100%" height="100%">
        <LineChart data={data}>
          <CartesianGrid strokeDasharray="3 3" />
          <XAxis dataKey="period" />
          <YAxis domain={[1, 5]} />
          <Tooltip />
          <Legend />
          {codes.map((code, index) => (
            <Line
              key={code}
              type="monotone"
              dataKey={code}
              stroke={COLORS[index % COLORS.length]}
              connectNulls
            />
          ))}
        </LineChart>
      </ResponsiveContainer>
    </div>
  );
}