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 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
@ -44,7 +46,8 @@ async def create_chart(
async with pool.acquire() as conn:
data = await conn.fetch(
'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,
end_date_obj,
from_currency.upper(),
@ -55,32 +58,43 @@ async def create_chart(
return None
date, rate = [], []
fig = plt.gcf()
for row in data:
date.append(str(row['date']))
rate.append(row['rate'])
width = 18.5 + (len(date) // 5) * 3
fig.set_size_inches(width, 9.5)
date.append(row[0])
rate.append(row[1])
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(newx_2)
fig, ax = plt.subplots(figsize=(15, 6))
plt.xlabel('Date')
plt.ylabel('Rate')
for label in (ax.get_xticklabels() + ax.get_yticklabels()):
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(
f'{from_currency.upper()}_{conv_currency.upper()}',
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()
return name

View file

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