2023-02-08 23:37:11 +00:00
|
|
|
# note: This module takes 1-2 seconds to import. It should be imported *on-demand*.
|
|
|
|
|
|
2016-12-17 17:06:25 +01:00
|
|
|
import datetime
|
2024-05-22 15:26:26 +00:00
|
|
|
from decimal import Decimal
|
2016-12-17 17:06:25 +01:00
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
|
|
import matplotlib
|
2017-09-23 05:54:38 +02:00
|
|
|
matplotlib.use('Qt5Agg')
|
2016-12-17 17:06:25 +01:00
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
import matplotlib.dates as md
|
|
|
|
|
|
2018-03-10 00:23:51 +01:00
|
|
|
from .i18n import _
|
|
|
|
|
from .bitcoin import COIN
|
|
|
|
|
|
2016-12-17 17:06:25 +01:00
|
|
|
|
2018-02-24 00:14:34 +01:00
|
|
|
class NothingToPlotException(Exception):
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return _("Nothing to plot.")
|
|
|
|
|
|
|
|
|
|
|
2018-02-15 14:59:05 +01:00
|
|
|
def plot_history(history):
|
2018-02-24 00:14:34 +01:00
|
|
|
if len(history) == 0:
|
|
|
|
|
raise NothingToPlotException()
|
2024-05-22 15:26:26 +00:00
|
|
|
hist_in = defaultdict(Decimal)
|
|
|
|
|
hist_out = defaultdict(Decimal)
|
2016-12-17 17:06:25 +01:00
|
|
|
for item in history:
|
2023-02-08 23:37:51 +00:00
|
|
|
is_lightning = item.get("lightning", False)
|
|
|
|
|
if not is_lightning and not item['confirmations']:
|
2016-12-17 17:06:25 +01:00
|
|
|
continue
|
2018-02-15 14:59:05 +01:00
|
|
|
if item['timestamp'] is None:
|
2016-12-17 17:06:25 +01:00
|
|
|
continue
|
2024-05-22 15:26:26 +00:00
|
|
|
value = Decimal(item['value'].value)/COIN
|
2018-02-15 14:59:05 +01:00
|
|
|
date = item['date']
|
2016-12-17 17:06:25 +01:00
|
|
|
datenum = int(md.date2num(datetime.date(date.year, date.month, 1)))
|
|
|
|
|
if value > 0:
|
|
|
|
|
hist_in[datenum] += value
|
|
|
|
|
else:
|
|
|
|
|
hist_out[datenum] -= value
|
|
|
|
|
|
|
|
|
|
f, axarr = plt.subplots(2, sharex=True)
|
|
|
|
|
plt.subplots_adjust(bottom=0.2)
|
2021-03-21 00:34:25 -04:00
|
|
|
plt.xticks(rotation=25)
|
2016-12-19 12:50:50 +01:00
|
|
|
ax = plt.gca()
|
2016-12-17 17:06:25 +01:00
|
|
|
plt.ylabel('BTC')
|
2016-12-19 12:50:50 +01:00
|
|
|
plt.xlabel('Month')
|
2016-12-17 17:06:25 +01:00
|
|
|
xfmt = md.DateFormatter('%Y-%m-%d')
|
|
|
|
|
ax.xaxis.set_major_formatter(xfmt)
|
2016-12-19 12:50:50 +01:00
|
|
|
axarr[0].set_title('Monthly Volume')
|
2016-12-17 17:06:25 +01:00
|
|
|
xfmt = md.DateFormatter('%Y-%m')
|
|
|
|
|
ax.xaxis.set_major_formatter(xfmt)
|
|
|
|
|
width = 20
|
2018-02-24 00:14:34 +01:00
|
|
|
|
|
|
|
|
r1 = None
|
|
|
|
|
r2 = None
|
|
|
|
|
dates_values = list(zip(*sorted(hist_in.items())))
|
|
|
|
|
if dates_values and len(dates_values) == 2:
|
|
|
|
|
dates, values = dates_values
|
|
|
|
|
r1 = axarr[0].bar(dates, values, width, label='incoming')
|
|
|
|
|
axarr[0].legend(loc='upper left')
|
2017-12-09 20:35:46 +01:00
|
|
|
dates_values = list(zip(*sorted(hist_out.items())))
|
|
|
|
|
if dates_values and len(dates_values) == 2:
|
|
|
|
|
dates, values = dates_values
|
|
|
|
|
r2 = axarr[1].bar(dates, values, width, color='r', label='outgoing')
|
|
|
|
|
axarr[1].legend(loc='upper left')
|
2018-02-24 00:14:34 +01:00
|
|
|
if r1 is None and r2 is None:
|
|
|
|
|
raise NothingToPlotException()
|
2016-12-17 17:06:25 +01:00
|
|
|
return plt
|