summaryrefslogtreecommitdiff
path: root/ping/frontend/src/lib/components/dashboard/TrendingSymbols.svelte
blob: 3d947ad414426eb761c248d3893be0c39eec3494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<script lang="ts">
	import { Chart } from 'chart.js';
	import { onMount } from 'svelte';

	function updateGraph() {
		getTrendingSymbols().then((trendingSymbols) => {
			const labels = trendingSymbols.map((d) => d.longName);

			const datasets = [
				{
					label: '50 day performance',
					data: trendingSymbols.map((ts) => ts.fiftyDayAverageChange),
					backgroundColor: 'green'
				},
				{
					label: '52 week performance',
					data: trendingSymbols.map((ts) => ts.fiftyTwoWeekLowChange),
					backgroundColor: 'orange'
				}
			];

			const data = { labels, datasets };

			// @ts-ignore
			new Chart(document.getElementById('graph'), {
				type: 'bar',
				data: data,
				options: {
					indexAxis: 'y',
					elements: {
						bar: {
							borderWidth: 2
						}
					},
					responsive: true,
					plugins: {
						legend: {
							position: 'bottom'
						}
					}
				}
			});
		});
	}

	async function getTrendingSymbols() {
		const trendingRes = await fetch('/stocksapi/trendingSymbols');
		if (!trendingRes.ok) {
			throw new Error('Failed to fetch trending symbols');
		}
		const trendingJson = await trendingRes.json();

		const { quotes } = trendingJson;

		const trendingSymbols = await Promise.all(
			quotes.map(async (quote: any) => {
				const quoteRes = await fetch(`/stocksapi/quote?query=${quote.symbol}`);
				const quoteJson = await quoteRes.json();
				return quoteJson;
			})
		);

		return trendingSymbols;
	}

	onMount(() => {
		updateGraph();
	});
</script>

<h2>Tendances</h2>
<canvas id="graph"></canvas>

<style>
	h2 {
		font-weight: bold;
		color: var(--text-lime);
		font-size: 24px;
	}

	#graph {
		max-height: 40vh;
		max-width: 40vw;
	}
</style>