bugfixing chart

This commit is contained in:
MuslemRahimi 2024-07-27 01:39:27 +02:00
parent e8b80ad436
commit d431683e8b
2 changed files with 249 additions and 302 deletions

View File

@ -1,107 +1,48 @@
let companyName; const cleanString = (input) => {
function cleanString(input) {
// Define a list of substrings to remove (case insensitive)
const substringsToRemove = [ const substringsToRemove = [
'Depositary', 'Depositary', 'Inc.', 'Incorporated', 'Holdings', 'Corporation', 'Corporations',
'Inc.', 'LLC', 'Holdings plc American Depositary Shares', 'Holding Corporation', 'Oyj',
'Incorporated', 'Company', 'The', 'plc',
'Holdings',
'Corporation',
'Corporations',
'LLC',
'Holdings plc American Depositary Shares',
'Holding Corporation',
'Oyj',
'Company',
'The',
'plc',
]; ];
// Create a regular expression pattern that matches any of the substrings surrounded by word boundaries
const pattern = new RegExp(`\\b(${substringsToRemove.join('|')})\\b|,`, 'gi'); const pattern = new RegExp(`\\b(${substringsToRemove.join('|')})\\b|,`, 'gi');
return input?.replace(pattern, '').trim();
// Use the replace method to remove the specified substrings and commas, then trim the result
return input?.replace(pattern, '')?.trim();
}
const fetchData = async (apiURL, apiKey, endpoint, ticker) => {
const postData = {
ticker: ticker
};
const response = await fetch(apiURL + endpoint, {
method: 'POST',
headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey
},
body: JSON.stringify(postData)
});
const output = await response.json();
if(endpoint === '/etf-profile')
{
companyName = cleanString(output?.at(0)?.name);
}
return output;
}; };
const fetchWatchlist = async (fastifyURL, userId) => { const fetchData = async (apiURL, apiKey, endpoint, ticker) => {
const response = await fetch(`${apiURL}${endpoint}`, {
const postData = {'userId': userId}
const response = await fetch(fastifyURL+'/all-watchlists', {
method: 'POST', method: 'POST',
headers: { headers: {
"Content-Type": "application/json" "Content-Type": "application/json",
"X-API-KEY": apiKey
}, },
body: JSON.stringify(postData) body: JSON.stringify({ ticker })
}); });
return response.json();
};
const fetchFromFastify = async (fastifyURL, endpoint, userId) => {
const output = (await response.json())?.items; const response = await fetch(`${fastifyURL}${endpoint}`, {
return output;
}
async function fetchPortfolio(fastifyURL, userId)
{
const postData = {'userId': userId};
const response = await fetch(fastifyURL+'/get-portfolio-data', {
method: 'POST', method: 'POST',
headers: { headers: { "Content-Type": "application/json" },
"Content-Type": "application/json" body: JSON.stringify({ userId })
},
body: JSON.stringify(postData)
}); });
const { items } = await response.json();
return items;
};
const output = (await response.json())?.items; export const load = async ({ params, locals, setHeaders }) => {
const { apiURL, fastifyURL, apiKey, wsURL, user } = locals;
const { tickerID } = params;
return output const endpoints = [
} '/etf-profile', '/similar-etfs', '/etf-country-weighting', '/etf-holdings',
'/stock-dividend', '/stock-quote', '/wiim', '/one-day-price'
];
export const load = async ({ params, locals, setHeaders}) => { const promises = [
...endpoints.map(endpoint => fetchData(apiURL, apiKey, endpoint, tickerID)),
let apiURL = locals?.apiURL; fetchFromFastify(fastifyURL, '/all-watchlists', user?.id),
let fastifyURL = locals?.fastifyURL; fetchFromFastify(fastifyURL, '/get-portfolio-data', user?.id)
let apiKey = locals?.apiKey;
let wsURL = locals?.wsURL;
const promises = [
fetchData(apiURL,apiKey, '/etf-profile', params.tickerID),
fetchData(apiURL,apiKey, '/similar-etfs', params.tickerID),
fetchData(apiURL,apiKey, '/etf-country-weighting', params.tickerID),
fetchData(apiURL,apiKey, '/etf-holdings', params.tickerID),
fetchData(apiURL,apiKey, '/stock-dividend',params.tickerID),
fetchData(apiURL,apiKey, '/stock-quote', params.tickerID),
fetchData(apiURL,apiKey, '/wiim',params.tickerID),
fetchData(apiURL,apiKey, '/one-day-price',params.tickerID),
fetchWatchlist(fastifyURL, locals?.user?.id),
fetchPortfolio(fastifyURL, locals?.user?.id)
]; ];
const [ const [
@ -117,11 +58,7 @@ const promises = [
getUserPortfolio, getUserPortfolio,
] = await Promise.all(promises); ] = await Promise.all(promises);
setHeaders({ 'cache-control': 'public, max-age=300' });
setHeaders({
'cache-control': 'public, max-age=300'
});
return { return {
getETFProfile, getETFProfile,
@ -134,9 +71,7 @@ const promises = [
getOneDayPrice, getOneDayPrice,
getUserWatchlist, getUserWatchlist,
getUserPortfolio, getUserPortfolio,
companyName, companyName: cleanString(getETFProfile?.[0]?.name),
wsURL, wsURL,
}; };
}; };

View File

@ -45,6 +45,10 @@
onMount(async() => { onMount(async() => {
if (chart) {
chart.timeScale().fitContent();
}
WIIM = (await import('$lib/components/WIIM.svelte')).default; WIIM = (await import('$lib/components/WIIM.svelte')).default;
StockSplits = (await import('$lib/components/StockSplits.svelte')).default; StockSplits = (await import('$lib/components/StockSplits.svelte')).default;
@ -899,6 +903,7 @@ function changeChartType() {
</h2> </h2>
{:else} {:else}
<div class="chart-container">
<Chart {...options} autoSize={true} ref={(ref) => chart = ref} on:crosshairMove={handleCrosshairMove} > <Chart {...options} autoSize={true} ref={(ref) => chart = ref} on:crosshairMove={handleCrosshairMove} >
{#if displayData === '1D'} {#if displayData === '1D'}
@ -1118,6 +1123,7 @@ function changeChartType() {
{/if} {/if}
</Chart> </Chart>
</div>
{/if} {/if}
</div> </div>
@ -1474,6 +1480,12 @@ function changeChartType() {
<style lang='scss'> <style lang='scss'>
.chart-container {
width: 100%;
height: 350px;
min-height: 300px;
display: block; /* Ensures the container is visible */
}
canvas { canvas {
width: 100%; width: 100%;