-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: connect chart and depth components by price
- Loading branch information
Showing
6 changed files
with
253 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@use '../../styles/mixins-vars/margins.scss' as margins; | ||
@use '../../styles/mixins-vars/paddings.scss' as paddings; | ||
|
||
.orderbook-page { | ||
.chart-depth-connector { | ||
min-width: paddings.$p-3; | ||
max-width: 50px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { useCallback, useState } from 'react'; | ||
import useResizeObserver from '@react-hook/resize-observer'; | ||
|
||
import './OrderbookChartConnector.scss'; | ||
|
||
type ConnectionPoint = [number, number]; | ||
|
||
export default function OrderbookChartConnector({ | ||
connectionPoints, | ||
}: { | ||
connectionPoints?: ConnectionPoint[]; | ||
}) { | ||
// define what to draw | ||
const draw = useCallback( | ||
(canvas: HTMLCanvasElement | null) => { | ||
const ctx = canvas?.getContext('2d'); | ||
if (canvas && ctx) { | ||
// reset canvas | ||
canvas.width = canvas.offsetWidth; | ||
canvas.height = canvas.offsetHeight; | ||
const width = canvas.width; | ||
const height = canvas.height; | ||
ctx?.clearRect(0, 0, width, height); | ||
// draw elements | ||
if (connectionPoints?.length) { | ||
drawConnectionPoints(ctx, connectionPoints); | ||
} | ||
} | ||
|
||
// define drawing functions | ||
function drawConnectionPoints( | ||
ctx: CanvasRenderingContext2D, | ||
connectionPoints: ConnectionPoint[], | ||
width: number = ctx.canvas.width | ||
) { | ||
ctx.lineWidth = 1; | ||
ctx.lineJoin = 'round'; | ||
ctx.strokeStyle = 'white'; | ||
ctx.beginPath(); | ||
connectionPoints.forEach(([y1, y2]) => { | ||
ctx.moveTo(sharpPoint(0), sharpPoint(y1)); | ||
ctx.lineTo(sharpPoint(0.7 * width), sharpPoint(y1)); | ||
ctx.lineTo(sharpPoint(0.9 * width), sharpPoint(y2)); | ||
ctx.lineTo(sharpPoint(width), sharpPoint(y2)); | ||
}); | ||
ctx.stroke(); | ||
} | ||
|
||
// use points centered at half-pixels for sharp lines | ||
function sharpPoint(value: number): number { | ||
return Math.round(value) + 0.5; | ||
} | ||
}, | ||
[connectionPoints] | ||
); | ||
|
||
// store ref but also draw on canvas when first found | ||
const [canvas, setCanvas] = useState<HTMLCanvasElement | null>(null); | ||
const getCanvasRef = useCallback( | ||
(canvas: HTMLCanvasElement | null) => { | ||
setCanvas(canvas); | ||
draw(canvas); | ||
}, | ||
[draw] | ||
); | ||
|
||
// redraw canvas when the screen size changes | ||
useResizeObserver(canvas, () => draw(canvas)); | ||
|
||
return <canvas className="flex" ref={getCanvasRef}></canvas>; | ||
} |
Oops, something went wrong.