update ftd chart

This commit is contained in:
MuslemRahimi 2025-02-25 10:38:22 +01:00
parent 0f2c4d22f4
commit 3b705842a1
2 changed files with 138 additions and 155 deletions

View File

@ -17,7 +17,6 @@
let config = null; let config = null;
let isLoaded = false;
let avgFailToDeliver; let avgFailToDeliver;
let weightedFTD; let weightedFTD;
@ -85,7 +84,7 @@
rawData?.sort((a, b) => new Date(a?.date) - new Date(b?.date)); rawData?.sort((a, b) => new Date(a?.date) - new Date(b?.date));
rawData?.forEach((item) => { rawData?.forEach((item) => {
dates?.push(item?.date); dates?.push(item?.date);
priceList?.push(item?.price); priceList?.push(Number(item?.price));
failToDeliverList?.push(item?.failToDeliver); failToDeliverList?.push(item?.failToDeliver);
}); });
@ -150,7 +149,6 @@
})}</span> <br> <span class="text-black font-normal text-sm">${abbreviateNumber(this.y)}</span>`; })}</span> <br> <span class="text-black font-normal text-sm">${abbreviateNumber(this.y)}</span>`;
}, },
}, },
xAxis: { xAxis: {
categories: dates, categories: dates,
labels: { labels: {
@ -167,12 +165,15 @@
}, },
yAxis: [ yAxis: [
{ {
gridLineWidth: 1,
gridLineColor: "#111827",
labels: { labels: {
enabled: false, enabled: false,
}, },
title: { title: {
text: null, text: null,
}, },
opposite: true,
}, },
{ {
gridLineWidth: 1, gridLineWidth: 1,
@ -219,7 +220,6 @@
$: { $: {
const ticker = $assetType === "stock" ? $stockTicker : $etfTicker; const ticker = $assetType === "stock" ? $stockTicker : $etfTicker;
if (ticker) { if (ticker) {
isLoaded = false;
if (rawData?.length > 0) { if (rawData?.length > 0) {
weightedFTD = ( weightedFTD = (
(rawData?.slice(-1)?.at(0)?.failToDeliver / (rawData?.slice(-1)?.at(0)?.failToDeliver /
@ -228,7 +228,6 @@
)?.toFixed(2); )?.toFixed(2);
config = getPlotOptions(); config = getPlotOptions();
} }
isLoaded = true;
} }
} }
</script> </script>
@ -239,166 +238,150 @@
<h1 class="text-xl sm:text-2xl text-white font-bold">FTD Chart</h1> <h1 class="text-xl sm:text-2xl text-white font-bold">FTD Chart</h1>
</div> </div>
{#if isLoaded} {#if rawData?.length !== 0}
{#if rawData?.length !== 0} <div class="w-full flex flex-col items-start">
<div class="w-full flex flex-col items-start"> <div class="text-white text-[1rem] mt-2 mb-2 w-full">
<div class="text-white text-[1rem] mt-2 mb-2 w-full"> Over the past year, {$displayCompanyName} has seen a monthly average of
Over the past year, {$displayCompanyName} has seen a monthly average <span class="font-semibold"
of >{avgFailToDeliver?.toLocaleString("en-US")}
<span class="font-semibold" </span> fail to deliver shares.
>{avgFailToDeliver?.toLocaleString("en-US")}
</span> fail to deliver shares.
</div>
</div> </div>
</div>
<div
class="chart mt-5 sm:mt-0 border border-gray-800 rounded"
use:highcharts={config}
></div>
<div
class="mt-5 flex flex-col sm:flex-row items-start sm:items-center w-full justify-between sm:border-y border-gray-800 sm:pt-2 sm:pb-2"
>
<h3 class="text-xl sm:text-2xl text-white font-bold mb-2 sm:mb-0">
FTD History
</h3>
<div <div
class="chart mt-5 sm:mt-0 border border-gray-800 rounded" class="inline-flex justify-center w-full rounded-md sm:w-auto sm:ml-auto"
use:highcharts={config}
></div>
<div
class="mt-5 flex flex-col sm:flex-row items-start sm:items-center w-full justify-between sm:border-y border-gray-800 sm:pt-2 sm:pb-2"
> >
<h3 class="text-xl sm:text-2xl text-white font-bold mb-2 sm:mb-0">
FTD History
</h3>
<div <div
class="inline-flex justify-center w-full rounded-md sm:w-auto sm:ml-auto" class="bg-secondary w-full min-w-24 sm:w-fit relative flex flex-wrap items-center justify-center rounded-md p-1"
> >
<div {#each tabs as item, i}
class="bg-secondary w-full min-w-24 sm:w-fit relative flex flex-wrap items-center justify-center rounded-md p-1" {#if data?.user?.tier !== "Pro" && i > 0}
> <button
{#each tabs as item, i} on:click={() => goto("/pricing")}
{#if data?.user?.tier !== "Pro" && i > 0} class="group relative z-[1] rounded-full w-1/2 min-w-24 md:w-auto px-5 py-1"
<button >
on:click={() => goto("/pricing")} <span class="relative text-sm block font-semibold">
class="group relative z-[1] rounded-full w-1/2 min-w-24 md:w-auto px-5 py-1" {item.title}
> <svg
<span class="relative text-sm block font-semibold"> class="inline-block ml-0.5 -mt-1 w-3.5 h-3.5"
{item.title} xmlns="http://www.w3.org/2000/svg"
<svg viewBox="0 0 24 24"
class="inline-block ml-0.5 -mt-1 w-3.5 h-3.5" ><path
xmlns="http://www.w3.org/2000/svg" fill="#A3A3A3"
viewBox="0 0 24 24" d="M17 9V7c0-2.8-2.2-5-5-5S7 4.2 7 7v2c-1.7 0-3 1.3-3 3v7c0 1.7 1.3 3 3 3h10c1.7 0 3-1.3 3-3v-7c0-1.7-1.3-3-3-3M9 7c0-1.7 1.3-3 3-3s3 1.3 3 3v2H9z"
><path /></svg
fill="#A3A3A3"
d="M17 9V7c0-2.8-2.2-5-5-5S7 4.2 7 7v2c-1.7 0-3 1.3-3 3v7c0 1.7 1.3 3 3 3h10c1.7 0 3-1.3 3-3v-7c0-1.7-1.3-3-3-3M9 7c0-1.7 1.3-3 3-3s3 1.3 3 3v2H9z"
/></svg
>
</span>
</button>
{:else}
<button
on:click={() => changeTimePeriod(i)}
class="group relative z-[1] rounded-full w-1/2 min-w-24 md:w-auto px-5 py-1 {activeIdx ===
i
? 'z-0'
: ''} "
>
{#if activeIdx === i}
<div class="absolute inset-0 rounded-md bg-[#fff]"></div>
{/if}
<span
class="relative text-sm block font-semibold whitespace-nowrap {activeIdx ===
i
? 'text-black'
: 'text-white'}"
> >
{item.title} </span>
</span> </button>
</button> {:else}
{/if} <button
{/each} on:click={() => changeTimePeriod(i)}
</div> class="group relative z-[1] rounded-full w-1/2 min-w-24 md:w-auto px-5 py-1 {activeIdx ===
i
? 'z-0'
: ''} "
>
{#if activeIdx === i}
<div class="absolute inset-0 rounded-md bg-[#fff]"></div>
{/if}
<span
class="relative text-sm block font-semibold whitespace-nowrap {activeIdx ===
i
? 'text-black'
: 'text-white'}"
>
{item.title}
</span>
</button>
{/if}
{/each}
</div> </div>
</div> </div>
</div>
<div class="w-full overflow-x-scroll"> <div class="w-full overflow-x-scroll">
<table <table
class="table table-sm table-compact bg-table border border-gray-800 rounded-none sm:rounded-md w-full m-auto mt-4" class="table table-sm table-compact bg-table border border-gray-800 rounded-none sm:rounded-md w-full m-auto mt-4"
> >
<thead class="bg-default"> <thead class="bg-default">
<tr> <tr>
<th class="text-white font-semibold text-start text-sm">Date</th <th class="text-white font-semibold text-start text-sm">Date</th>
<th class="text-white font-semibold text-end text-sm">Price</th>
<th class="text-white font-semibold text-end text-sm"
>FTD Shares</th
>
<th class="text-white font-semibold text-end text-sm">% Change</th
>
</tr>
</thead>
<tbody>
{#each tableList as item, index}
<!-- row -->
<tr
class="sm:hover:bg-[#245073] sm:hover:bg-opacity-[0.2] odd:bg-odd border-b border-gray-800"
>
<td
class="text-white font-medium text-sm sm:text-[1rem] whitespace-nowrap"
> >
<th class="text-white font-semibold text-end text-sm">Price</th> {item?.date}
<th class="text-white font-semibold text-end text-sm" </td>
>FTD Shares</th
>
<th class="text-white font-semibold text-end text-sm"
>% Change</th
>
</tr>
</thead>
<tbody>
{#each tableList as item, index}
<!-- row -->
<tr
class="sm:hover:bg-[#245073] sm:hover:bg-opacity-[0.2] odd:bg-odd border-b border-gray-800"
>
<td
class="text-white font-medium text-sm sm:text-[1rem] whitespace-nowrap"
>
{item?.date}
</td>
<td <td
class="text-white text-sm sm:text-[1rem] text-right whitespace-nowrap" class="text-white text-sm sm:text-[1rem] text-right whitespace-nowrap"
> >
{item?.price} {item?.price}
</td> </td>
<td <td
class="text-white text-sm sm:text-[1rem] text-right whitespace-nowrap" class="text-white text-sm sm:text-[1rem] text-right whitespace-nowrap"
> >
{abbreviateNumber(item?.failToDeliver)} {abbreviateNumber(item?.failToDeliver)}
</td> </td>
<td <td
class="text-white text-sm sm:text-[1rem] whitespace-nowrap font-medium text-end" class="text-white text-sm sm:text-[1rem] whitespace-nowrap font-medium text-end"
> >
{#if index === tableList?.length - 1} {#if index === tableList?.length - 1}
n/a n/a
{:else if item?.failToDeliver > tableList[index + 1]?.failToDeliver} {:else if item?.failToDeliver > tableList[index + 1]?.failToDeliver}
<span class="text-[#00FC50]"> <span class="text-[#00FC50]">
+{( +{(
((item?.failToDeliver - ((item?.failToDeliver -
tableList[index + 1]?.failToDeliver) /
tableList[index + 1]?.failToDeliver) *
100
)?.toFixed(2)}%
</span>
{:else if item?.failToDeliver < tableList[index + 1]?.failToDeliver}
<span class="text-[#FF2F1F]">
-{(
Math.abs(
(item?.failToDeliver -
tableList[index + 1]?.failToDeliver) / tableList[index + 1]?.failToDeliver) /
tableList[index + 1]?.failToDeliver) * tableList[index + 1]?.failToDeliver,
100 ) * 100
)?.toFixed(2)}% )?.toFixed(2)}%
</span> </span>
{:else if item?.failToDeliver < tableList[index + 1]?.failToDeliver} {:else}
<span class="text-[#FF2F1F]"> n/a
-{( {/if}
Math.abs( </td>
(item?.failToDeliver - </tr>
tableList[index + 1]?.failToDeliver) / {/each}
tableList[index + 1]?.failToDeliver, </tbody>
) * 100 </table>
)?.toFixed(2)}%
</span>
{:else}
n/a
{/if}
</td>
</tr>
{/each}
</tbody>
</table>
</div>
{/if}
{:else}
<div class="flex justify-center items-center h-80">
<div class="relative">
<label
class="bg-secondary rounded-md h-14 w-14 flex justify-center items-center absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2"
>
<span class="loading loading-spinner loading-md text-gray-400"
></span>
</label>
</div>
</div> </div>
{/if} {/if}
</main> </main>

View File

@ -19,19 +19,19 @@
: "n/a"; : "n/a";
function computeYearOverYearChange(rawData) { function computeYearOverYearChange(rawData) {
if (rawData.length < 2) { if (rawData?.length < 2) {
return null; // Not enough rawData to compute change return null; // Not enough rawData to compute change
} }
// Step 1: Get the last entry in the list // Step 1: Get the last entry in the list
const lastEntry = rawData[rawData.length - 1]; const lastEntry = rawData[rawData?.length - 1];
const lastDate = new Date(lastEntry.date); const lastDate = new Date(lastEntry?.date);
const lastValue = rawData?.slice(-1)?.at(0).failToDeliver; const lastValue = rawData?.slice(-1)?.at(0)?.failToDeliver;
// Step 2: Find the entry closest to one year before the last date // Step 2: Find the entry closest to one year before the last date
let closestEntry = null; let closestEntry = null;
for (let i = rawData.length - 2; i >= 0; i--) { for (let i = rawData.length - 2; i >= 0; i--) {
const entryDate = new Date(rawData[i].date); const entryDate = new Date(rawData[i]?.date);
const oneYearAgo = new Date(lastDate); const oneYearAgo = new Date(lastDate);
oneYearAgo.setFullYear(lastDate.getFullYear() - 1); oneYearAgo.setFullYear(lastDate.getFullYear() - 1);