mirror of
https://github.com/dylanbenzi/yellowbird.git
synced 2026-11-27 01:07:01 +00:00
media inq, advanced react charting
This commit is contained in:
@@ -0,0 +1,210 @@
|
||||
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";
|
||||
@@ -60,7 +60,7 @@ const PortfolioChart = (props: any) => {
|
||||
|
||||
return (
|
||||
<div
|
||||
className="flex justify-center h-3/4"
|
||||
className="flex justify-center h-full"
|
||||
ref={chartContainerRef}
|
||||
id="chart"
|
||||
/>
|
||||
|
||||
@@ -58,7 +58,7 @@ export function NewsTable<TData, TValue>({
|
||||
<TableRow
|
||||
key={row.id}
|
||||
data-state={row.getIsSelected() && "selected"}
|
||||
className="text-white"
|
||||
className="text-sm text-white 2xl:text-base"
|
||||
>
|
||||
{row.getVisibleCells().map((cell) => (
|
||||
<TableCell key={cell.id} className="p-1 pl-4 overflow-hidden">
|
||||
|
||||
@@ -11,7 +11,7 @@ export const newsColumns: ColumnDef<newsDataType>[] = [
|
||||
const headline: string = row.getValue("headline");
|
||||
|
||||
return (
|
||||
<div className="max-w-[450px] whitespace-nowrap overflow-hidden text-ellipsis">
|
||||
<div className="max-w-[250px] whitespace-nowrap overflow-hidden text-ellipsis 2xl:max-w-[450px]">
|
||||
{headline}
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -9,7 +9,7 @@ interface HomepageModuleProps {
|
||||
|
||||
const HomepageModule = ({ title, children }: HomepageModuleProps) => {
|
||||
return (
|
||||
<div className="flex flex-col h-full bg-[rgb(19,20,25)] rounded-lg">
|
||||
<div className="flex flex-col h-80 bg-[rgb(19,20,25)] rounded-lg 2xl:h-full">
|
||||
<div className="flex justify-between px-2 py-2 pb-0">
|
||||
<div className="flex items-center text-lg px-2 font-semibold">
|
||||
{title}
|
||||
|
||||
@@ -21,7 +21,9 @@ export const PortfolioModule = () => {
|
||||
<div className="rounded-full bg-neutral-900 px-4">2W 1M YTD</div>
|
||||
<div className="text-green-500">^10.0% $3,412.09</div>
|
||||
</div>
|
||||
<PortfolioChart data={portfolioData} />
|
||||
<div className="h-3/4">
|
||||
<PortfolioChart data={portfolioData} />
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<Button className="text-white" variant="link">
|
||||
Go to Portfolio
|
||||
@@ -51,39 +53,39 @@ export const HeatmapModule = () => {
|
||||
export const MoversModule = () => {
|
||||
return (
|
||||
<HomepageModule title="Movers">
|
||||
<div className="flex justify-between px-8">
|
||||
<div className="mt-6">
|
||||
<div className="flex justify-between 2xl:px-8">
|
||||
<div className="px-4 mt-4 2xl:mt-6">
|
||||
<div className="text-lg">Gainers</div>
|
||||
<div className="mt-4 grid grid-cols-2 grid-rows-5 gap-y-6 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">
|
||||
{winnerMovers.map((item) => (
|
||||
<div
|
||||
className="flex border-b border-neutral-400 pb-1"
|
||||
className="flex border-b border-neutral-400 pb-1 text-sm 2xl:text-base"
|
||||
key={item.symbol}
|
||||
>
|
||||
<div className="w-24 font-bold">{item.symbol}</div>
|
||||
<div className="w-20 text-right text-neutral-400">
|
||||
<div className="w-32 font-bold 2xl:w-24">{item.symbol}</div>
|
||||
<div className="text-xs text-right text-neutral-400 2xl:w-20">
|
||||
{item.stockPrice}
|
||||
</div>
|
||||
<div className="ml-4 rounded-lg bg-green-700 bg-opacity-90 w-16 text-center">
|
||||
<div className="text-xs ml-2 rounded-lg bg-green-700 bg-opacity-90 w-20 text-center 2xl:ml-4">
|
||||
+{item.percentChange}%
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-6">
|
||||
<div className="px-4 mt-4 2xl:mt-6">
|
||||
<div className="text-lg">Losers</div>
|
||||
<div className="mt-4 grid grid-cols-2 grid-rows-5 gap-y-6 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">
|
||||
{loserMovers.map((item) => (
|
||||
<div
|
||||
className="flex border-b border-neutral-400 pb-1"
|
||||
className="flex border-b border-neutral-400 pb-1 text-sm 2xl:text-base"
|
||||
key={item.symbol}
|
||||
>
|
||||
<div className="w-24 font-bold">{item.symbol}</div>
|
||||
<div className="w-20 text-right text-neutral-400">
|
||||
<div className="w-32 font-bold 2xl:w-24">{item.symbol}</div>
|
||||
<div className="text-xs text-right text-neutral-400 2xl:w-20">
|
||||
{item.stockPrice}
|
||||
</div>
|
||||
<div className="ml-4 rounded-lg bg-red-700 bg-opacity-90 w-16 text-center">
|
||||
<div className="text-xs ml-2 rounded-lg bg-red-700 bg-opacity-90 w-20 text-center 2xl:ml-4">
|
||||
{item.percentChange}%
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user