-
Notifications
You must be signed in to change notification settings - Fork 6
/
cardata.py
71 lines (62 loc) · 2.3 KB
/
cardata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from datetime import date, datetime, timezone, timedelta
from matplotlib import dates, pyplot
from db import *
import numpy as np
cardata = {}
def cardata_add(dealertype, date, carsplit):
if len(carsplit) >= 3 and carsplit[2] != "soldout":
if int(carsplit[0]) in cardata:
cardata[int(carsplit[0])].append(f"{dealertype},{date},{carsplit[1]}")
else:
cardata[int(carsplit[0])] = [f"{dealertype},{date},{carsplit[1]}"]
def cardata_exists(carid):
if carid in cardata and len(cardata[carid]) > 1:
return True
return False
def cardata_plot(carid):
now = datetime.now(timezone.utc).date()
mindate = now - timedelta(weeks=53)
#mindate = date.fromisoformat("2022-03-03")
daycredits = {}
for values in cardata[carid]:
type, datestr, credits = values.split(',')
if datestr == "*":
daycredits["*"] = int(credits)
else:
dateval = date.fromisoformat(f"20{datestr}")
daycredits[dates.date2num(dateval)] = int(credits)
# convert to matplotlib (which also allows for indexing)
mindate = dates.date2num(mindate)
x = []
y = []
annotates = []
lastval = None
weeks = []
for i in range(int(mindate), int(dates.date2num(now))+1):
f = np.float64(i)
x.append(dates.num2date(f))
if f in daycredits:
y.append(daycredits[f])
elif "*" in daycredits:
y.append(daycredits["*"])
else:
y.append(None)
if y[-1] != lastval:
lastval = y[-1]
if lastval is not None:
annotates.append((f"Cr. {int(lastval):,}",f,lastval))
for i in range(int(dates.date2num(now)), int(mindate)-6, -7):
f = np.float64(i)
weeks.append(f)
fig = pyplot.figure(figsize=(16,6))
pyplot.title(cardb_id_to_name(carid), {'fontsize': 20})
pyplot.ylim(0,max([i for i in y if i is not None])*1.1)
pyplot.plot(x, y, marker='.')
for annot in annotates:
pyplot.annotate(annot[0], (annot[1], annot[2]), xytext=(4,4), textcoords="offset pixels")
pyplot.grid(True, alpha=0.2)
pyplot.xticks(weeks, rotation=60)
pyplot.ticklabel_format(axis='y', style='plain')
fig.subplots_adjust(bottom=0.16)
pyplot.savefig(f"build/cars/prices_{carid}.png")
pyplot.close()