import React, { useState } from "react";
const HistogramBuilderWidget: React.FC = () => {
const [mode, setMode] = useState<"count" | "percent">("count");
// Data: [60, 70), [70, 80), [80, 90), [90, 100)
const data = [
{ bin: "60-70", count: 4, label: "60s" },
{ bin: "70-80", count: 9, label: "70s" },
{ bin: "80-90", count: 6, label: "80s" },
{ bin: "90-100", count: 1, label: "90s" },
];
const total = data.reduce((acc, curr) => acc + curr.count, 0); // 20
const maxCount = Math.max(...data.map((d) => d.count));
const maxPercent = maxCount / total; // 0.45
return (
Test Scores Distribution
{/* Y Axis Labels */}
{mode === "count"
? maxCount + 1
: ((maxPercent + 0.05) * 100).toFixed(0) + "%"}
{mode === "count"
? Math.round((maxCount + 1) / 2)
: (((maxPercent + 0.05) / 2) * 100).toFixed(0) + "%"}
0
{data.map((d, i) => {
// Normalize to max height of graph area roughly
// Actually map 0 to maxScale
const maxScale = mode === "count" ? maxCount + 1 : maxPercent + 0.05;
const val = mode === "count" ? d.count : d.count / total;
const hPercent = (val / maxScale) * 100;
return (
{/* Tooltip */}
{d.bin}:{" "}
{mode === "count"
? d.count
: `${((d.count / total) * 100).toFixed(0)}%`}
{/* Bar */}
{/* Bin Label */}
{d.label}
);
})}
Key Takeaway: Notice that the{" "}
shape of the
distribution stays exactly the same. Only the{" "}
Y-axis scale{" "}
changes.
);
};
export default HistogramBuilderWidget;