def plot_balance_report(user_id): current_month = get_current_month() current_month_trends = data_observer.get_balances_trends( user_id, current_month["period"]) current_title = f"Изменение остатков {current_month['name']}" last_month = get_last_month() last_month_trends = data_observer.get_balances_trends( user_id, last_month["period"]) last_title = f"Изменение остатков {last_month['name']}" current_month_figure = make_trends_figure(current_month_trends, current_title, current_month) last_month_figure = make_trends_figure(last_month_trends, last_title, last_month) vertical_space = round(current_month_figure.plot_height * 0.1) return column( current_month_figure, Spacer(height=vertical_space), last_month_figure )
def __init__(self): self.base_url = 'https://data.police.uk/api/' self.today = datetime.today() self.current_month = str(datetime(self.today.year, self.today.month, 1))[:7] self.last_month = get_last_month() self.police_force = 'metropolitan'
def show_statistics(bot, update): user = update.effective_user current_month = get_current_month() last_month = get_last_month() reply_text = "Вот статистика расходов в текущем месяце:\n " for expense in data_observer.statistics_for_period_by_category( user.id, current_month["period"]): amount = f"{expense[0]:,.0f}".replace(",", " ") reply_text = "\n".join([reply_text, f"{expense[1]} - {amount}"]) reply_text = "\n \n".join( [reply_text, f"Твои расходы в прошлом месяце:\n "]) for expense in data_observer.statistics_for_period_by_category( user.id, last_month["period"]): amount = f"{expense[0]:,.0f}".replace(",", " ") reply_text = "\n".join([reply_text, f"{expense[1]} - {amount}"]) update.message.reply_text(reply_text) # Send the barplot with statistics current_data = data_observer.statistics_for_period_by_category( user.id, current_month["period"]) df_current = pd.DataFrame(current_data, columns=['Amount', 'Category']) df_current['Month'] = current_month['name'] previous_data = data_observer.statistics_for_period_by_category( user.id, last_month["period"]) df_previous = pd.DataFrame(previous_data, columns=['Amount', 'Category']) df_previous['Month'] = last_month["name"] data = pd.concat([df_current, df_previous]) number_of_categories = len(data['Category'].unique()) sns.set_style("darkgrid") sns.set_context("notebook", font_scale=1.5) sns.set_palette("deep") plot = sns.barplot(x="Category", y="Amount", hue="Month", data=data) plot.set_xlabel("") plot.set_title("Expenses") for item in plot.get_xticklabels(): item.set_rotation(-90) imgdata = BytesIO() plot.figure.set_size_inches(10, 2.5 * number_of_categories) plot.figure.subplots_adjust(bottom=0.2) plot.figure.savefig(imgdata, format="png", pad_inches=5) plot.clear() imgdata.seek(0) bot.send_photo(chat_id=update.message.chat_id, photo=imgdata)
def plot_categories(user_id): current_month = get_current_month() current_month_data = data_observer.statistics_for_period_by_category( user_id, current_month['period'] ) df_current = pd.DataFrame(current_month_data, columns=['Amount', 'Category']) df_current['Month'] = current_month['name'] last_month = get_last_month() last_month_data = data_observer.statistics_for_period_by_category( user_id, last_month['period'] ) df_last = pd.DataFrame(last_month_data, columns=['Amount', 'Category']) df_last['Month'] = last_month['name'] categories = pd.concat([df_current, df_last])['Category'].unique() max_amount = max(df_current['Amount'].max(), df_last['Amount'].max()) cats_plot = figure(x_range=categories, y_range=(0, max_amount + 10000), plot_height=250, plot_width=1000, title="Затраты в текущем месяце", toolbar_location=None, tooltips=[("Сумма", "@Amount")], tools="") cats_plot.vbar(x=dodge('Category', -0.15, range=cats_plot.x_range), top='Amount', width=0.2, legend=current_month['name'], color="#718dbf", source=ColumnDataSource(df_current)) cats_plot.vbar(x=dodge('Category', 0.15, range=cats_plot.x_range), top='Amount', width=0.2, legend=last_month['name'], color="#e84d60", source=ColumnDataSource(df_last)) cats_plot.xgrid.grid_line_color = None cats_plot.yaxis[0].formatter = NumeralTickFormatter(format="0,0[.]00") cats_plot.legend.orientation = "vertical" cats_plot.legend.location = "top_right" return cats_plot
def plot_expense_reports(user_id): cats_plot = plot_categories(user_id) trends_current_plot = plot_expense_trends( user_id, get_current_month() ) trends_last_plot = plot_expense_trends( user_id, get_last_month() ) vertical_space = round(cats_plot.plot_height * 0.2) return column(cats_plot, Spacer(height=vertical_space), trends_current_plot, Spacer(height=vertical_space), trends_last_plot, Spacer(height=vertical_space) )
def balances(): if not current_user.is_authenticated: return redirect(url_for("user.login")) user_id = current_user.user_id # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script, div = components(plot_balance_report(user_id)) html = render_template("report/balances.html", plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, title="Остатки на счетах", current_month=get_current_month()["name"], last_month=get_last_month()["name"], accounts_list=sorted( data_observer.get_all_account_names(user_id))) return encode_utf8(html)