From 2c28db0eb64faa3c6220b7648f0ec89742902600 Mon Sep 17 00:00:00 2001 From: Redume Date: Mon, 17 Feb 2025 19:50:24 +0300 Subject: [PATCH] feat(chart): Rounded the corners, reduced the number of dates --- chart/function/create_chart.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/chart/function/create_chart.py b/chart/function/create_chart.py index 5faf618..44213a1 100644 --- a/chart/function/create_chart.py +++ b/chart/function/create_chart.py @@ -6,6 +6,8 @@ based on historical data retrieved from the database. from datetime import datetime from matplotlib import pyplot as plt +from scipy.interpolate import make_interp_spline +import numpy as np from function.gen_unique_name import generate_unique_name from database.server import create_pool @@ -55,32 +57,32 @@ async def create_chart( return None date, rate = [], [] - fig = plt.gcf() for row in data: - date.append(str(row['date'])) + parsed_date = datetime.strptime(row['data'], '%Y-%m-%d%H:%M:%S.%fZ').date() + date.append(parsed_date) rate.append(row['rate']) - width = 18.5 + (len(date) // 5) * 3 - fig.set_size_inches(width, 9.5) + spline = make_interp_spline(range(len(date)), rate, k=2) + x = np.arange(len(date)) - if rate[0] < rate[-1]: - plt.plot(date, rate, color='green', marker='o') - elif rate[0] > rate[-1]: - plt.plot(date, rate, color='red', marker='o') - else: - plt.plot(date, rate, color='grey') + newx_2 = np.linspace(0, len(date) - 1, 200) + newy_2 = spline_2(newx_2) + fig, ax = plt.subplot(figsize=(15, 6)) - plt.xlabel('Date') - plt.ylabel('Rate') + for label in (ax.get_xticklabels() + ax.get_yticklabels()): + label.set_fontsize(10) + ax.set_xtick(np.linspace(0, len(date) - 1, 10)) + ax.set_xticklabels([date[int(i)].strftime('%Y-%m-%d') for i in np.linspace(0, len(date) - 1, 10).astype(int)]) name = await generate_unique_name( f'{from_currency.upper()}_{conv_currency.upper()}', datetime.now() ) - fig.savefig(f'../charts/{name}.png') + plt.plot(newx_2, newy_2) + plt.savefig(f'../charts/{name}.png') fig.clear() return name