Jelajahi Sumber

add bar graph

Daniel Sheffield 2 tahun lalu
induk
melakukan
a76a375dcb
2 mengubah file dengan 58 tambahan dan 6 penghapusan
  1. 33 6
      app/activities/TransactionEditor.py
  2. 25 0
      app/widgets.py

+ 33 - 6
app/activities/TransactionEditor.py

@@ -27,7 +27,9 @@ from ..widgets import (
     AutoCompleteFloatEdit,
     FocusWidget,
     AutoCompletePopUp,
-    NoTabCheckBox
+    NoTabCheckBox,
+    FlowBarGraph,
+    FlowGraphVScale,
 )
 from . import ActivityManager
 from .Rating import Rating
@@ -140,7 +142,24 @@ class TransactionEditor(FocusWidget):
         _avg, _min, _max = [
           float(x) for x in df[['avg','min','max']].iloc[0]
         ]
-        self.rating.update_rating(_avg, _min, _max, unit, price=price, quantity=quantity),
+        self.rating.update_rating(_avg, _min, _max, unit, price=price, quantity=quantity)
+        #print(df)
+        #input()
+        self.graph._invalidate()
+        top = 10 # need to set this same as in column widget
+        scale = (df['max'].apply(float).iloc[0]/top)*(top+1)
+        self.graph.set_bar_width(1)
+        # need to sort by ts and take last 45 items
+        self.graph.set_data(df[['ts_raw','$/unit']].apply(
+            lambda x, axis=None: (
+                x['ts_raw'].timestamp(), float(x['$/unit'])
+            ), axis=1), scale)
+        self.graph_vscale._invalidate()
+        self.graph_vscale.set_scale([
+            ((_min*top)//scale, f'{_min:>5.2f} '),
+            ((_avg*top)//scale, f'{_avg:>5.2f} '),
+            ((_max*top)//scale, f'{_max:>5.2f} '),
+        ], top)
 
 
     def focus_on_product(self):
@@ -183,6 +202,8 @@ class TransactionEditor(FocusWidget):
             'spread': Text(''),
             'marker': Text(''),
         }
+        self.graph = FlowBarGraph(['bg','badge_highlight','popup_focus', ], hatt=['dark red', 'dark red', 'dark red'])
+        self.graph_vscale = FlowGraphVScale(self.graph, [], 0)
         self.rating = Rating(dict(filter(
           lambda x: x[0] in ('spread','rating','marker'),
           self.text_fields.items()
@@ -252,10 +273,16 @@ class TransactionEditor(FocusWidget):
               lambda x: _widgets[x] if x is not None else Divider(),
               bottom_pane
             )),
-            (50, Pile([LineBox(
-              AttrMap(components['badge'], 'badge'),
-              title="Current Price", title_align='left',
-            )])),
+            (50, Pile([
+              LineBox(
+                AttrMap(components['badge'], 'badge'),
+                title="Current Price", title_align='left',
+              ),
+              Columns([
+                (6, self.graph_vscale),
+                (44, self.graph),
+              ])
+            ])),
           ])
         })
         connect_signal(self.buttons['done'], 'click', lambda _: save_and_clear_cb())

+ 25 - 0
app/widgets.py

@@ -310,3 +310,28 @@ class SuggestionPopup(urwid.WidgetWrap):
             return
 
         return super().keypress(size, key)
+
+class FlowBarGraph(urwid.BarGraph):
+    ignore_focus = True
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+    
+    def rows(self, size, focus=False):
+        return 10
+    
+    def render(self, size, focus=False):
+        _size = (size[0], self.rows(size))
+        return super().render(_size,focus=focus)
+
+class FlowGraphVScale(urwid.GraphVScale):
+    ignore_focus = True
+    def __init__(self, graph, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.graph = graph
+    
+    def rows(self, size, focus=False):
+        return self.graph.rows(size)
+    
+    def render(self, size, focus=False):
+        _size = (size[0], self.rows(size))
+        return super().render(_size,focus=focus)