File size: 11,000 Bytes
fba4209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c68d01
b225f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fba4209
 
 
 
 
b225f7d
fba4209
 
b225f7d
fba4209
 
 
 
 
b225f7d
fba4209
b225f7d
fba4209
 
 
 
 
 
 
b225f7d
fba4209
 
 
 
b225f7d
fba4209
 
 
 
 
 
 
b225f7d
fba4209
6c68d01
fba4209
 
 
 
b225f7d
 
fba4209
b225f7d
fba4209
 
 
 
 
 
 
 
 
6c68d01
fba4209
b225f7d
fba4209
 
 
 
 
 
6c68d01
fba4209
b225f7d
 
fba4209
 
 
 
 
 
6c68d01
fba4209
b225f7d
 
fba4209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b225f7d
 
fba4209
 
 
b225f7d
 
 
 
 
 
 
fba4209
 
 
 
 
 
 
 
 
 
 
 
 
 
b225f7d
fba4209
 
 
6c68d01
 
 
 
fba4209
 
 
 
 
 
 
 
6c68d01
b225f7d
fba4209
b225f7d
fba4209
 
 
 
 
 
 
 
 
 
 
 
b225f7d
 
 
fba4209
 
 
 
 
b225f7d
 
 
fba4209
 
 
 
 
 
 
 
 
b225f7d
fba4209
b225f7d
fba4209
 
 
 
b225f7d
fba4209
 
 
b225f7d
fba4209
 
 
 
b225f7d
fba4209
 
 
 
b225f7d
6c68d01
b225f7d
6c68d01
fba4209
 
6c68d01
 
fba4209
 
 
 
b225f7d
fba4209
 
 
 
b225f7d
fba4209
b225f7d
6c68d01
 
 
 
 
fba4209
 
 
b225f7d
6c68d01
fba4209
 
 
 
 
 
 
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
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;