Skip to content

Commit 5807e79

Browse files
author
Ritika Mishra
committed
added real-time value display on beta candle
1 parent 99057f7 commit 5807e79

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

src/components/BandPowerGraph.tsx

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,26 @@ const Graph: React.FC<GraphProps> = ({
6868
},
6969
[FREQ_RESOLUTION]
7070
);
71-
71+
7272
useEffect(() => {
7373
if (fftData.length > 0 && fftData[0].length > 0) {
7474
const channelData = fftData[0];
75-
75+
7676
const deltaPower = calculateBandPower(channelData, DELTA_RANGE);
7777
const thetaPower = calculateBandPower(channelData, THETA_RANGE);
7878
const alphaPower = calculateBandPower(channelData, ALPHA_RANGE);
7979
const betaPower = calculateBandPower(channelData, BETA_RANGE);
8080
const gammaPower = calculateBandPower(channelData, GAMMA_RANGE);
8181
const total = deltaPower + thetaPower + alphaPower + betaPower + gammaPower;
82-
82+
8383
const newBandPowerData = [
8484
(deltaPower / total) * 100,
8585
(thetaPower / total) * 100,
8686
(alphaPower / total) * 100,
8787
(betaPower / total) * 100,
8888
(gammaPower / total) * 100,
8989
];
90-
90+
9191
if (
9292
newBandPowerData.some((value) => !isNaN(value) && value > -Infinity)
9393
) {
@@ -97,14 +97,14 @@ const Graph: React.FC<GraphProps> = ({
9797
}
9898
return prev;
9999
});
100-
100+
101101
if (onBetaUpdate) {
102102
onBetaUpdate(newBandPowerData[3]);
103103
}
104-
}
104+
}
105105
}
106106
}, [fftData, calculateBandPower, onBetaUpdate && onBetaUpdate.toString()]); // Memoized dependencies
107-
107+
108108

109109
const drawGraph = useCallback(
110110
(currentBandPowerData: number[]) => {
@@ -132,7 +132,8 @@ const Graph: React.FC<GraphProps> = ({
132132
ctx.clearRect(0, 0, width, height);
133133

134134
// Responsive bar sizing and margins
135-
const leftMargin = width < 640 ? 40 : 70; // Smaller margin on mobile
135+
const leftMargin = width < 500 ? 50 : 70;
136+
136137
const rightMargin = 20;
137138
const bottomMargin = width < 640 ? 40 : 50; // Smaller margin on mobile
138139
const barWidth = (width - leftMargin - rightMargin) / bandNames.length;
@@ -156,15 +157,23 @@ const Graph: React.FC<GraphProps> = ({
156157
ctx.stroke();
157158

158159
// Draw bars
160+
// Draw bars and beta percentage
159161
currentBandPowerData.forEach((power, index) => {
160162
const x = leftMargin + index * barWidth;
161-
const normalizedHeight = Math.max(0, (power - minPower) / (maxPower - minPower));
162-
const barHeight = Math.max(0,normalizedHeight * (height - bottomMargin - 10));
163-
163+
const normalizedHeight = Math.max(0, (power - minPower) / (maxPower - minPower));
164+
const barHeight = Math.max(0, normalizedHeight * (height - bottomMargin - 10));
165+
166+
const barX = x + barSpacing / 2;
167+
const barY = height - bottomMargin - barHeight;
168+
const actualBarWidth = barWidth - barSpacing * 1.5; // Make it thinner than before
169+
164170
ctx.fillStyle = bandColors[index];
165-
ctx.fillRect(x + barSpacing / 2, height - bottomMargin - barHeight, barWidth - barSpacing, barHeight);
171+
ctx.fillRect(barX, barY, actualBarWidth, barHeight);
172+
173+
166174
});
167175

176+
168177
// Draw labels
169178
ctx.fillStyle = axisColor;
170179
const fontSize = width < 640 ? 10 : 12; // Smaller text on mobile
@@ -188,16 +197,19 @@ const Graph: React.FC<GraphProps> = ({
188197
ctx.fillText(band, labelX, height - bottomMargin + 5);
189198
});
190199

191-
// Title
192-
ctx.font = `${Math.min(fontSize + 2, 14)}px Arial`;
193-
ctx.fillText("EEG Band Power", width / 2, height - 20);
200+
// EEG Band Power – same as "Frequency (Hz)"
201+
ctx.font = "16px Arial";
202+
ctx.textAlign = "center";
203+
ctx.fillText("EEG Band Power", (width + leftMargin) / 2, height - 17);
194204

195-
// Rotate and position the y-axis label
205+
// Power – same as "Magnitude"
196206
ctx.save();
197207
ctx.rotate(-Math.PI / 2);
208+
ctx.font = "20px Arial";
198209
ctx.textAlign = "center";
199-
ctx.fillText("Power", -height / 2 + 15, fontSize);
210+
ctx.fillText("Power", -height / 2, 15);
200211
ctx.restore();
212+
201213
},
202214
[theme, bandColors, bandNames]
203215
);
@@ -243,12 +255,12 @@ const Graph: React.FC<GraphProps> = ({
243255
return (
244256
<div
245257
ref={containerRef}
246-
className={`w-full h-full min-h-0 min-w-0`}
258+
className={`w-full h-full min-h-0 min-w-0 px-4`}
247259
>
248-
<canvas
249-
ref={canvasRef}
250-
className="w-full h-full dark:bg-highlight rounded-md"
251-
/>
260+
<canvas
261+
ref={canvasRef}
262+
className="w-full h-full dark:bg-highlight rounded-md"
263+
/>
252264
</div>
253265
);
254266
};

src/components/CandleLit.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,17 @@ const BrightCandleView: React.FC<BrightCandleViewProps> = ({ fftData = [], betaP
8484
before:absolute before:inset-0 before:bg-white/10 before:opacity-40 before:rounded-b-xl before:rounded-t-md
8585
after:absolute after:bottom-0 after:left-0 after:right-0 after:h-2 after:bg-gradient-to-b after:from-transparent after:to-gray-300/30"
8686
>
87+
8788
<div className="absolute inset-0 overflow-hidden rounded-b-xl rounded-t-md bg-gradient-to-b from-cyan-300 via-blue-400 to-blue-600
8889
">
90+
8991
<div className="absolute top-2 left-2 right-2 h-0.5 bg-gray-300/30"></div>
90-
<div className="absolute top-6 left-2 right-2 h-0.5 bg-gray-300/30"></div>
92+
93+
<div className="absolute top-2 left-1/2 transform -translate-x-1/2 text-center">
94+
<div className="text-sm font-semibold text-gray-700 px-2 py-1 rounded-md ">
95+
Beta: {betaPower.toFixed(1)}%
96+
</div>
97+
</div>
9198
</div>
9299
</div>
93100

@@ -151,6 +158,8 @@ const BrightCandleView: React.FC<BrightCandleViewProps> = ({ fftData = [], betaP
151158
className="transition-all duration-300"
152159
/>
153160
</svg>
161+
162+
154163
</div>
155164
</div>
156165
);

0 commit comments

Comments
 (0)