From c9dde12f834f45c4969098152c9c91cb5ea120f4 Mon Sep 17 00:00:00 2001 From: dylanbenzi Date: Sun, 3 Mar 2024 22:57:13 -0500 Subject: [PATCH] removed adv chart --- src/components/charts/AdvChart.tsx | 210 ------------------ .../charts/{PortfolioChart.tsx => Chart.tsx} | 4 +- src/components/charts/TestChart.tsx | 70 ++++++ src/components/modules/HomepageModules.tsx | 26 ++- 4 files changed, 90 insertions(+), 220 deletions(-) delete mode 100644 src/components/charts/AdvChart.tsx rename src/components/charts/{PortfolioChart.tsx => Chart.tsx} (95%) create mode 100644 src/components/charts/TestChart.tsx diff --git a/src/components/charts/AdvChart.tsx b/src/components/charts/AdvChart.tsx deleted file mode 100644 index b0edc01..0000000 --- a/src/components/charts/AdvChart.tsx +++ /dev/null @@ -1,210 +0,0 @@ -import { createChart } from "lightweight-charts"; -import React, { - createContext, - forwardRef, - useCallback, - useContext, - useEffect, - useImperativeHandle, - useLayoutEffect, - useRef, - useState, -} from "react"; - -const Context = createContext(); - -const initialData = [ - { time: "2018-10-11", value: 52.89 }, - { time: "2018-10-12", value: 51.65 }, - { time: "2018-10-13", value: 51.56 }, - { time: "2018-10-14", value: 50.19 }, - { time: "2018-10-15", value: 51.86 }, - { time: "2018-10-16", value: 51.25 }, -]; -const currentDate = new Date(initialData[initialData.length - 1].time); - -export const App = (props: any) => { - const { - colors: { - backgroundColor = "white", - lineColor = "#2962FF", - textColor = "black", - } = {}, - } = props; - - const [chartLayoutOptions, setChartLayoutOptions] = useState({}); - // The following variables illustrate how a series could be updated. - const series1 = useRef(null); - const [started, setStarted] = useState(false); - - // The purpose of this effect is purely to show how a series could - // be updated using the `reference` passed to the `Series` component. - useEffect(() => { - if (series1.current === null) { - return; - } - - if (started) { - const interval = setInterval(() => { - currentDate.setDate(currentDate.getDate() + 1); - const next = { - time: currentDate.toISOString().slice(0, 10), - value: 53 - 2 * Math.random(), - }; - series1.current.update(next); - }, 1000); - return () => clearInterval(interval); - } - }, [started]); - - useEffect(() => { - setChartLayoutOptions({ - background: { - color: backgroundColor, - }, - textColor, - }); - }, [backgroundColor, textColor]); - - return ( - <> - - - - - - ); -}; - -export function Chart(props: any) { - const [container, setContainer] = useState(false); - const handleRef = useCallback((ref) => setContainer(ref), []); - return ( -
- {container && } -
- ); -} - -interface chartContainerProps { - children: any; - container: any; - layout: any; -} - -export const ChartContainer = forwardRef((props: chartContainerProps, ref) => { - const { children, container, layout, ...rest } = props; - - const chartApiRef = useRef({ - api() { - if (!this._api) { - this._api = createChart(container, { - ...rest, - layout, - width: container.clientWidth, - height: 300, - }); - this._api.timeScale().fitContent(); - } - return this._api; - }, - free() { - if (this._api) { - this._api.remove(); - } - }, - }); - - useLayoutEffect(() => { - const currentRef = chartApiRef.current; - const chart = currentRef.api(); - - const handleResize = () => { - chart.applyOptions({ - ...rest, - width: container.clientWidth, - }); - }; - - window.addEventListener("resize", handleResize); - return () => { - window.removeEventListener("resize", handleResize); - chart.remove(); - }; - }, []); - - useLayoutEffect(() => { - const currentRef = chartApiRef.current; - currentRef.api(); - }, []); - - useLayoutEffect(() => { - const currentRef = chartApiRef.current; - currentRef.api().applyOptions(rest); - }, []); - - useImperativeHandle(ref, () => chartApiRef.current.api(), []); - - useEffect(() => { - const currentRef = chartApiRef.current; - currentRef.api().applyOptions({ layout }); - }, [layout]); - - return ( - - {props.children} - - ); -}); -ChartContainer.displayName = "ChartContainer"; - -export const Series = forwardRef((props, ref) => { - const parent = useContext(Context); - const context = useRef({ - api() { - if (!this._api) { - const { children, data, type, ...rest } = props; - this._api = - type === "line" - ? parent.api().addLineSeries(rest) - : parent.api().addAreaSeries(rest); - this._api.setData(data); - } - return this._api; - }, - free() { - if (this._api) { - parent.free(); - } - }, - }); - - useLayoutEffect(() => { - const currentRef = context.current; - currentRef.api(); - - return () => currentRef.free(); - }, []); - - useLayoutEffect(() => { - const currentRef = context.current; - const { children, data, ...rest } = props; - currentRef.api().applyOptions(rest); - }); - - useImperativeHandle(ref, () => context.current.api(), []); - - return ( - - {props.children} - - ); -}); -Series.displayName = "Series"; diff --git a/src/components/charts/PortfolioChart.tsx b/src/components/charts/Chart.tsx similarity index 95% rename from src/components/charts/PortfolioChart.tsx rename to src/components/charts/Chart.tsx index 3c8ac14..61bb88f 100644 --- a/src/components/charts/PortfolioChart.tsx +++ b/src/components/charts/Chart.tsx @@ -4,7 +4,7 @@ import { createChart, ColorType } from "lightweight-charts"; import React, { MutableRefObject, useEffect, useRef } from "react"; import { indexData, portfolioData } from "@/app/MockData/MockData"; -const PortfolioChart = (props: any) => { +const Chart = (props: any) => { const { data, colors: { @@ -67,4 +67,4 @@ const PortfolioChart = (props: any) => { ); }; -export default PortfolioChart; +export default Chart; diff --git a/src/components/charts/TestChart.tsx b/src/components/charts/TestChart.tsx new file mode 100644 index 0000000..61bb88f --- /dev/null +++ b/src/components/charts/TestChart.tsx @@ -0,0 +1,70 @@ +"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; diff --git a/src/components/modules/HomepageModules.tsx b/src/components/modules/HomepageModules.tsx index 12324a2..ba380b4 100644 --- a/src/components/modules/HomepageModules.tsx +++ b/src/components/modules/HomepageModules.tsx @@ -6,12 +6,13 @@ import { loserMovers, } from "@/app/MockData/MockData"; import HomepageModule from "./HomepageModuleCard"; -import PortfolioChart from "../charts/PortfolioChart"; +import PortfolioChart from "../charts/Chart"; import MarketHeatmap from "../charts/MarketHeatmap"; import { Button } from "../ui/button"; import { ArrowRight, Divide } from "lucide-react"; import { NewsTable } from "../charts/news/NewsTable"; import { newsColumns } from "../charts/news/newsColumns"; +import Chart from "../charts/Chart"; export const PortfolioModule = () => { return ( @@ -22,7 +23,7 @@ export const PortfolioModule = () => {
^10.0% $3,412.09
- +