Jaguar_Project / frontend /src /components /FeatureChart.js
xenecode's picture
Claude Opus 5.5
Translate frontend and backend text to English
6c68d01
Raw History Blame Contribute Delete
11 kB
import React, { useMemo } from "react";
import { Line } from "react-chartjs-2";
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
PointElement,
LineElement,
Tooltip,
Legend,
Title,
Filler
} from "chart.js";
ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
Tooltip,
Legend,
Title,
Filler
);
// Dark theme colors (the hmi palette in tailwind.config.js)
const HMI = {
text: '#e8e6e1',
muted: '#9197a0',
line: 'rgba(255, 255, 255, 0.07)',
accent: '#f97316',
ok: '#4ade80',
err: '#f87171',
warn: '#facc15',
raised: '#1f2226',
};
ChartJS.defaults.color = HMI.muted;
ChartJS.defaults.borderColor = HMI.line;
ChartJS.defaults.font.family = '"JetBrains Mono", "DejaVu Sans Mono", ui-monospace, monospace';
const StatTile = ({ label, value, tone = 'text-hmi-text' }) => (
<div className="border border-hmi-line bg-hmi-raised px-3 py-2 text-center">
<div className="text-[11px] font-medium uppercase tracking-[0.12em] text-hmi-muted">{label}</div>
<div className={`font-mono text-lg font-semibold tabular-nums ${tone}`}>{value}</div>
</div>
);
const LegendItem = ({ swatch, label }) => (
<div className="flex items-center gap-2">
{swatch}
<span>{label}</span>
</div>
);
const FeatureChart = ({ data, selectedFeature }) => {
const chartData = useMemo(() => {
// Extract feature data and tolerance information
const featureValues = [];
const toleranceInfo = { target: null, tolerance: null };
data.forEach(item => {
const feature = item.features.find(f => f.name === selectedFeature);
if (feature) {
featureValues.push({
iteration: item.iteration,
value: feature.value,
withinTolerance: feature.tolerance_check?.within_tolerance,
color: feature.value_color ? `#${feature.value_color}` : HMI.accent
});
// Store tolerance info from the first valid entry
if (feature.tolerance_check && toleranceInfo.target === null) {
toleranceInfo.target = feature.tolerance_check.target;
toleranceInfo.tolerance = feature.tolerance_check.tolerance;
}
}
});
// Create upper and lower bound arrays if tolerance info exists
const upperBoundData = [];
const lowerBoundData = [];
const targetLineData = [];
if (toleranceInfo.target !== null) {
data.forEach(() => {
upperBoundData.push(toleranceInfo.target + toleranceInfo.tolerance);
lowerBoundData.push(toleranceInfo.target - toleranceInfo.tolerance);
targetLineData.push(toleranceInfo.target);
});
}
return {
labels: featureValues.map(item => `Iter ${item.iteration}`),
datasets: [
{
label: selectedFeature,
data: featureValues.map(item => item.value),
borderColor: HMI.accent,
backgroundColor: 'rgba(249, 115, 22, 0.1)',
pointBackgroundColor: featureValues.map(item => item.color),
pointBorderColor: featureValues.map(item => item.withinTolerance === false ? HMI.err : HMI.ok),
pointBorderWidth: 2,
pointRadius: 5,
pointHoverRadius: 7,
tension: 0.3,
borderWidth: 2,
fill: false,
},
...(toleranceInfo.target !== null ? [
{
label: 'Target',
data: targetLineData,
borderColor: HMI.text,
borderWidth: 2,
borderDash: [5, 5],
pointRadius: 0,
fill: false,
},
{
label: 'Upper bound',
data: upperBoundData,
borderColor: HMI.warn,
backgroundColor: 'rgba(250, 204, 21, 0.06)',
borderWidth: 1,
borderDash: [3, 3],
pointRadius: 0,
fill: '+1',
},
{
label: 'Lower bound',
data: lowerBoundData,
borderColor: HMI.warn,
backgroundColor: 'rgba(250, 204, 21, 0.06)',
borderWidth: 1,
borderDash: [3, 3],
pointRadius: 0,
fill: false,
}
] : [])
]
};
}, [data, selectedFeature]);
const chartOptions = {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'top',
labels: {
usePointStyle: true,
padding: 20,
color: HMI.text
}
},
tooltip: {
backgroundColor: HMI.raised,
borderColor: '#2c3036',
borderWidth: 1,
cornerRadius: 0,
titleColor: HMI.text,
bodyColor: HMI.text,
footerColor: HMI.muted,
callbacks: {
label: function(context) {
let label = context.dataset.label || '';
if (label) {
label += ': ';
}
if (context.parsed.y !== null) {
label += context.parsed.y.toFixed(3);
}
return label;
},
footer: function(tooltipItems) {
const dataIndex = tooltipItems[0].dataIndex;
const feature = data[dataIndex]?.features.find(f => f.name === selectedFeature);
if (feature?.tolerance_check) {
const { target, tolerance, distance, within_tolerance } = feature.tolerance_check;
return [
`Target: ${target}`,
`Tolerance: ±${tolerance}`,
`Deviation: ${distance.toFixed(3)}`,
`Status: ${within_tolerance ? 'Within tolerance' : 'Out of tolerance'}`
];
}
return '';
}
}
},
title: {
display: true,
text: `Feature trend: ${selectedFeature}`,
color: HMI.text,
font: {
size: 15,
weight: 'bold'
},
padding: {
top: 10,
bottom: 20
}
}
},
scales: {
x: {
grid: {
display: true,
color: HMI.line
},
ticks: { color: HMI.muted }
},
y: {
beginAtZero: false,
grid: {
display: true,
color: HMI.line
},
ticks: { color: HMI.muted }
}
}
};
// Compute feature statistics
const featureStats = useMemo(() => {
const values = data
.map(item => item.features.find(f => f.name === selectedFeature)?.value)
.filter(value => value !== undefined && value !== null);
if (values.length === 0) return null;
const sum = values.reduce((a, b) => a + b, 0);
const avg = sum / values.length;
const min = Math.min(...values);
const max = Math.max(...values);
const targetFeature = data[0]?.features.find(f => f.name === selectedFeature);
const target = targetFeature?.tolerance_check?.target;
const tolerance = targetFeature?.tolerance_check?.tolerance;
return { avg, min, max, target, tolerance };
}, [data, selectedFeature]);
return (
<div className="mb-4 border border-hmi-line bg-hmi-panel px-4 py-4">
{selectedFeature ? (
<>
{/* Feature stats summary */}
{featureStats && (
<div className="mb-4 grid grid-cols-2 gap-2 md:grid-cols-5">
<StatTile label="Average" value={featureStats.avg.toFixed(3)} />
<StatTile label="Min" value={featureStats.min.toFixed(3)} />
<StatTile label="Max" value={featureStats.max.toFixed(3)} />
{featureStats.target !== undefined && (
<>
<StatTile label="Target" value={featureStats.target} tone="text-hmi-accent" />
<StatTile label="Tolerance" value={`±${featureStats.tolerance}`} tone="text-hmi-warn" />
</>
)}
</div>
)}
{/* Chart container with fixed height */}
<div className="h-64 md:h-80">
<Line data={chartData} options={chartOptions} />
</div>
{/* Legend explanation */}
<div className="mt-4 flex flex-wrap justify-center gap-x-5 gap-y-2 text-sm text-hmi-muted">
<LegendItem swatch={<span className="inline-block h-3 w-3 bg-hmi-accent" />} label="Measured value" />
<LegendItem swatch={<span className="inline-block h-0 w-6 border-t-2 border-dashed border-hmi-text" />} label="Target value" />
<LegendItem swatch={<span className="inline-block h-0 w-6 border-t-2 border-dashed border-hmi-warn" />} label="Tolerance range" />
<LegendItem swatch={<span className="inline-block h-3 w-3 border-2 border-hmi-err" />} label="Out of tolerance" />
<LegendItem swatch={<span className="inline-block h-3 w-3 border-2 border-hmi-ok" />} label="Within tolerance" />
</div>
</>
) : (
<div className="flex h-64 items-center justify-center text-hmi-muted">
Select a feature to see its trend
</div>
)}
</div>
);
};
export default FeatureChart;