File size: 36,083 Bytes
20e666e |
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 |
// components/DemographicAnalysis.jsx - Complete Updated File
"use client";
import React, { useState, useMemo, useEffect, useRef } from "react";
import {
BarChart,
Bar,
XAxis,
YAxis,
CartesianGrid,
Tooltip as RechartsTooltip,
Legend,
ResponsiveContainer,
Cell,
LabelList,
} from "recharts";
import {
getSignificanceIndicator,
formatDisplayKey,
getMetricTooltip,
} from "../lib/utils"; // Adjust path as needed
import { Tooltip } from "./Tooltip"; // Your custom Tooltip component
// Helper component for info tooltips with fixed positioning
const InfoTooltip = ({ text }) => {
const [isVisible, setIsVisible] = useState(false);
const [position, setPosition] = useState({ top: 0, left: 0 });
const buttonRef = useRef(null);
// Update position when tooltip becomes visible
useEffect(() => {
if (isVisible && buttonRef.current) {
const rect = buttonRef.current.getBoundingClientRect();
setPosition({
top: rect.top - 10, // Position above the icon with a small gap
left: rect.left + 12, // Center with the icon
});
}
}, [isVisible]);
return (
<div className="relative inline-block ml-1 align-middle">
<button
ref={buttonRef}
className="text-gray-400 hover:text-gray-600 focus:outline-none"
onMouseEnter={() => setIsVisible(true)}
onMouseLeave={() => setIsVisible(false)}
onClick={(e) => {
e.stopPropagation();
setIsVisible(!isVisible);
}}
aria-label="Info"
>
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-4 w-4"
viewBox="0 0 20 20"
fill="currentColor"
>
<path
fillRule="evenodd"
d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z"
clipRule="evenodd"
/>
</svg>
</button>
{isVisible && (
<div
className="fixed p-2 bg-white border-1 rounded shadow-xl text-xs text-gray-700 whitespace-pre-wrap"
style={{
top: `${position.top}px`,
left: `${position.left}px`,
zIndex: 9999,
maxWidth: "250px",
transform: "translate(-50%, -100%)",
}}
>
{text}
</div>
)}
</div>
);
};
// Custom tooltip for DEMOGRAPHIC chart (shows scores per model for a level)
const CustomDemographicTooltip = ({ active, payload, label }) => {
if (active && payload && payload.length) {
const sortedPayload = [...payload].sort(
(a, b) => (b.value || 0) - (a.value || 0)
);
return (
<div className="bg-white p-3 border rounded shadow-lg max-w-xs">
<p className="font-medium text-sm mb-1">{label}</p>
{sortedPayload.map((entry, index) => (
<div key={`item-${index}`} className="flex items-center mt-1">
<div
className="w-3 h-3 mr-2 rounded-full flex-shrink-0"
style={{
backgroundColor:
entry.payload[`${entry.dataKey}_color`] ||
entry.color ||
"#999",
}}
></div>
<span className="text-xs flex-grow pr-2">{entry.name}: </span>
<span className="text-xs font-medium ml-1 whitespace-nowrap">
{typeof entry.value === "number" ? entry.value.toFixed(1) : "N/A"}
</span>
</div>
))}
</div>
);
}
return null;
};
// Custom tooltip for EQUITY GAP chart - UPDATED
const EquityGapTooltip = ({ active, payload }) => {
if (active && payload && payload.length > 0) {
const data = payload[0].payload; // data here IS an item from equityGapChartData (derived from all_equity_gaps)
if (!data || typeof data !== "object") return null;
// Get significance indicator parts
const significanceInfo = getSignificanceIndicator(
data.is_statistically_significant,
data.p_value
);
const ciLower = data.gap_confidence_interval_95_lower;
const ciUpper = data.gap_confidence_interval_95_upper;
return (
<div className="bg-white p-3 border rounded shadow-lg text-xs max-w-xs">
<p className="font-medium text-sm mb-2">{data.model}</p>
<div className="space-y-1">
<div className="flex justify-between">
<span className="font-semibold">Equity Gap:</span>
{/* 'gap' key is used in chart data */}
<span>{data.gap?.toFixed(1) ?? "N/A"} pts</span>
</div>
{data.effect_size !== undefined && data.effect_size !== null && (
<div className="flex justify-between">
<span className="font-semibold">Effect Size:</span>
<span>
{data.effect_size?.toFixed(2) ?? "N/A"} (
{data.effect_size_class || "N/A"})
</span>
</div>
)}
{/* Show Significance */}
<div className="flex justify-between items-center">
<span className="font-semibold">Significance:</span>
<span className={`flex items-center ${significanceInfo.className}`}>
{significanceInfo.tooltip.replace(/Statistically /g, "")}{" "}
{/* Shorten text */}
<span className="ml-1 font-bold">{significanceInfo.symbol}</span>
</span>
</div>
{/* Show Confidence Interval */}
<div className="flex justify-between">
<span className="font-semibold">95% CI:</span>
<span>
{typeof ciLower === "number" && typeof ciUpper === "number"
? `[${ciLower.toFixed(1)}, ${ciUpper.toFixed(1)}]`
: "N/A"}
</span>
</div>
{/* Show Concern Flag */}
{data.is_equity_concern !== undefined && (
<div className="flex justify-between">
<span className="font-semibold">Concern Flag:</span>
<span
className={
data.is_equity_concern
? "font-bold text-red-600"
: "text-gray-600"
}
>
{data.is_equity_concern ? "Yes" : "No"}
</span>
</div>
)}
{/* Show Min/Max Groups */}
<div className="flex justify-between">
<span className="font-semibold">Lowest Group:</span>
<span>
{data.min_level || "N/A"} ({data.min_score?.toFixed(1) ?? "-"})
</span>
</div>
<div className="flex justify-between">
<span className="font-semibold">Highest Group:</span>
<span>
{data.max_level || "N/A"} ({data.max_score?.toFixed(1) ?? "-"})
</span>
</div>
</div>
</div>
);
}
return null;
};
// New helper functions for styling consistency
// New helper function to get badge color for effect size
const getEffectSizeBadgeStyle = (effectSizeClass) => {
switch (effectSizeClass) {
case "Large":
return "bg-red-100 text-red-800";
case "Medium":
return "bg-yellow-100 text-yellow-800";
case "Small":
return "bg-blue-100 text-blue-800";
case "Negligible":
return "bg-green-100 text-green-800";
default:
return "bg-gray-100 text-gray-800";
}
};
// New helper function to get badge color for significance
const getSignificanceBadgeStyle = (isSignificant) => {
if (isSignificant === null || isSignificant === undefined)
return "bg-gray-100 text-gray-800";
return isSignificant
? "bg-blue-100 text-blue-800"
: "bg-gray-100 text-gray-600";
};
// New helper function to get badge color for concern
const getConcernBadgeStyle = (isConcern) => {
if (isConcern === null || isConcern === undefined)
return "bg-gray-100 text-gray-800";
return isConcern ? "bg-red-100 text-red-800" : "bg-green-100 text-green-800";
};
// New helper function to format p-value
const formatPValue = (pValue) => {
if (pValue === null || pValue === undefined) return "N/A";
return `p=${pValue.toFixed(3)}` + (pValue < 0.05 ? " < 0.05" : " ≥ 0.05");
};
// New helper function to create effect size tooltip content
const getEffectSizeTooltip = (effectSize) => {
return `Effect Size: ${effectSize.toFixed(2)}
Calculation: Normalized Effect Size = (Max Score - Min Score) / Category Standard Deviation
Category Standard Deviation: The standard deviation of all demographic scores within this specific category.
Thresholds:
• ≥ 0.8: "Large"
• ≥ 0.5 and < 0.8: "Medium"
• ≥ 0.2 and < 0.5: "Small"
• < 0.2: "Negligible"`;
};
// Main component
const DemographicAnalysis = ({
rawData = { demographicOptions: {}, mrpDemographics: {} }, // Expect camelCase keys here, snake_case inside mrpDemographics
modelsMeta = [], // Expect camelCase keys
metricsData = { highLevelCategories: {}, lowLevelMetrics: {} }, // Expect Title Case keys, contains internalMetricKey
equityAnalysis = { all_equity_gaps: [], universal_issues: [] }, // Expect snake_case keys
}) => {
// Use Title Case metric keys for state and dropdowns
const highLevelMetricDisplayKeys = Object.keys(
metricsData?.highLevelCategories || {}
).sort();
const lowLevelMetricDisplayKeys = Object.keys(
metricsData?.lowLevelMetrics || {}
).sort();
const [selectedDemographicFactor, setSelectedDemographicFactor] =
useState(null);
const [selectedMetricDisplayKey, setSelectedMetricDisplayKey] =
useState(null); // State holds Title Case
const [metricLevel, setMetricLevel] = useState("high");
const currentMetricDisplayKeys = useMemo(
() =>
metricLevel === "high"
? highLevelMetricDisplayKeys
: lowLevelMetricDisplayKeys,
[metricLevel, highLevelMetricDisplayKeys, lowLevelMetricDisplayKeys]
);
const getModelColor = (modelName) =>
modelsMeta.find((m) => m.model === modelName)?.color || "#999999";
// Set default factor
useEffect(() => {
const factors = Object.keys(rawData.demographicOptions || {});
if (!selectedDemographicFactor && factors.length > 0) {
const defaultFactor = factors.includes("Age") ? "Age" : factors.sort()[0];
setSelectedDemographicFactor(defaultFactor);
}
}, [rawData.demographicOptions, selectedDemographicFactor]);
// Set default metric when list available
useEffect(() => {
if (!selectedMetricDisplayKey && currentMetricDisplayKeys.length > 0) {
// Default logic might need adjustment if "Overall" isn't a key
const defaultMetric = currentMetricDisplayKeys.includes("Overall Score")
? "Overall Score"
: currentMetricDisplayKeys[0];
setSelectedMetricDisplayKey(defaultMetric);
} else if (
selectedMetricDisplayKey &&
!currentMetricDisplayKeys.includes(selectedMetricDisplayKey)
) {
setSelectedMetricDisplayKey(
currentMetricDisplayKeys.length > 0 ? currentMetricDisplayKeys[0] : null
);
}
}, [currentMetricDisplayKeys, selectedMetricDisplayKey, metricLevel]);
// Get the internal snake_case key for filtering equity gaps
const internalMetricKey = useMemo(() => {
if (!selectedMetricDisplayKey) return null;
const allMetrics = {
...(metricsData?.highLevelCategories || {}),
...(metricsData?.lowLevelMetrics || {}),
};
// Look up using Title Case display key
return allMetrics[selectedMetricDisplayKey]?.internalMetricKey ?? null;
}, [selectedMetricDisplayKey, metricsData]);
// Filter equity gaps based on internal key and factor
const filteredEquityGaps = useMemo(() => {
// Use internalMetricKey (snake_case) and selectedDemographicFactor
if (
!internalMetricKey ||
!selectedDemographicFactor ||
!equityAnalysis?.all_equity_gaps ||
!Array.isArray(equityAnalysis.all_equity_gaps)
) {
return [];
}
// Filter all_equity_gaps (which has snake_case keys)
return equityAnalysis.all_equity_gaps.filter(
(gap) =>
gap.category === internalMetricKey &&
gap.demographic_factor === selectedDemographicFactor
);
}, [
internalMetricKey,
selectedDemographicFactor,
equityAnalysis?.all_equity_gaps,
]);
// Prepare data for Equity Gap Chart - uses snake_case keys from filteredEquityGaps
const equityGapChartData = useMemo(() => {
return filteredEquityGaps
.map((gap) => ({
// Pass all original snake_case keys needed by tooltip/table
// These keys match the fields expected by EquityGapTooltip
model: gap.model,
gap: gap.score_range ?? 0, // Rename score_range to gap for chart dataKey
score_range: gap.score_range,
effect_size: gap.effect_size,
effect_size_class: gap.effect_size_class,
is_statistically_significant: gap.is_statistically_significant,
p_value: gap.p_value,
gap_confidence_interval_95_lower: gap.gap_confidence_interval_95_lower,
gap_confidence_interval_95_upper: gap.gap_confidence_interval_95_upper,
is_equity_concern: gap.is_equity_concern,
min_level: gap.min_level,
min_score: gap.min_score,
max_level: gap.max_level,
max_score: gap.max_score,
// Add derived properties
color: getModelColor(gap.model),
}))
.sort((a, b) => (a.gap ?? 0) - (b.gap ?? 0)) // Sort by gap size ascending
.map((item, index) => ({ ...item, rank: index + 1 })); // Add rank based on gap size
}, [filteredEquityGaps]); // Depend only on filteredEquityGaps
// Prepare data for Demographic Breakdown Chart
const demographicChartData = useMemo(() => {
// selectedMetricDisplayKey is Title Case, matching keys in mrpDemographics
if (
!selectedDemographicFactor ||
!selectedMetricDisplayKey ||
!rawData.mrpDemographics
)
return [];
const metricKeyInData = selectedMetricDisplayKey; // Use Title Case key
const levels = rawData.demographicOptions[selectedDemographicFactor] || [];
if (levels.length === 0) return [];
const chartData = levels.map((level) => {
const entry = { level };
modelsMeta.forEach((model) => {
// Access mrpDemographics using Title Case metric key
const score =
rawData.mrpDemographics[model.model]?.[selectedDemographicFactor]?.[
level
]?.[metricKeyInData];
entry[model.model] =
score !== undefined && score !== null && score !== "N/A"
? parseFloat(score)
: null;
entry[`${model.model}_color`] = model.color;
});
return entry;
});
return chartData.sort((a, b) => {
if (a.level === "N/A") return 1;
if (b.level === "N/A") return -1;
return a.level.localeCompare(b.level);
});
}, [
selectedDemographicFactor,
selectedMetricDisplayKey,
rawData.mrpDemographics,
rawData.demographicOptions,
modelsMeta,
]);
const modelsWithDemoData = useMemo(
() =>
modelsMeta
.map((m) => m.model)
.filter((modelName) =>
demographicChartData.some(
(d) => d[modelName] !== null && d[modelName] !== undefined
)
),
[modelsMeta, demographicChartData]
);
return (
<div>
{/* Controls Panel */}
<div className="border rounded-lg overflow-hidden mb-6 shadow-sm">
<div className="px-4 py-3 bg-gray-50 border-b">
<h3 className="font-semibold text-gray-800">
Demographic Analysis Controls
</h3>
</div>
<div className="p-4 grid grid-cols-1 md:grid-cols-3 gap-4">
{/* Factor Selector */}
<div>
<label
htmlFor="factorSelect"
className="block text-sm font-medium text-gray-700 mb-1"
>
Demographic Factor
</label>
<select
id="factorSelect"
className="w-full border rounded-md px-3 py-2 bg-white shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500"
value={selectedDemographicFactor || ""}
onChange={(e) => setSelectedDemographicFactor(e.target.value)}
>
<option value="" disabled>
Select factor
</option>
{Object.keys(rawData.demographicOptions || {})
.sort()
.map((factor) => (
<option key={factor} value={factor}>
{formatDisplayKey(factor)}
</option>
))}
</select>
</div>
{/* Level Toggle */}
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
Metric Level
</label>
<div className="flex">
<button
className={`px-3 py-2 text-sm font-medium border ${
metricLevel === "high"
? "bg-blue-100 text-blue-800 border-blue-300"
: "bg-white text-gray-700 border-gray-300 hover:bg-gray-50"
} rounded-l-md flex-1`}
onClick={() => setMetricLevel("high")}
>
High-Level
</button>
<button
className={`px-3 py-2 text-sm font-medium border-t border-b border-r ${
metricLevel === "low"
? "bg-blue-100 text-blue-800 border-blue-300"
: "bg-white text-gray-700 border-gray-300 hover:bg-gray-50"
} rounded-r-md flex-1`}
onClick={() => setMetricLevel("low")}
>
Low-Level
</button>
</div>
</div>
{/* Metric Selector - Uses Title Case keys */}
<div>
<label
htmlFor="metricSelect"
className="block text-sm font-medium text-gray-700 mb-1"
>
<Tooltip content={getMetricTooltip(selectedMetricDisplayKey)}>
<span>
{metricLevel === "high"
? "High-Level Category"
: "Low-Level Metric"}
</span>
</Tooltip>
</label>
<select
id="metricSelect"
className="w-full border rounded-md px-3 py-2 bg-white shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500"
value={selectedMetricDisplayKey || ""}
onChange={(e) => setSelectedMetricDisplayKey(e.target.value)}
disabled={currentMetricDisplayKeys.length === 0}
>
<option value="" disabled>
Select metric
</option>
{/* Iterate through Title Case keys */}
{currentMetricDisplayKeys.map((displayKey) => (
<option key={displayKey} value={displayKey}>
{displayKey}
</option>
))}
</select>
{!selectedMetricDisplayKey &&
currentMetricDisplayKeys.length > 0 && (
<p className="mt-1 text-xs text-gray-500">
Select a metric to view analysis.
</p>
)}
{currentMetricDisplayKeys.length === 0 && (
<p className="mt-1 text-xs text-amber-600">
No {metricLevel} metrics available.
</p>
)}
</div>
</div>
</div>
{/* Demographic Breakdown Chart */}
<div className="border rounded-lg overflow-hidden mb-6 shadow-sm">
<div className="px-4 py-3 bg-gray-50 border-b">
<h3 className="font-semibold text-gray-800">
{selectedMetricDisplayKey || "Metric"} Scores across{" "}
{formatDisplayKey(selectedDemographicFactor) || "Groups"}
<InfoTooltip
text={`Shows the average score (0-100) for each model within each subgroup of ${formatDisplayKey(
selectedDemographicFactor
)}. Higher scores are better.`}
/>
</h3>
</div>
<div className="p-4">
{demographicChartData.length > 0 && modelsWithDemoData.length > 0 ? (
<div className="h-80">
<ResponsiveContainer width="100%" height="100%">
<BarChart
data={demographicChartData}
margin={{ top: 5, right: 5, left: 0, bottom: 60 }}
>
<CartesianGrid strokeDasharray="3 3" vertical={false} />
<XAxis
dataKey="level"
angle={-45}
textAnchor="end"
tick={{ fontSize: 11 }}
interval={0}
height={70}
/>
<YAxis domain={[0, 100]} tick={{ fontSize: 11 }} width={40} />
<RechartsTooltip
content={<CustomDemographicTooltip />}
wrapperStyle={{ zIndex: 10 }}
/>
<Legend
layout="horizontal"
verticalAlign="bottom"
align="center"
wrapperStyle={{ paddingTop: 30 }}
iconSize={10}
/>
{modelsWithDemoData.map((modelName) => (
<Bar
key={modelName}
dataKey={modelName}
name={modelName}
fill={getModelColor(modelName)}
/>
))}
</BarChart>
</ResponsiveContainer>
</div>
) : (
<div className="flex items-center justify-center h-60 bg-gray-50 rounded">
<div className="text-center p-4">
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-10 w-10 mx-auto text-gray-400 mb-3"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M9 17v-2m3 2v-4m3 4v-6m2 10H7a2 2 0 01-2-2V7a2 2 0 012-2h2l2-3h6l2 3h2a2 2 0 012 2v10a2 2 0 01-2 2h-1"
/>
</svg>
<h3 className="text-lg font-medium text-gray-900 mb-1">
No Data Available
</h3>
<p className="text-sm text-gray-600">
{!selectedDemographicFactor
? "Please select a demographic factor."
: !selectedMetricDisplayKey
? "Please select a metric."
: "No score data found."}
</p>
</div>
</div>
)}
</div>
</div>
{/* Equity Gap Comparison Chart */}
<div className="border rounded-lg overflow-hidden mb-6 shadow-sm">
<div className="px-4 py-3 bg-gray-50 border-b">
<h3 className="font-semibold text-gray-800">
Equity Gap Comparison for {selectedMetricDisplayKey || "Metric"}
<InfoTooltip
text={`Compares the maximum score difference observed between ${formatDisplayKey(
selectedDemographicFactor
)} groups for each model. Lower gaps indicate better equity.`}
/>
</h3>
</div>
<div className="p-4">
{equityGapChartData.length > 0 ? (
<div className="h-72">
<ResponsiveContainer width="100%" height="100%">
<BarChart
data={equityGapChartData}
margin={{ top: 5, right: 30, left: 5, bottom: 5 }}
layout="vertical"
>
<CartesianGrid
strokeDasharray="3 3"
horizontal={true}
vertical={false}
/>
<XAxis
type="number"
dataKey="gap"
domain={[0, "auto"]}
tick={{ fontSize: 11 }}
allowDecimals={false}
/>
<YAxis
dataKey="model"
type="category"
width={130}
tick={{ fontSize: 11 }}
/>
<RechartsTooltip
content={<EquityGapTooltip />}
wrapperStyle={{ zIndex: 10 }}
/>
<Bar
dataKey="gap"
name="Equity Gap"
barSize={20}
radius={[0, 4, 4, 0]}
>
{equityGapChartData.map((entry, index) => (
<Cell
key={`cell-${index}`}
fill={entry.color}
fillOpacity={0.8}
/>
))}
<LabelList
dataKey="gap"
position="right"
formatter={(value) => value?.toFixed(1) ?? ""}
style={{ fontSize: 11, fill: "#6b7280" }}
/>
</Bar>
</BarChart>
</ResponsiveContainer>
</div>
) : (
<div className="flex items-center justify-center h-60 bg-gray-50 rounded">
<div className="text-center p-4">
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-10 w-10 mx-auto text-gray-400 mb-3"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M9 17v-2m3 2v-4m3 4v-6m2 10H7a2 2 0 01-2-2V7a2 2 0 012-2h2l2-3h6l2 3h2a2 2 0 012 2v10a2 2 0 01-2 2h-1"
/>
</svg>
<h3 className="text-lg font-medium text-gray-900 mb-1">
No Equity Gap Data
</h3>
<p className="text-sm text-gray-600">
{!selectedDemographicFactor
? "Select factor."
: !selectedMetricDisplayKey
? "Select metric."
: "No equity gaps found."}
</p>
</div>
</div>
)}
{equityGapChartData.length > 0 && (
<p className="mt-3 text-xs text-gray-500">
Chart ranks models by equity gap size (lower is better).
</p>
)}
</div>
</div>
{/* Equity Gap Details Table - IMPROVED */}
{equityGapChartData.length > 0 && (
<div className="border rounded-lg overflow-hidden mb-6 shadow-sm">
<div className="px-4 py-3 bg-gray-50 border-b">
<h3 className="font-semibold text-gray-800">
Detailed Equity Gaps: {selectedMetricDisplayKey || "Metric"} by{" "}
{formatDisplayKey(selectedDemographicFactor) || "Factor"}
</h3>
</div>
<div className="p-4 overflow-x-auto">
<table className="min-w-full divide-y divide-gray-200">
<thead className="bg-gray-50">
<tr>
<th className="px-3 py-2 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Rank
</th>
<th className="px-3 py-2 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Model
</th>
<th className="px-3 py-2 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Equity Gap
</th>
<th className="px-3 py-2 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Effect Size
</th>
<th className="px-3 py-2 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Significance
</th>
<th className="px-3 py-2 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">
Concern?
</th>
<th className="px-3 py-2 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Lowest Group (Score)
</th>
<th className="px-3 py-2 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Highest Group (Score)
</th>
</tr>
</thead>
<tbody className="bg-white divide-y divide-gray-200">
{equityGapChartData.map((gap) => {
const minScoreDisplay =
typeof gap.min_score === "number"
? gap.min_score.toFixed(1)
: "-";
const maxScoreDisplay =
typeof gap.max_score === "number"
? gap.max_score.toFixed(1)
: "-";
return (
<tr
key={gap.model}
className={`hover:bg-gray-50 ${
gap.is_equity_concern ? "bg-red-50" : ""
}`}
>
<td className="px-3 py-2 whitespace-nowrap text-sm text-gray-500">
{gap.rank}
</td>
<td className="px-3 py-2 whitespace-nowrap">
<div className="flex items-center">
<div
className="w-3 h-3 rounded-full mr-2 flex-shrink-0"
style={{ backgroundColor: gap.color }}
></div>
<span className="text-sm font-medium text-gray-900">
{gap.model}
</span>
</div>
</td>
<td className="px-3 py-2 whitespace-nowrap text-sm font-medium">
{/* Equity Gap as plain text */}
{gap.gap !== undefined && gap.gap !== null
? gap.gap.toFixed(1)
: "N/A"}
</td>
<td className="px-3 py-2 whitespace-nowrap text-sm">
{gap.effect_size !== undefined &&
gap.effect_size !== null ? (
<div className="flex items-center">
<span
className={`px-2 py-0.5 rounded-full text-xs font-medium ${getEffectSizeBadgeStyle(
gap.effect_size_class
)}`}
>
{gap.effect_size_class || "N/A"}
</span>
<InfoTooltip
text={getEffectSizeTooltip(gap.effect_size)}
/>
</div>
) : (
<span className="text-gray-500">N/A</span>
)}
</td>
<td className="px-3 py-2 whitespace-nowrap text-sm">
<div className="flex flex-col">
<div className="flex items-center">
<span
className={`px-2 py-0.5 rounded-full text-xs font-medium ${getSignificanceBadgeStyle(
gap.is_statistically_significant
)}`}
>
{gap.is_statistically_significant ? (
<span>Significant ✔</span>
) : (
<span>Not Significant ✘</span>
)}
</span>
</div>
<div className="text-xs text-gray-500 mt-1">
{gap.p_value !== undefined && gap.p_value !== null
? formatPValue(gap.p_value)
: ""}
</div>
</div>
</td>
<td className="px-3 py-2 whitespace-nowrap text-sm text-center">
<span
className={`inline-block px-2 py-0.5 rounded-full text-xs font-medium ${getConcernBadgeStyle(
gap.is_equity_concern
)}`}
>
{gap.is_equity_concern ? "Yes" : "No"}
</span>
</td>
<td className="px-3 py-2 whitespace-nowrap text-sm">
{gap.min_level ? (
<div className="flex flex-col">
<span className="font-medium">{gap.min_level}</span>
<span className="text-gray-500">
{minScoreDisplay}
</span>
</div>
) : (
<span className="text-gray-500">-</span>
)}
</td>
<td className="px-3 py-2 whitespace-nowrap text-sm">
{gap.max_level ? (
<div className="flex flex-col">
<span className="font-medium">{gap.max_level}</span>
<span className="text-gray-500">
{maxScoreDisplay}
</span>
</div>
) : (
<span className="text-gray-500">-</span>
)}
</td>
</tr>
);
})}
</tbody>
</table>
</div>
{/* Table Footer/Explanation - IMPROVED */}
<div className="px-4 pb-4 pt-2 text-xs text-gray-600">
<div className="space-y-1">
<p>
<span className="font-semibold">Rank:</span> Based on lowest
Equity Gap value for this metric/factor
</p>
<p>
<span className="font-semibold">Equity Gap:</span> Score
difference (0-100 points) between highest and lowest scoring
groups
</p>
<p>
<span className="font-semibold">Effect Size:</span> Gap
magnitude relative to score variation (hover for details)
</p>
<p>
<span className="font-semibold">Significance:</span>Whether the
gap is statistically significant after adjusting for multiple
tests (Benjamini-Hochberg FDR correction, q<0.05)
</p>
<p>
<span className="font-semibold">Concern?:</span> 'Yes' flags
potential equity concerns (Large Effect Size AND Statistically
Significant)
</p>
</div>
</div>
</div>
)}
</div>
);
};
export default DemographicAnalysis;
|