bugfixing latest change percentage

This commit is contained in:
MuslemRahimi 2024-11-05 18:51:38 +01:00
parent 71eaa7b02e
commit f8b27e1c1c

View File

@ -42,7 +42,6 @@
let stockDeck = {}; let stockDeck = {};
$: previousClose = data?.getStockQuote?.previousClose; $: previousClose = data?.getStockQuote?.previousClose;
let latestChangePercentage = 0;
//============================================// //============================================//
const intervals = ["1D", "1W", "1M", "1Y", "MAX"]; const intervals = ["1D", "1W", "1M", "1Y", "MAX"];
@ -60,8 +59,9 @@
$: { $: {
if (output !== null) { if (output !== null) {
//Bug value is NaN
let change; let change;
let formattedDate;
let safeFormattedDate;
if (displayData === "1D") { if (displayData === "1D") {
const length = oneDayPrice?.length; const length = oneDayPrice?.length;
@ -73,29 +73,23 @@
} }
} }
//currentDataRow = oneWeekPrice.slice(-1)[0] if ($realtimePrice !== null) {
if (!$isCrosshairMoveActive && $realtimePrice !== null) { change = (($realtimePrice / previousClose - 1) * 100).toFixed(2);
change = (($realtimePrice / previousClose - 1) * 100)?.toFixed(2);
} else { } else {
latestChangePercentage = (
((currentDataRow?.close ?? currentDataRow?.value) / previousClose -
1) *
100
)?.toFixed(2);
change = change =
displayData === "1D" displayData === "1D"
? latestChangePercentage ? ((currentDataRow?.close ?? currentDataRow?.value) /
: ( previousClose -
((currentDataRow?.close ?? currentDataRow?.value) / 1) *
displayLastLogicalRangeValue - 100
1) * : ((currentDataRow?.close ?? currentDataRow?.value) /
100 displayLastLogicalRangeValue -
)?.toFixed(2); 1) *
100;
change = change.toFixed(2);
} }
const date = new Date(currentDataRow?.time * 1000); const date = new Date(currentDataRow?.time * 1000);
const options = { const options = {
day: "2-digit", day: "2-digit",
month: "short", month: "short",
@ -105,8 +99,7 @@
timeZone: "UTC", timeZone: "UTC",
}; };
//const formattedDate = (displayData === '1D' || displayData === '1W' || displayData === '1M') ? date.toLocaleString('en-GB', options).replace(/\//g, '.') : date.toLocaleDateString('en-GB', { day: '2-digit', month: '2-digit', year: 'numeric' }).replace(/\//g, '.'); formattedDate =
const formattedDate =
displayData === "1D" || displayData === "1W" || displayData === "1M" displayData === "1D" || displayData === "1W" || displayData === "1M"
? date.toLocaleString("en-US", options) ? date.toLocaleString("en-US", options)
: date.toLocaleDateString("en-US", { : date.toLocaleDateString("en-US", {
@ -115,10 +108,11 @@
year: "numeric", year: "numeric",
}); });
const safeFormattedDate = safeFormattedDate =
formattedDate === "Invalid Date" formattedDate === "Invalid Date"
? convertTimestamp(data?.getStockQuote?.timestamp) ? convertTimestamp(data?.getStockQuote?.timestamp)
: formattedDate; : formattedDate;
displayLegend = { displayLegend = {
close: close:
currentDataRow?.value === "-" && currentDataRow?.close === undefined currentDataRow?.value === "-" && currentDataRow?.close === undefined
@ -625,7 +619,7 @@
high, high,
low, low,
close, close,
volume volume,
}), }),
); );
@ -813,10 +807,10 @@
<div class="flex flex-row items-center w-full"> <div class="flex flex-row items-center w-full">
<span <span
class="items-center justify-start {latestChangePercentage > 0 class="items-center justify-start {displayLegend?.change > 0
? "before:content-['+'] text-[#00FC50]" ? "before:content-['+'] text-[#00FC50]"
: 'text-[#FF2F1F]'} font-medium text-xs sm:text-sm" : 'text-[#FF2F1F]'} font-medium text-xs sm:text-sm"
>{latestChangePercentage}%</span >{displayLegend?.change ?? "-"}%</span
> >
<span class="ml-3 text-white text-xs sm:text-sm" <span class="ml-3 text-white text-xs sm:text-sm"