refactored DreamCharts to consolidate chart rendering logic into reusable renderChart function; improved chart resize handling for better responsiveness and maintainability

Signed-off-by: Matthias Puchstein <matthias@puchstein.bayern>
This commit is contained in:
2025-07-17 03:56:10 +02:00
parent 936a2fa1ee
commit 618e5b442e

View File

@@ -16,6 +16,10 @@ export const EEGChart: React.FC<{ chipInput: ChipInput }> = ({chipInput}) => {
{name: 'Delta', values: chipInput.eeg.delta} {name: 'Delta', values: chipInput.eeg.delta}
]; ];
// Function to render the chart
const renderChart = () => {
if (!chartRef.current) return;
// Clear previous chart // Clear previous chart
d3.select(chartRef.current).selectAll('*').remove(); d3.select(chartRef.current).selectAll('*').remove();
@@ -98,14 +102,15 @@ export const EEGChart: React.FC<{ chipInput: ChipInput }> = ({chipInput}) => {
.attr('dy', '0.32em') .attr('dy', '0.32em')
.style('fill', 'var(--text)') .style('fill', 'var(--text)')
.text(d => d.name); .text(d => d.name);
};
// Initial render
renderChart();
// Handle resize // Handle resize
const handleResize = () => { const handleResize = () => {
if (!chartRef.current) return; if (!chartRef.current) return;
renderChart();
// Re-render chart on resize
d3.select(chartRef.current).selectAll('*').remove();
// We would re-render the chart here, but for simplicity we'll just reload the component
}; };
window.addEventListener('resize', handleResize); window.addEventListener('resize', handleResize);
@@ -127,6 +132,10 @@ export const VitalsChart: React.FC<{ chipInput: ChipInput }> = ({chipInput}) =>
{name: 'HRV', values: chipInput.hrv} {name: 'HRV', values: chipInput.hrv}
]; ];
// Function to render the chart
const renderChart = () => {
if (!chartRef.current) return;
// Clear previous chart // Clear previous chart
d3.select(chartRef.current).selectAll('*').remove(); d3.select(chartRef.current).selectAll('*').remove();
@@ -211,14 +220,15 @@ export const VitalsChart: React.FC<{ chipInput: ChipInput }> = ({chipInput}) =>
.attr('dy', '0.32em') .attr('dy', '0.32em')
.style('fill', 'var(--text)') .style('fill', 'var(--text)')
.text(d => d.name); .text(d => d.name);
};
// Initial render
renderChart();
// Handle resize // Handle resize
const handleResize = () => { const handleResize = () => {
if (!chartRef.current) return; if (!chartRef.current) return;
renderChart();
// Re-render chart on resize
d3.select(chartRef.current).selectAll('*').remove();
// We would re-render the chart here, but for simplicity we'll just reload the component
}; };
window.addEventListener('resize', handleResize); window.addEventListener('resize', handleResize);
@@ -239,6 +249,10 @@ export const MovementChart: React.FC<{ chipInput: ChipInput }> = ({chipInput}) =
{name: 'Bewegung', values: chipInput.bewegung.map(v => v * 100)} // Scale movement for better visibility {name: 'Bewegung', values: chipInput.bewegung.map(v => v * 100)} // Scale movement for better visibility
]; ];
// Function to render the chart
const renderChart = () => {
if (!chartRef.current) return;
// Clear previous chart // Clear previous chart
d3.select(chartRef.current).selectAll('*').remove(); d3.select(chartRef.current).selectAll('*').remove();
@@ -317,14 +331,15 @@ export const MovementChart: React.FC<{ chipInput: ChipInput }> = ({chipInput}) =
.attr('dy', '0.32em') .attr('dy', '0.32em')
.style('fill', 'var(--text)') .style('fill', 'var(--text)')
.text(d => d.name); .text(d => d.name);
};
// Initial render
renderChart();
// Handle resize // Handle resize
const handleResize = () => { const handleResize = () => {
if (!chartRef.current) return; if (!chartRef.current) return;
renderChart();
// Re-render chart on resize
d3.select(chartRef.current).selectAll('*').remove();
// We would re-render the chart here, but for simplicity we'll just reload the component
}; };
window.addEventListener('resize', handleResize); window.addEventListener('resize', handleResize);