Skip to content

Commit 67ca344

Browse files
author
Ritika Mishra
committed
updated ui of bandpower and candle
1 parent 2c3f4a9 commit 67ca344

File tree

3 files changed

+775
-773
lines changed

3 files changed

+775
-773
lines changed

src/components/BandPowerGraph.tsx

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use client";
12
import React, {
23
useEffect,
34
useRef,
@@ -111,108 +112,103 @@ const Graph: React.FC<GraphProps> = ({
111112
const canvas = canvasRef.current;
112113
const container = containerRef.current;
113114
if (!canvas || !container) return;
114-
115+
115116
if (currentBandPowerData.some(isNaN)) {
116117
console.error("NaN values detected in band power data");
117118
return;
118119
}
119-
120+
120121
const ctx = canvas.getContext("2d");
121122
if (!ctx) return;
122-
123+
123124
// Responsive canvas sizing
124125
const containerWidth = container.clientWidth;
125126
const containerHeight = Math.min(containerWidth * 0.5, 400); // Limit max height
126127
canvas.width = containerWidth;
127128
canvas.height = containerHeight;
128-
129+
129130
const width = canvas.width;
130131
const height = canvas.height;
131-
132+
132133
ctx.clearRect(0, 0, width, height);
133-
134-
// Responsive bar sizing and margins
135-
const leftMargin = width < 500 ? 50 : 70;
136-
137-
const rightMargin = 20;
138-
const bottomMargin = width < 640 ? 40 : 50; // Smaller margin on mobile
134+
135+
// inside drawGraph:
136+
const topMargin = 30; // space for any in-canvas labels
137+
const leftMargin = width < 500 ? 60 : 80;
138+
const bottomMargin = width < 640 ? 70 : 80;
139+
const rightMargin = 60;
140+
141+
// Draw axes
142+
const axisColor = theme === "dark" ? "white" : "black";
143+
ctx.beginPath();
144+
ctx.moveTo(leftMargin, topMargin);
145+
ctx.lineTo(leftMargin, height - bottomMargin);
146+
ctx.lineTo(width - rightMargin, height - bottomMargin);
147+
ctx.strokeStyle = axisColor;
148+
ctx.stroke();
149+
139150
const barWidth = (width - leftMargin - rightMargin) / bandNames.length;
140151
const barSpacing = barWidth * 0.2; // Space between bars
141-
152+
142153
let minPower = 0;
143154
let maxPower = 100;
144-
145155
if (maxPower - minPower < 1) {
146156
maxPower = minPower + 1;
147157
}
148-
149-
const axisColor = theme === "dark" ? "white" : "black";
150-
151-
// Draw axes
152-
ctx.beginPath();
153-
ctx.moveTo(leftMargin, 10);
154-
ctx.lineTo(leftMargin, height - bottomMargin);
155-
ctx.lineTo(width - rightMargin, height - bottomMargin);
156-
ctx.strokeStyle = axisColor;
157-
ctx.stroke();
158-
158+
159159
// Draw bars
160-
// Draw bars and beta percentage
161160
currentBandPowerData.forEach((power, index) => {
162161
const x = leftMargin + index * barWidth;
163162
const normalizedHeight = Math.max(0, (power - minPower) / (maxPower - minPower));
164-
const barHeight = Math.max(0, normalizedHeight * (height - bottomMargin - 10));
165-
163+
const barHeight = Math.max(0, normalizedHeight * (height - bottomMargin - topMargin));
164+
166165
const barX = x + barSpacing / 2;
167166
const barY = height - bottomMargin - barHeight;
168167
const actualBarWidth = barWidth - barSpacing * 1.5; // Make it thinner than before
169-
168+
170169
ctx.fillStyle = bandColors[index];
171170
ctx.fillRect(barX, barY, actualBarWidth, barHeight);
172-
173-
174171
});
175-
176-
177-
// Draw labels
172+
173+
// Y-axis labels
178174
ctx.fillStyle = axisColor;
179175
const fontSize = width < 640 ? 10 : 12; // Smaller text on mobile
180176
ctx.font = `${fontSize}px Arial`;
181-
182-
// Y-axis labels (log scale)
183177
ctx.textAlign = "right";
184178
ctx.textBaseline = "middle";
185-
const yLabelCount = Math.min(5, Math.floor(height / 50)); // Fewer labels on small screens
179+
const yLabelCount = Math.min(5, Math.floor(height / 50));
186180
for (let i = 0; i <= yLabelCount; i++) {
187181
const value = minPower + (maxPower - minPower) * (i / yLabelCount);
188-
const labelY = height - bottomMargin - (i / yLabelCount) * (height - bottomMargin - 10);
182+
const labelY = height - bottomMargin - (i / yLabelCount) * (height - bottomMargin - topMargin);
189183
ctx.fillText(value.toFixed(1), leftMargin - 5, labelY);
190184
}
191-
192-
// X-axis labels
185+
186+
// X-axis labels, centered under each actual bar
193187
ctx.textAlign = "center";
194188
ctx.textBaseline = "top";
195189
bandNames.forEach((band, index) => {
196-
const labelX = leftMargin + index * barWidth + barWidth * 0.5;
190+
const barX = leftMargin + index * barWidth + barSpacing / 2;
191+
const barW = barWidth - barSpacing * 1.5;
192+
const labelX = barX + barW / 2;
197193
ctx.fillText(band, labelX, height - bottomMargin + 5);
198194
});
199-
200-
// EEG Band Power – same as "Frequency (Hz)"
201-
ctx.font = "16px Arial";
195+
196+
// Title
197+
ctx.font = "1.2em Arial";
202198
ctx.textAlign = "center";
203199
ctx.fillText("EEG Band Power", (width + leftMargin) / 2, height - 17);
204-
205-
// Power – same as "Magnitude"
200+
201+
// Power (Y-axis name) inside canvas
206202
ctx.save();
207203
ctx.rotate(-Math.PI / 2);
208-
ctx.font = "20px Arial";
204+
ctx.font = "1.2em Arial";
209205
ctx.textAlign = "center";
210206
ctx.fillText("Power", -height / 2, 15);
211207
ctx.restore();
212-
213208
},
214209
[theme, bandColors, bandNames]
215210
);
211+
216212

217213
// Rest of the component remains the same (animateGraph, useEffect hooks)
218214
const animateGraph = useCallback(() => {

0 commit comments

Comments
 (0)