customizable charts

This commit is contained in:
2024-03-04 23:12:59 -05:00
parent c9dde12f83
commit 6a83909fb1
3 changed files with 333 additions and 229 deletions
+89 -22
View File
@@ -1,20 +1,76 @@
"use client";
import { createChart, ColorType } from "lightweight-charts";
import {
createChart,
ColorType,
LineSeriesPartialOptions,
AreaSeriesPartialOptions,
BaselineSeriesPartialOptions,
CandlestickSeriesPartialOptions,
HistogramSeriesPartialOptions,
SingleValueData,
WhitespaceData,
LineData,
CandlestickData,
HistogramData,
} 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;
interface ChartProps {
colors?: {
backgroundColor?: string;
textColor?: string;
};
series: Array<
| LineSeriesPartialOptionsWithData
| AreaSeriesPartialOptionsWithData
| BaselineSeriesPartialOptionsWithData
| CandlestickSeriesPartialOptionsWithData
| HistogramSeriesPartialOptionsWithData
>;
}
interface LineChartWithData {
type: "line";
data: Array<LineData | WhitespaceData>;
}
interface AreaChartWithData {
type: "area";
data: Array<SingleValueData | WhitespaceData>;
}
interface BaselineChartWithData {
type: "baseline";
data: Array<SingleValueData | WhitespaceData>;
}
interface CandlestickChartWithData {
type: "candle";
data: Array<CandlestickData | WhitespaceData>;
}
interface HistogramChartWithData {
type: "histogram";
data: Array<HistogramData | WhitespaceData>;
}
export type LineSeriesPartialOptionsWithData = LineSeriesPartialOptions &
LineChartWithData;
export type AreaSeriesPartialOptionsWithData = AreaSeriesPartialOptions &
AreaChartWithData;
export type BaselineSeriesPartialOptionsWithData =
BaselineSeriesPartialOptions & BaselineChartWithData;
export type CandlestickSeriesPartialOptionsWithData =
CandlestickSeriesPartialOptions & CandlestickChartWithData;
export type HistogramSeriesPartialOptionsWithData =
HistogramSeriesPartialOptions & HistogramChartWithData;
const Chart = ({ colors, series }: ChartProps) => {
const chartContainerRef = useRef() as MutableRefObject<HTMLDivElement>;
useEffect(() => {
@@ -26,8 +82,11 @@ const Chart = (props: any) => {
const chart = createChart(chartContainerRef.current, {
layout: {
background: { type: ColorType.Solid, color: backgroundColor },
textColor,
background: {
type: ColorType.Solid,
color: colors?.backgroundColor,
},
textColor: colors?.textColor,
},
width: chartContainerRef.current.clientWidth * 0.95,
height: chartContainerRef.current.clientHeight * 0.95,
@@ -39,15 +98,23 @@ const Chart = (props: any) => {
});
chart.timeScale().fitContent();
const portfolioSeries = chart.addLineSeries({
color: portfolioLineColor,
});
portfolioSeries.setData(portfolioData);
series.forEach((seriesItem) => {
switch (seriesItem.type) {
case "line":
const lineChart = chart.addLineSeries({
color: seriesItem.color,
});
const indexSeries = chart.addLineSeries({
color: indexLineColor,
lineChart.setData(seriesItem.data);
case "area":
case "baseline":
case "candle":
case "histogram":
}
});
indexSeries.setData(indexData);
window.addEventListener("resize", handleResize);
@@ -56,7 +123,7 @@ const Chart = (props: any) => {
chart.remove();
};
}, [data, backgroundColor, portfolioLineColor, indexLineColor, textColor]);
});
return (
<div
+42 -5
View File
@@ -2,19 +2,34 @@ import {
heatmapData,
newsData,
portfolioData,
indexData,
winnerMovers,
loserMovers,
} from "@/app/MockData/MockData";
import HomepageModule from "./HomepageModuleCard";
import PortfolioChart from "../charts/Chart";
import PortfolioChart, {
LineSeriesPartialOptionsWithData,
} from "../charts/Chart";
import MarketHeatmap from "../charts/MarketHeatmap";
import { Button } from "../ui/button";
import { ArrowRight, Divide } from "lucide-react";
import { ArrowRight } from "lucide-react";
import { NewsTable } from "../charts/news/NewsTable";
import { newsColumns } from "../charts/news/newsColumns";
import Chart from "../charts/Chart";
export const PortfolioModule = () => {
const portfolioLine: LineSeriesPartialOptionsWithData = {
type: "line",
data: portfolioData,
color: "white",
};
const indexLine: LineSeriesPartialOptionsWithData = {
type: "line",
data: indexData,
color: "yellow",
};
return (
<HomepageModule title="Portfolio">
<div className="flex justify-between px-4">
@@ -23,7 +38,13 @@ export const PortfolioModule = () => {
<div className="text-green-500">^10.0% $3,412.09</div>
</div>
<div className="h-3/4">
<Chart data={portfolioData} />
<Chart
series={[portfolioLine, indexLine]}
colors={{
backgroundColor: "transparent",
textColor: "white",
}}
/>
</div>
<div className="flex justify-between">
<Button className="text-white" variant="link">
@@ -99,12 +120,28 @@ export const MoversModule = () => {
};
export const MarketsModule = () => {
// const chartSeries: chartSeries[] = [
// {
// options: {color: "yellow"},
// type: "line",
// data: portfolioData,
// },
// {
// type: "line",
// data:
// }
// ];
return (
<HomepageModule title="Markets">
<div className="w-full">
<div className="w-full h-full">
<div className="w-1/2"></div>
<div className="w-1/2 border-blue-200 border">
<Chart />
{/* <Chart
series={chartSeries}
backgroundColor={"transparent"}
textColor={"white"}
/> */}
</div>
</div>
</HomepageModule>