mirror of
https://github.com/dylanbenzi/yellowbird.git
synced 2026-11-27 01:07:01 +00:00
138 lines
2.9 KiB
TypeScript
138 lines
2.9 KiB
TypeScript
"use client";
|
|
|
|
import {
|
|
createChart,
|
|
ColorType,
|
|
LineSeriesPartialOptions,
|
|
AreaSeriesPartialOptions,
|
|
BaselineSeriesPartialOptions,
|
|
CandlestickSeriesPartialOptions,
|
|
HistogramSeriesPartialOptions,
|
|
SingleValueData,
|
|
WhitespaceData,
|
|
LineData,
|
|
CandlestickData,
|
|
HistogramData,
|
|
} from "lightweight-charts";
|
|
import React, { MutableRefObject, useEffect, useRef } from "react";
|
|
|
|
interface ChartProps {
|
|
colors?: {
|
|
backgroundColor?: string;
|
|
textColor?: string;
|
|
};
|
|
series: Array<
|
|
| LineSeriesPartialOptionsWithData
|
|
| AreaSeriesPartialOptionsWithData
|
|
| BaselineSeriesPartialOptionsWithData
|
|
| CandlestickSeriesPartialOptionsWithData
|
|
| HistogramSeriesPartialOptionsWithData
|
|
>;
|
|
}
|
|
|
|
interface LineChartWithData {
|
|
type: "line";
|
|
data: Array<LineData | WhitespaceData>;
|
|
}
|
|
|
|
interface AreaChartWithData {
|
|
type: "area";
|
|
data: Array<SingleValueData | WhitespaceData>;
|
|
}
|
|
|
|
interface BaselineChartWithData {
|
|
type: "baseline";
|
|
data: Array<SingleValueData | WhitespaceData>;
|
|
}
|
|
|
|
interface CandlestickChartWithData {
|
|
type: "candle";
|
|
data: Array<CandlestickData | WhitespaceData>;
|
|
}
|
|
|
|
interface HistogramChartWithData {
|
|
type: "histogram";
|
|
data: Array<HistogramData | WhitespaceData>;
|
|
}
|
|
|
|
export type LineSeriesPartialOptionsWithData = LineSeriesPartialOptions &
|
|
LineChartWithData;
|
|
|
|
export type AreaSeriesPartialOptionsWithData = AreaSeriesPartialOptions &
|
|
AreaChartWithData;
|
|
|
|
export type BaselineSeriesPartialOptionsWithData =
|
|
BaselineSeriesPartialOptions & BaselineChartWithData;
|
|
|
|
export type CandlestickSeriesPartialOptionsWithData =
|
|
CandlestickSeriesPartialOptions & CandlestickChartWithData;
|
|
|
|
export type HistogramSeriesPartialOptionsWithData =
|
|
HistogramSeriesPartialOptions & HistogramChartWithData;
|
|
|
|
const Chart = ({ colors, series }: ChartProps) => {
|
|
const chartContainerRef = useRef() as MutableRefObject<HTMLDivElement>;
|
|
|
|
useEffect(() => {
|
|
const handleResize = () => {
|
|
chart.applyOptions({
|
|
width: chartContainerRef.current.clientWidth * 0.95,
|
|
});
|
|
};
|
|
|
|
const chart = createChart(chartContainerRef.current, {
|
|
layout: {
|
|
background: {
|
|
type: ColorType.Solid,
|
|
color: colors?.backgroundColor,
|
|
},
|
|
textColor: colors?.textColor,
|
|
},
|
|
width: chartContainerRef.current.clientWidth * 0.95,
|
|
height: chartContainerRef.current.clientHeight * 0.95,
|
|
grid: {
|
|
vertLines: {
|
|
visible: false,
|
|
},
|
|
},
|
|
});
|
|
chart.timeScale().fitContent();
|
|
|
|
series.forEach((seriesItem) => {
|
|
switch (seriesItem.type) {
|
|
case "line":
|
|
const lineChart = chart.addLineSeries({
|
|
color: seriesItem.color,
|
|
});
|
|
|
|
lineChart.setData(seriesItem.data);
|
|
case "area":
|
|
|
|
case "baseline":
|
|
|
|
case "candle":
|
|
|
|
case "histogram":
|
|
}
|
|
});
|
|
|
|
window.addEventListener("resize", handleResize);
|
|
|
|
return () => {
|
|
window.removeEventListener("resize", handleResize);
|
|
|
|
chart.remove();
|
|
};
|
|
});
|
|
|
|
return (
|
|
<div
|
|
className="flex justify-center h-full"
|
|
ref={chartContainerRef}
|
|
id="chart"
|
|
/>
|
|
);
|
|
};
|
|
|
|
export default Chart;
|