Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chart fix #245

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/app/src/app/resources/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,14 @@ const MarketContent = ({ params }: { params: { id: string } }) => {
}}
activeWindow={TimeWindow.D}
isLoading={isResourcePricesLoading}
seriesVisibility={seriesVisibility}
seriesVisibility={{
candles: false,
index: false,
resource: true,
}}
toggleSeries={toggleSeries}
filterResourcePrices={false}
resourceOnly={true}
/>
</div>
</div>
Expand Down
116 changes: 81 additions & 35 deletions packages/app/src/lib/components/chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ interface Props {
resource: boolean;
};
toggleSeries: (series: 'candles' | 'index' | 'resource') => void;
filterResourcePrices?: boolean;
resourceOnly?: boolean;
}

interface IndexPrice {
Expand All @@ -36,12 +38,33 @@ interface ResourcePricePoint {
price: number;
}

const findClosestResourcePrice = (
targetTimestamp: number,
resourcePrices: ResourcePricePoint[],
shouldFilter: boolean
) => {
if (!resourcePrices?.length) return 0;
if (!shouldFilter) return resourcePrices.find(p => p.timestamp === targetTimestamp)?.price || 0;

const targetInSeconds = Math.floor(targetTimestamp / 1000);
const closest = resourcePrices.reduce((prev, curr) => {
const prevDiff = Math.abs((prev.timestamp / 1000) - targetInSeconds);
const currDiff = Math.abs((curr.timestamp / 1000) - targetInSeconds);
return currDiff < prevDiff ? curr : prev;
});

const timeDiff = Math.abs((closest.timestamp / 1000) - targetInSeconds);
return timeDiff <= 6 ? closest.price : 0;
};

const CandlestickChart: React.FC<Props> = ({
data,
activeWindow,
isLoading,
seriesVisibility,
toggleSeries,
filterResourcePrices = true,
resourceOnly = false,
}) => {
const chartContainerRef = useRef<HTMLDivElement>(null);
const chartRef = useRef<any>(null);
Expand Down Expand Up @@ -71,16 +94,10 @@ const CandlestickChart: React.FC<Props> = ({
},
grid: {
vertLines: {
color:
theme === 'dark'
? 'rgba(197, 203, 206, 0.2)'
: 'rgba(197, 203, 206, 0.5)',
color: theme === 'dark' ? 'rgba(197, 203, 206, 0.2)' : 'rgba(197, 203, 206, 0.5)',
},
horzLines: {
color:
theme === 'dark'
? 'rgba(197, 203, 206, 0.2)'
: 'rgba(197, 203, 206, 0.5)',
color: theme === 'dark' ? 'rgba(197, 203, 206, 0.2)' : 'rgba(197, 203, 206, 0.5)',
},
},
crosshair: {
Expand All @@ -90,28 +107,32 @@ const CandlestickChart: React.FC<Props> = ({
borderColor: theme === 'dark' ? '#363537' : '#cccccc',
timeVisible: true,
secondsVisible: false,
minBarSpacing: 0.001,
minBarSpacing: 2,
rightOffset: 5,
fixLeftEdge: true,
fixRightEdge: true,
},
});

chartRef.current = chart;

candlestickSeriesRef.current = chart.addCandlestickSeries({
upColor: '#26a69a',
downColor: '#ef5350',
borderVisible: false,
wickUpColor: '#26a69a',
wickDownColor: '#ef5350',
});
if (!resourceOnly) {
candlestickSeriesRef.current = chart.addCandlestickSeries({
upColor: '#26a69a',
downColor: '#ef5350',
borderVisible: false,
wickUpColor: '#26a69a',
wickDownColor: '#ef5350',
});

indexPriceSeriesRef.current = chart.addAreaSeries({
lineColor: 'blue',
topColor: 'rgba(128, 128, 128, 0.4)',
bottomColor: 'rgba(128, 128, 128, 0.0)',
lineStyle: 2,
});
indexPriceSeriesRef.current = chart.addAreaSeries({
lineColor: 'blue',
topColor: 'rgba(128, 128, 128, 0.4)',
bottomColor: 'rgba(128, 128, 128, 0.0)',
lineStyle: 2,
});
}

// Create resource price series regardless of initial data
resourcePriceSeriesRef.current = chart.addLineSeries({
color: '#4CAF50',
lineWidth: 2,
Expand Down Expand Up @@ -144,16 +165,36 @@ const CandlestickChart: React.FC<Props> = ({
chartRef.current = null;
}
};
}, [theme]); // Only recreate chart when theme changes
}, [theme, resourceOnly]);

// Separate effect for updating data
useEffect(() => {
if (!chartRef.current || !candlestickSeriesRef.current) return;
if (!chartRef.current) return;

if (resourceOnly) {
if (data.resourcePrices?.length && resourcePriceSeriesRef.current) {
const resourceLineData = data.resourcePrices.map((p) => ({
time: (p.timestamp / 1000) as UTCTimestamp,
value: p.price,
}));
resourcePriceSeriesRef.current.setData(resourceLineData);
resourcePriceSeriesRef.current.applyOptions({
visible: seriesVisibility.resource,
});


chartRef.current.timeScale().fitContent();
}
return;
}

const combinedData = data.marketPrices
.map((mp, i) => {
const timestamp = (mp.endTimestamp / 1000) as UTCTimestamp;
const indexPrice = data.indexPrices[i]?.price || 0;
const resourcePrice = filterResourcePrices
? findClosestResourcePrice(mp.endTimestamp, data.resourcePrices || [], true)
: data.resourcePrices?.find(p => p.timestamp === mp.endTimestamp)?.price || 0;
const adjustedPrice = isLoading ? 0 : indexPrice / (stEthPerToken || 1);

if (!mp.open || !mp.high || !mp.low || !mp.close) {
Expand All @@ -179,30 +220,30 @@ const CandlestickChart: React.FC<Props> = ({
? Number(mp.close)
: Number(convertGgasPerWstEthToGwei(mp.close, stEthPerToken)),
};

const lineData: LineData = {
time: timestamp,
value: displayPriceValue || 0,
};

return { candleData, lineData };
const resourceData: LineData = {
time: timestamp,
value: resourcePrice,
};

return { candleData, lineData, resourceData };
})
.filter((item): item is NonNullable<typeof item> => item !== null);

const candleSeriesData = combinedData.map((d) => d.candleData);
const lineSeriesData = combinedData.map((d) => d.lineData);
const resourceSeriesData = combinedData.map((d) => d.resourceData);

candlestickSeriesRef.current.setData(candleSeriesData);

if (!isLoading && indexPriceSeriesRef.current) {
indexPriceSeriesRef.current.setData(lineSeriesData);

if (data.resourcePrices?.length && resourcePriceSeriesRef.current) {
const resourceLineData = data.resourcePrices.map((p) => ({
time: (p.timestamp / 1000) as UTCTimestamp,
value: p.price,
}));
resourcePriceSeriesRef.current.setData(resourceLineData);
if (resourcePriceSeriesRef.current) {
resourcePriceSeriesRef.current.setData(resourceSeriesData);
}
}

Expand All @@ -222,7 +263,12 @@ const CandlestickChart: React.FC<Props> = ({
visible: seriesVisibility.resource,
});
}
}, [data, isLoading, stEthPerToken, useMarketUnits, seriesVisibility]);

if (chartRef.current) {
chartRef.current.timeScale().resetTimeScale();
chartRef.current.timeScale().fitContent();
}
}, [data, isLoading, stEthPerToken, useMarketUnits, seriesVisibility, filterResourcePrices, resourceOnly, activeWindow]);

return (
<div className="flex flex-col flex-1">
Expand Down