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' }) => (
);
const LegendItem = ({ swatch, label }) => (
{swatch}
{label}
);
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 (
{selectedFeature ? (
<>
{/* Feature stats summary */}
{featureStats && (
{featureStats.target !== undefined && (
<>
>
)}
)}
{/* Chart container with fixed height */}
{/* Legend explanation */}
} label="Measured value" />
} label="Target value" />
} label="Tolerance range" />
} label="Out of tolerance" />
} label="Within tolerance" />
>
) : (
Select a feature to see its trend
)}
);
};
export default FeatureChart;