]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/commitdiff
Fix color graph history for different flavors coffee
authorTomas Prochazka <tomas.prochazka@cvut.cz>
Fri, 10 Aug 2018 07:02:37 +0000 (09:02 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Fri, 10 Aug 2018 09:04:01 +0000 (11:04 +0200)
app.py
coffee_db.py

diff --git a/app.py b/app.py
index c87ffc9021525cd6755dce4545e1028523723f41..e8d7154aed61b63cd9007ff12c76095e9f472e59 100644 (file)
--- a/app.py
+++ b/app.py
@@ -87,10 +87,37 @@ def coffee_graph_history():
     if hist == []:
         days = tuple()
         counts = tuple()
+        flavors = tuple()
     else:
-        days, counts = zip(*hist)
+        days, counts, flavors = zip(*hist)
     fig = plt.figure(figsize=(4,3))
     ax = fig.add_subplot(111)
+
+    list_flavor=sorted(db.flavors())
+    l=[{} for i in range(len(list_flavor))]
+    for ll in l:
+        for d in days:
+            ll[d] = 0
+
+    for(d, c, f) in zip(days, counts, flavors):
+       if f is None:
+           continue
+       what_f = 0
+       for i in range(len(list_flavor)):
+           if f == list_flavor[i]:
+              what_f = i;
+              break
+       l[what_f][d] += c
+
+    z=list(0 for i in range(len(l[0])))
+    for flavor in range(len(list_flavor)):
+        sortedlist = [(k, l[flavor][k]) for k in sorted(l[flavor])]
+        x=[i[0] for i in sortedlist]
+        y=[i[1] for i in sortedlist]
+        ax.bar(range(len(x)),y,bottom=z)
+        z=[sum(i) for i in zip(y, z)]
+
+    days=set(days)
     xdays = [i.strftime("%a") for i in [
         date.today() - timedelta(j - 1) for j in
         range(len(days), 0, -1)]]
@@ -98,7 +125,6 @@ def coffee_graph_history():
     xdays[-2] = "YDA"
     ax.set_xticks(range(len(days)))
     ax.set_xticklabels(xdays)
-    ax.bar(range(len(days)), counts)
     ax.set_title("Your week")
     fig.savefig(b, format="svg", bbox_inches="tight")
     b.seek(0)
index 6207b29d3764c781e0cfdcfb63c448d2bbf1961a..a838746dfaf9ba9e77f1ff5fc6f8634c9dab375a 100644 (file)
@@ -90,24 +90,25 @@ def coffee_flavors(uid=None):
     close_db(conn)
     return res
 
+
 def coffee_history(uid=None):
     conn, c = open_db()
 
     if uid is None:
         res = list(c.execute("""
-            select strftime('%d', ds.d),count(c.time) from
+            select strftime('%d', ds.d),count(c.flavor),c.flavor from
             (select num,date('now',-num || ' days') as d from days) ds
             left join coffees c
-            on d = date(c.time) group by d
+            on d = date(c.time) group by d, c.flavor
             """))
     else:
         res = list(c.execute(
             """
-            select strftime('%d', ds.d),count(c.time) from
+            select strftime('%d', ds.d),count(c.flavor),c.flavor from
             (select num,date('now',-num || ' days') as d from days) ds
             left join
-            (select time from coffees where id = ? ) c
-            on d = date(c.time) group by d
+            (select time,flavor from coffees where id = ?) c
+            on d = date(c.time) group by d, c.flavor
             """
             , (uid,)))