mirror of
https://github.com/dylanbenzi/yellowbird.git
synced 2026-11-27 09:17:00 +00:00
71 lines
1.6 KiB
TypeScript
71 lines
1.6 KiB
TypeScript
"use client";
|
|
|
|
import { createChart, ColorType } from "lightweight-charts";
|
|
import React, { MutableRefObject, useEffect, useRef } from "react";
|
|
import { indexData, portfolioData } from "@/app/MockData/MockData";
|
|
|
|
const Chart = (props: any) => {
|
|
const {
|
|
data,
|
|
colors: {
|
|
backgroundColor = "transparent",
|
|
portfolioLineColor = "#FACC15",
|
|
indexLineColor = "#FFFFFF",
|
|
textColor = "white",
|
|
} = {},
|
|
} = props;
|
|
|
|
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: backgroundColor },
|
|
textColor,
|
|
},
|
|
width: chartContainerRef.current.clientWidth * 0.95,
|
|
height: chartContainerRef.current.clientHeight * 0.95,
|
|
grid: {
|
|
vertLines: {
|
|
visible: false,
|
|
},
|
|
},
|
|
});
|
|
chart.timeScale().fitContent();
|
|
|
|
const portfolioSeries = chart.addLineSeries({
|
|
color: portfolioLineColor,
|
|
});
|
|
portfolioSeries.setData(portfolioData);
|
|
|
|
const indexSeries = chart.addLineSeries({
|
|
color: indexLineColor,
|
|
});
|
|
indexSeries.setData(indexData);
|
|
|
|
window.addEventListener("resize", handleResize);
|
|
|
|
return () => {
|
|
window.removeEventListener("resize", handleResize);
|
|
|
|
chart.remove();
|
|
};
|
|
}, [data, backgroundColor, portfolioLineColor, indexLineColor, textColor]);
|
|
|
|
return (
|
|
<div
|
|
className="flex justify-center h-full"
|
|
ref={chartContainerRef}
|
|
id="chart"
|
|
/>
|
|
);
|
|
};
|
|
|
|
export default Chart;
|