From 2941d31cf246451c5d867de1d2b010e568573360 Mon Sep 17 00:00:00 2001 From: Janita Chalam Date: Mon, 7 Feb 2022 14:00:21 -0800 Subject: [PATCH] add time range dropdown to performance charts (#298) --- .../charts/piePriceAreaChart.svelte | 124 +++++++++++++++++- 1 file changed, 117 insertions(+), 7 deletions(-) diff --git a/src/components/charts/piePriceAreaChart.svelte b/src/components/charts/piePriceAreaChart.svelte index 99f2c503..3dea1981 100644 --- a/src/components/charts/piePriceAreaChart.svelte +++ b/src/components/charts/piePriceAreaChart.svelte @@ -2,21 +2,37 @@ import { watchResize } from 'svelte-watch-resize'; import uniqueId from 'lodash/uniqueId'; import { fetchChartData } from '../../stores/coingecko.js'; + import { clickOutside } from '../../helpers/clickOutside.js'; export let coingeckoId; let chart; let chartId = uniqueId('chart_'); + let dropdownOpen = false; + + const toggleDropdown = (event) => { + dropdownOpen = !dropdownOpen; + }; + + const closeDropdown = (event) => { + dropdownOpen = false; + }; const resize = (node) => { if (chart && chart.resize) chart.resize(node.clientWidth, node.clientHeight); }; - (async () => { - const chartData = await fetchChartData(coingeckoId); + function yearToDate(){ + const currentDate = new Date(); + return (Date.UTC(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()) - Date.UTC(currentDate.getFullYear(), 0, 0)) / 24 / 60 / 60 / 1000; + }; + + async function loadChart(days = 90) { + const chartData = await fetchChartData(coingeckoId, days); + if (chart) chart.remove(); chart = LightweightCharts.createChart(document.getElementById(chartId), { - width: 1200, - height: 400, + width: 1250, + height: 450, grid: { horzLines: { color: '#ffffff', @@ -36,6 +52,9 @@ chart.applyOptions({ handleScroll: true, handleScale: false, + timeScale: { + timeVisible: true, + } }); var areaSeries = chart.addAreaSeries({ @@ -52,8 +71,99 @@ }; }); - areaSeries.setData(normalizedData); - })(); + areaSeries.setData(normalizedData); + chart.timeScale().fitContent(); + } + + loadChart(); + -
+
+
+
+ + {#if dropdownOpen} +
+ +
+ {/if} +
+
+
+ +