"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; 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 (
); }; export default Chart;