Skip to content

Commit 4f7ff8b

Browse files
Format, use theme font
1 parent a569b46 commit 4f7ff8b

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

src/components/LiveGraph.tsx

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* SPDX-License-Identifier: MIT
66
*/
7-
import { Box } from "@chakra-ui/react";
7+
import { Box, useTheme } from "@chakra-ui/react";
88
import { useSize } from "@chakra-ui/react-use-size";
99
import { AccelerometerDataEvent } from "@microbit/microbit-connection";
1010
import { useCallback, useEffect, useRef } from "react";
@@ -31,7 +31,7 @@ const recordingFill = "rgba(0, 0, 255, 0.03)";
3131
const recordingStroke = "rgba(64, 64, 255, 0.27)";
3232
const recordingLineWidth = 2;
3333
const labelAreaWidth = 38;
34-
const labelFont = "20px sans-serif";
34+
const labelFontSize = "20px";
3535
const labelMinSpacing = 24;
3636

3737
interface RecordingRegion {
@@ -84,11 +84,7 @@ function valueToY(value: number, height: number): number {
8484
return height * (1 - (value + maxAccelerationScaleForGraphs) / range);
8585
}
8686

87-
function timeToX(
88-
t: number,
89-
now: number,
90-
width: number
91-
): number {
87+
function timeToX(t: number, now: number, width: number): number {
9288
return width - (now - t) / millisPerPixel;
9389
}
9490

@@ -131,7 +127,8 @@ function drawLabels(
131127
buf: RingBuffer,
132128
width: number,
133129
height: number,
134-
colors: { x: string; y: string; z: string }
130+
colors: { x: string; y: string; z: string },
131+
fontFamily: string
135132
) {
136133
if (buf.count === 0) return;
137134

@@ -159,7 +156,7 @@ function drawLabels(
159156
fixOverlappingLabels(labels);
160157

161158
const graphWidth = width - labelAreaWidth;
162-
ctx.font = labelFont;
159+
ctx.font = `${labelFontSize} ${fontFamily}`;
163160
ctx.textBaseline = "middle";
164161

165162
const arrowWidth = 9;
@@ -201,7 +198,8 @@ function render(
201198
regions: RecordingRegion[],
202199
axes: AxisConfig[],
203200
colors: { x: string; y: string; z: string },
204-
prevFirstVisible: number
201+
prevFirstVisible: number,
202+
fontFamily: string
205203
) {
206204
// Clear.
207205
ctx.clearRect(0, 0, width, height);
@@ -226,7 +224,9 @@ function render(
226224
const dx = (buf.timestamps[idxB] - buf.timestamps[idxA]) / millisPerPixel;
227225
for (const axis of axes) {
228226
if (!axis.lineDash) continue;
229-
const dy = valueToY(axis.values[idxB], height) - valueToY(axis.values[idxA], height);
227+
const dy =
228+
valueToY(axis.values[idxB], height) -
229+
valueToY(axis.values[idxA], height);
230230
const segLen = Math.sqrt(dx * dx + dy * dy);
231231
const totalDash = axis.lineDash[0] + axis.lineDash[1];
232232
axis.dashOffset = (axis.dashOffset + segLen) % totalDash;
@@ -335,7 +335,7 @@ function render(
335335
ctx.restore();
336336

337337
// Labels in the reserved right area.
338-
drawLabels(ctx, buf, width, height, colors);
338+
drawLabels(ctx, buf, width, height, colors, fontFamily);
339339

340340
return firstVisible;
341341
}
@@ -345,6 +345,8 @@ const LiveGraph = () => {
345345
const [{ graphColorScheme, graphLineScheme, graphLineWeight }] =
346346
useSettings();
347347

348+
const theme = useTheme();
349+
const fontFamily: string = theme.fonts?.body ?? "sans-serif";
348350
const colors = useGraphColors(graphColorScheme);
349351
const lineStyles = useGraphLineStyles(graphLineScheme);
350352
const canvasRef = useRef<HTMLCanvasElement>(null);
@@ -365,6 +367,8 @@ const LiveGraph = () => {
365367
lineStylesRef.current = lineStyles;
366368
const lineWidthRef = useRef(lineWidth);
367369
lineWidthRef.current = lineWidth;
370+
const fontFamilyRef = useRef(fontFamily);
371+
fontFamilyRef.current = fontFamily;
368372

369373
// Recording region tracking.
370374
const isRecording = useStore((s) => s.isRecording);
@@ -407,9 +411,27 @@ const LiveGraph = () => {
407411
let lastRenderTime = 0;
408412
let prevFirstVisible = -1;
409413
const axisConfigs: AxisConfig[] = [
410-
{ values: null!, color: "", lineWidth: 0, lineDash: undefined, dashOffset: 0 },
411-
{ values: null!, color: "", lineWidth: 0, lineDash: undefined, dashOffset: 0 },
412-
{ values: null!, color: "", lineWidth: 0, lineDash: undefined, dashOffset: 0 },
414+
{
415+
values: null!,
416+
color: "",
417+
lineWidth: 0,
418+
lineDash: undefined,
419+
dashOffset: 0,
420+
},
421+
{
422+
values: null!,
423+
color: "",
424+
lineWidth: 0,
425+
lineDash: undefined,
426+
dashOffset: 0,
427+
},
428+
{
429+
values: null!,
430+
color: "",
431+
lineWidth: 0,
432+
lineDash: undefined,
433+
dashOffset: 0,
434+
},
413435
];
414436

415437
const animate = () => {
@@ -458,7 +480,8 @@ const LiveGraph = () => {
458480
regionsRef.current,
459481
axisConfigs,
460482
c,
461-
prevFirstVisible
483+
prevFirstVisible,
484+
fontFamilyRef.current
462485
);
463486
};
464487

@@ -467,12 +490,7 @@ const LiveGraph = () => {
467490
}, [isConnected]);
468491

469492
return (
470-
<Box
471-
ref={containerRef}
472-
width="100%"
473-
height="100%"
474-
overflow="hidden"
475-
>
493+
<Box ref={containerRef} width="100%" height="100%" overflow="hidden">
476494
<canvas
477495
ref={canvasRef}
478496
width={width}

0 commit comments

Comments
 (0)