From 0e84fe09f836053dc6279699fc336d9c86d60ae0 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Tue, 3 Dec 2024 23:13:29 +0100 Subject: [PATCH] update politician page --- src/lib/components/RatingsChart.svelte | 19 +-- src/routes/analysts/[slug]/+page.svelte | 10 +- src/routes/politicians/[slug]/+page.svelte | 128 ++++++++++++++++++++- 3 files changed, 141 insertions(+), 16 deletions(-) diff --git a/src/lib/components/RatingsChart.svelte b/src/lib/components/RatingsChart.svelte index 8e96b65b..3ee9f306 100644 --- a/src/lib/components/RatingsChart.svelte +++ b/src/lib/components/RatingsChart.svelte @@ -21,6 +21,7 @@ export let symbol; export let ratingsList; export let numOfRatings = 0; + export let title = "Ratings"; let isLoaded = false; let optionsData = null; @@ -113,28 +114,31 @@ historicalData, timePeriod, ); - // Prepare markPoints for ratings const markPoints = ratingsList ?.filter((rating) => { // Ensure date format is correct and matches return dates.includes(rating?.date) && rating?.ticker === symbol; }) - .map((rating) => ({ + ?.map((rating) => ({ // Marker at the rating's date type: "max", // Marking the rating date - name: rating?.rating_current, + name: rating?.type, coord: [ - rating.date, - closeValues[dates.indexOf(rating.date)], // Find the close value corresponding to the rating date + rating?.date, + closeValues[dates?.indexOf(rating?.date)], // Find the close value corresponding to the rating date ], label: { - formatter: rating.rating_current + formatter: rating?.type //rating.rating_current + ?.replace("Bought", "Buy") + ?.replace("Sold", "Sell") ?.replace("Sector Perform", "Hold") ?.replace("Equal-Weight", "Hold") ?.replace("Market Perform", "Hold") ?.replace("Overweight", "Buy") + ?.replace("Market Outperform", "Buy") ?.replace("Outperform", "Buy") + ?.replace("Market Underperform", "Sell") ?.replace("Underperform", "Sell") ?.replace("Underweight", "Sell"), // Display the rating_current text position: "top", // Position the label above the point @@ -281,7 +285,8 @@

- {symbol} - {numOfRatings} Ratings + {symbol} - {numOfRatings} + {title}

diff --git a/src/routes/analysts/[slug]/+page.svelte b/src/routes/analysts/[slug]/+page.svelte index b05ff618..4becadf7 100644 --- a/src/routes/analysts/[slug]/+page.svelte +++ b/src/routes/analysts/[slug]/+page.svelte @@ -31,7 +31,7 @@ const tickerMap = new Map(); data.forEach((item) => { - const { ticker, date } = item; + const { ticker } = item; if (!ticker) return; // Skip if ticker is not defined @@ -573,8 +573,12 @@ style="position: relative; height: 0px; z-index: 1;" > ({ + ...item, + type: item?.rating_current, + }), + )} symbol={item?.ticker} numOfRatings={item?.ratings} /> diff --git a/src/routes/politicians/[slug]/+page.svelte b/src/routes/politicians/[slug]/+page.svelte index 318c2f05..65d44dae 100644 --- a/src/routes/politicians/[slug]/+page.svelte +++ b/src/routes/politicians/[slug]/+page.svelte @@ -2,13 +2,18 @@ import { numberOfUnreadNotification } from "$lib/store"; import { formatString, sectorNavigation } from "$lib/utils"; import HoverStockChart from "$lib/components/HoverStockChart.svelte"; + import RatingsChart from "$lib/components/RatingsChart.svelte"; export let data; let cloudFrontUrl = import.meta.env.VITE_IMAGE_URL; let rawData = data?.getPolitician?.output; - let name = rawData?.history?.at(0)?.representative ?? "n/a"; let numOfTrades = rawData?.history?.length; + + let tableData = + rawData?.history?.length > 0 ? processTickerData(rawData?.history) : []; + + let name = rawData?.history?.at(0)?.representative ?? "n/a"; let mainSectors = rawData?.mainSectors || []; let mainIndustries = rawData?.mainIndustries || []; @@ -53,6 +58,49 @@ } return 0; } + + function processTickerData(data) { + const tickerMap = new Map(); + + data.forEach((item) => { + const { ticker } = item; + + if (!ticker) return; // Skip if ticker is not defined + + if (!tickerMap.has(ticker)) { + // Add the item and initialize count + tickerMap.set(ticker, { ...item, transaction: 1 }); + } else { + const existing = tickerMap.get(ticker); + + // Increment the ratings count + existing.transaction += 1; + + // Keep the item with the latest date + if ( + new Date(item?.transactionDate) > new Date(existing?.transactionDate) + ) { + tickerMap.set(ticker, { + ...item, + transaction: existing?.transaction, + }); + } + } + }); + + // Convert the Map back to an array + return Array?.from(tickerMap?.values()); + } + + $: checkedSymbol = ""; + function openGraph(symbol) { + // Clear all existing symbols + if (checkedSymbol === symbol) { + checkedSymbol = ""; + } else { + checkedSymbol = symbol; + } + } @@ -159,7 +207,7 @@ }).format(totalAmountTraded)}
- Total Amount Traded + Total Amount
@@ -172,7 +220,7 @@ {numOfTrades?.toLocaleString("en-US")}
- Total Trades + Transaction
@@ -192,7 +240,7 @@ : "n/a"}
- Last Traded + Last Transaction
+ + @@ -279,7 +331,12 @@ - Traded + Transaction + + + Last Trade - {#each rawData?.history as item} + {#each tableData as item} + + @@ -330,6 +408,12 @@ {item?.amount} + + {item?.transaction} + @@ -358,6 +442,38 @@ )} + {#if checkedSymbol === (item?.ticker ?? item?.symbol)} +
+
+
+
+
+ ({ + ...item, + date: item.transactionDate, + }), + )} + symbol={item?.ticker ?? item?.symbol} + numOfRatings={item?.transaction} + /> +
+
+
+
+
+ + {/if} {/each}