removed adv chart

This commit is contained in:
2024-03-03 22:57:13 -05:00
parent 1bd646805c
commit c9dde12f83
4 changed files with 90 additions and 220 deletions
-210
View File
@@ -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 (
<>
<button type="button" onClick={() => setStarted((current) => !current)}>
{started ? "Stop updating" : "Start updating series"}
</button>
<Chart layout={chartLayoutOptions}>
<Series
ref={series1}
type={"line"}
data={initialData}
color={lineColor}
/>
</Chart>
</>
);
};
export function Chart(props: any) {
const [container, setContainer] = useState(false);
const handleRef = useCallback((ref) => setContainer(ref), []);
return (
<div ref={handleRef}>
{container && <ChartContainer {...props} container={container} />}
</div>
);
}
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 (
<Context.Provider value={chartApiRef.current}>
{props.children}
</Context.Provider>
);
});
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 (
<Context.Provider value={context.current}>
{props.children}
</Context.Provider>
);
});
Series.displayName = "Series";
@@ -4,7 +4,7 @@ import { createChart, ColorType } from "lightweight-charts";
import React, { MutableRefObject, useEffect, useRef } from "react"; import React, { MutableRefObject, useEffect, useRef } from "react";
import { indexData, portfolioData } from "@/app/MockData/MockData"; import { indexData, portfolioData } from "@/app/MockData/MockData";
const PortfolioChart = (props: any) => { const Chart = (props: any) => {
const { const {
data, data,
colors: { colors: {
@@ -67,4 +67,4 @@ const PortfolioChart = (props: any) => {
); );
}; };
export default PortfolioChart; export default Chart;
+70
View File
@@ -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<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;
+18 -8
View File
@@ -6,12 +6,13 @@ import {
loserMovers, loserMovers,
} from "@/app/MockData/MockData"; } from "@/app/MockData/MockData";
import HomepageModule from "./HomepageModuleCard"; import HomepageModule from "./HomepageModuleCard";
import PortfolioChart from "../charts/PortfolioChart"; import PortfolioChart from "../charts/Chart";
import MarketHeatmap from "../charts/MarketHeatmap"; import MarketHeatmap from "../charts/MarketHeatmap";
import { Button } from "../ui/button"; import { Button } from "../ui/button";
import { ArrowRight, Divide } from "lucide-react"; import { ArrowRight, Divide } from "lucide-react";
import { NewsTable } from "../charts/news/NewsTable"; import { NewsTable } from "../charts/news/NewsTable";
import { newsColumns } from "../charts/news/newsColumns"; import { newsColumns } from "../charts/news/newsColumns";
import Chart from "../charts/Chart";
export const PortfolioModule = () => { export const PortfolioModule = () => {
return ( return (
@@ -22,7 +23,7 @@ export const PortfolioModule = () => {
<div className="text-green-500">^10.0% $3,412.09</div> <div className="text-green-500">^10.0% $3,412.09</div>
</div> </div>
<div className="h-3/4"> <div className="h-3/4">
<PortfolioChart data={portfolioData} /> <Chart data={portfolioData} />
</div> </div>
<div className="flex justify-between"> <div className="flex justify-between">
<Button className="text-white" variant="link"> <Button className="text-white" variant="link">
@@ -53,7 +54,7 @@ export const HeatmapModule = () => {
export const MoversModule = () => { export const MoversModule = () => {
return ( return (
<HomepageModule title="Movers"> <HomepageModule title="Movers">
<div className="flex justify-between 2xl:px-8"> <div className="flex justify-between 2xl:px-2">
<div className="px-4 mt-4 2xl:mt-6"> <div className="px-4 mt-4 2xl:mt-6">
<div className="text-lg">Gainers</div> <div className="text-lg">Gainers</div>
<div className="mt-4 grid grid-cols-2 grid-rows-5 gap-y-4 gap-x-2 2xl:gap-y-6 2xl:gap-x-10"> <div className="mt-4 grid grid-cols-2 grid-rows-5 gap-y-4 gap-x-2 2xl:gap-y-6 2xl:gap-x-10">
@@ -63,10 +64,10 @@ export const MoversModule = () => {
key={item.symbol} key={item.symbol}
> >
<div className="w-32 font-bold 2xl:w-24">{item.symbol}</div> <div className="w-32 font-bold 2xl:w-24">{item.symbol}</div>
<div className="text-xs text-right text-neutral-400 2xl:w-20"> <div className="text-xs text-right text-neutral-400 2xl:w-20 2xl:text-base">
{item.stockPrice} {item.stockPrice}
</div> </div>
<div className="text-xs ml-2 rounded-lg bg-green-700 bg-opacity-90 w-20 text-center 2xl:ml-4"> <div className="text-xs ml-2 rounded-lg bg-green-700 bg-opacity-90 w-20 text-center 2xl:ml-4 2xl:text-base">
+{item.percentChange}% +{item.percentChange}%
</div> </div>
</div> </div>
@@ -82,10 +83,10 @@ export const MoversModule = () => {
key={item.symbol} key={item.symbol}
> >
<div className="w-32 font-bold 2xl:w-24">{item.symbol}</div> <div className="w-32 font-bold 2xl:w-24">{item.symbol}</div>
<div className="text-xs text-right text-neutral-400 2xl:w-20"> <div className="text-xs text-right text-neutral-400 2xl:w-20 2xl:text-base">
{item.stockPrice} {item.stockPrice}
</div> </div>
<div className="text-xs ml-2 rounded-lg bg-red-700 bg-opacity-90 w-20 text-center 2xl:ml-4"> <div className="text-xs ml-2 rounded-lg bg-red-700 bg-opacity-90 w-20 text-center 2xl:ml-4 2xl:text-base">
{item.percentChange}% {item.percentChange}%
</div> </div>
</div> </div>
@@ -98,7 +99,16 @@ export const MoversModule = () => {
}; };
export const MarketsModule = () => { export const MarketsModule = () => {
return <HomepageModule title="Markets"></HomepageModule>; return (
<HomepageModule title="Markets">
<div className="w-full">
<div className="w-1/2"></div>
<div className="w-1/2 border-blue-200 border">
<Chart />
</div>
</div>
</HomepageModule>
);
}; };
export const EarningsModule = () => { export const EarningsModule = () => {