Merge pull request #4 from Redume/feat/improved-graphics
Some checks are pending
Create and publish a Docker image / detect what files changed (push) Waiting to run
Create and publish a Docker image / build-and-push-server (push) Blocked by required conditions
Create and publish a Docker image / build-and-push-chart (push) Blocked by required conditions
Create and publish a Docker image / build-and-push-CR (push) Blocked by required conditions
Create and publish a Docker image / build-and-push-web (push) Blocked by required conditions
Deploy docs / detect what files changed (push) Waiting to run
Deploy docs / deploy (push) Blocked by required conditions

Feat/improved graphics
This commit is contained in:
Данил 2025-02-19 14:29:06 +03:00 committed by GitHub
commit 2c4a15a3fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 85 additions and 69 deletions

View file

@ -6,6 +6,8 @@ based on historical data retrieved from the database.
from datetime import datetime from datetime import datetime
from matplotlib import pyplot as plt 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 function.gen_unique_name import generate_unique_name
from database.server import create_pool from database.server import create_pool
@ -44,7 +46,8 @@ async def create_chart(
async with pool.acquire() as conn: async with pool.acquire() as conn:
data = await conn.fetch( data = await conn.fetch(
'SELECT date, rate FROM currency ' 'SELECT date, rate FROM currency '
'WHERE (date BETWEEN $1 AND $2) AND from_currency = $3 AND conv_currency = $4 ORDER BY date', 'WHERE (date BETWEEN $1 AND $2) ' +
'AND from_currency = $3 AND conv_currency = $4 ORDER BY date',
start_date_obj, start_date_obj,
end_date_obj, end_date_obj,
from_currency.upper(), from_currency.upper(),
@ -55,32 +58,43 @@ async def create_chart(
return None return None
date, rate = [], [] date, rate = [], []
fig = plt.gcf()
for row in data: for row in data:
date.append(str(row['date'])) date.append(row[0])
rate.append(row['rate']) rate.append(row[1])
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]: newx_2 = np.linspace(0, len(date) - 1, 200)
plt.plot(date, rate, color='green', marker='o') newy_2 = spline(newx_2)
elif rate[0] > rate[-1]: fig, ax = plt.subplots(figsize=(15, 6))
plt.plot(date, rate, color='red', marker='o')
else:
plt.plot(date, rate, color='grey')
plt.xlabel('Date') for label in (ax.get_xticklabels() + ax.get_yticklabels()):
plt.ylabel('Rate') label.set_fontsize(10)
ax.set_xticks(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( name = await generate_unique_name(
f'{from_currency.upper()}_{conv_currency.upper()}', f'{from_currency.upper()}_{conv_currency.upper()}',
datetime.now() datetime.now()
) )
fig.savefig(f'../charts/{name}.png')
if rate[0] < rate[-1]:
plt.plot(newx_2, newy_2, color='green')
elif rate[0] > rate[-1]:
plt.plot(newx_2, newy_2, color='red')
else:
plt.plot(newx_2, newy_2, color='grey')
plt.savefig(f'../charts/{name}.png')
fig.clear() fig.clear()
return name return name

View file

@ -5,3 +5,5 @@ fastapi[standard]~=0.115.2
starlette~=0.40.0 starlette~=0.40.0
user_agents==2.2.0 user_agents==2.2.0
asyncpg~=0.30.0 asyncpg~=0.30.0
scipy~=1.15.2
numpy~=2.2.0