Drawing tools
Fibonacci & Gann
Fib retracement, trend-based fib extension and Gann fan.
"use client";
import { ChartCanvas, Pane } from "@reincharts/core";
import { CandlestickSeries } from "@reincharts/series";
import { Grid, XAxis, YAxis } from "@reincharts/axes";
import { Crosshair } from "@reincharts/coordinates";
import { type Drawing, Drawings, DrawingStyleToolbar, DrawingToolbar, useDrawings } from "@reincharts/interactive";
import { daily } from "@/lib/data";
import { useDocsChartTheme } from "@/lib/use-docs-theme";
// a point on the bar `back` bars before the last one
const at = (back: number, price: number) => ({ time: daily[daily.length - 1 - back].date.getTime(), price });
const defaultDrawings: Drawing[] = [
{ id: "retracement", type: "fibRetracement", pane: "main", points: [at(99, 50.4), at(76, 57.7)], style: {} },
{
id: "extension",
type: "fibExtension",
pane: "main",
points: [at(34, 56.7), at(24, 60.1), at(11, 57.6)],
style: {},
},
{ id: "gann", type: "gannFan", pane: "main", points: [at(55, 55.8), at(40, 57.4)], style: {} },
];
export default function FibTools() {
const theme = useDocsChartTheme();
const drawings = useDrawings({ defaultDrawings });
return (
<div className="flex size-full flex-col">
{/* only this group's tools, as a horizontal toolbar */}
<DrawingToolbar
controller={drawings}
tools={["fibRetracement", "fibExtension", "gannFan"]}
orientation="horizontal"
/>
<div className="min-h-0 flex-1">
<ChartCanvas data={daily} theme={theme} barSpacing={8}>
<Pane id="main">
<Grid />
<CandlestickSeries />
<YAxis />
</Pane>
<XAxis />
<Crosshair />
<Drawings controller={drawings} />
<DrawingStyleToolbar controller={drawings} />
</ChartCanvas>
</div>
</div>
);
}| Tool | Type | Points |
|---|---|---|
| Fib retracement | fibRetracement | 2 |
| Trend-based fib extension | fibExtension | 3 |
| Gann fan | gannFan | 2 |
Levels are part of the drawing style: style.levels is a list of { value, color?, visible? }, so each drawing can
have its own levels. fillOpacity controls the per-level fills (0 disables them) and showLabels the level labels.
The defaults are exported as FIB_LEVELS and GANN_LEVELS.
drawings.add({
type: "fibRetracement",
points: [a, b],
style: { levels: [{ value: 0 }, { value: 0.382 }, { value: 0.5 }, { value: 0.618, color: "#f59e0b" }, { value: 1 }] },
});