InNoobWeTrust commited on
Commit
0fa6f3b
·
1 Parent(s): 37a1056

refactor: improve UX for zoom and span

Browse files
Files changed (1) hide show
  1. streamlit_app.py +18 -20
streamlit_app.py CHANGED
@@ -27,8 +27,9 @@ def gen_charts(asset, chart_size={"width": 560, "height": 150}):
27
  cum_flow_individual = data.cum_flow_individual
28
  cum_flow_total = data.cum_flow_total
29
 
30
- # Selection interval
31
- interval = alt.selection_interval(encodings=["x"])
 
32
  # Line chart of price
33
  price = (
34
  alt.Chart(price)
@@ -38,12 +39,10 @@ def gen_charts(asset, chart_size={"width": 560, "height": 150}):
38
  y=alt.Y("Price:Q").scale(zero=False),
39
  color=alt.value("crimson"),
40
  )
41
- ).properties(
42
  width=chart_size["width"],
43
  height=chart_size["height"],
44
  )
45
- # View selection
46
- view = price.add_params(interval)
47
 
48
  trading_vol_individual = (
49
  alt.Chart(etf_volumes)
@@ -56,7 +55,7 @@ def gen_charts(asset, chart_size={"width": 560, "height": 150}):
56
  y=alt.Y("Volume:Q", title="Trading Volume Individual"),
57
  color="Funds:N",
58
  )
59
- ).properties(
60
  width=chart_size["width"],
61
  height=chart_size["height"],
62
  )
@@ -71,7 +70,7 @@ def gen_charts(asset, chart_size={"width": 560, "height": 150}):
71
  y=alt.Y("sum(Volume):Q", title="Trading Volume Total"),
72
  color=alt.value("teal"),
73
  )
74
- ).properties(
75
  width=chart_size["width"],
76
  height=chart_size["height"],
77
  )
@@ -89,7 +88,7 @@ def gen_charts(asset, chart_size={"width": 560, "height": 150}):
89
  y=alt.Y("Net Flow:Q", title="Net Flow Individual"),
90
  color="Funds:N",
91
  )
92
- ).properties(
93
  width=chart_size["width"],
94
  height=chart_size["height"],
95
  )
@@ -105,7 +104,7 @@ def gen_charts(asset, chart_size={"width": 560, "height": 150}):
105
  alt.value("orangered"), # The negative color
106
  ),
107
  )
108
- ).properties(
109
  width=chart_size["width"],
110
  height=chart_size["height"],
111
  )
@@ -123,7 +122,7 @@ def gen_charts(asset, chart_size={"width": 560, "height": 150}):
123
  y=alt.Y("Net Flow:Q", title="Cumulative Flow Individual"),
124
  color=alt.Color("Funds:N", scale=alt.Scale(scheme="tableau20")),
125
  )
126
- ).properties(
127
  width=chart_size["width"],
128
  height=chart_size["height"],
129
  )
@@ -142,14 +141,12 @@ def gen_charts(asset, chart_size={"width": 560, "height": 150}):
142
  "negative:N", title="Negative Flow", scale=alt.Scale(scheme="set2")
143
  ),
144
  )
145
- ).properties(
146
  width=chart_size["width"],
147
  height=chart_size["height"],
148
  )
149
 
150
  return SimpleNamespace(
151
- interval=interval,
152
- view=view,
153
  price=price,
154
  trading_vol_individual=trading_vol_individual,
155
  trading_vol_total=trading_vol_total,
@@ -161,15 +158,16 @@ def gen_charts(asset, chart_size={"width": 560, "height": 150}):
161
 
162
  def asset_charts(asset: str, chart_size={"width": "container", "height": 150}):
163
  charts = gen_charts(asset, chart_size)
 
164
  # Vertical concat the charts in each asset into single column of that asset
165
  all_charts = (
166
- charts.view
167
- & charts.trading_vol_individual.encode(x=alt.X("Date:T", axis=alt.Axis(tickCount="day"), title="",scale=alt.Scale(domain=charts.interval)))
168
- & charts.trading_vol_total.encode(x=alt.X("Date:T", axis=alt.Axis(tickCount="day"), title="",scale=alt.Scale(domain=charts.interval)))
169
- & charts.net_flow_individual.encode(x=alt.X("Date:T", axis=alt.Axis(tickCount="day"), title="",scale=alt.Scale(domain=charts.interval)))
170
- & charts.net_flow_total.encode(x=alt.X("Date:T", axis=alt.Axis(tickCount="day"), title="",scale=alt.Scale(domain=charts.interval)))
171
- & charts.cum_flow_individual.encode(x=alt.X("Date:T", axis=alt.Axis(tickCount="day"), title="",scale=alt.Scale(domain=charts.interval)))
172
- & charts.cum_flow_total.encode(x=alt.X("Date:T", axis=alt.Axis(tickCount="day"), title="",scale=alt.Scale(domain=charts.interval)))
173
  ).resolve_scale(
174
  color="independent",
175
  ).properties(
 
27
  cum_flow_individual = data.cum_flow_individual
28
  cum_flow_total = data.cum_flow_total
29
 
30
+ # Create bindings for interval selection
31
+ scale_selection = alt.selection_interval(encodings=["x"],bind="scales")
32
+
33
  # Line chart of price
34
  price = (
35
  alt.Chart(price)
 
39
  y=alt.Y("Price:Q").scale(zero=False),
40
  color=alt.value("crimson"),
41
  )
42
+ ).add_params(scale_selection).properties(
43
  width=chart_size["width"],
44
  height=chart_size["height"],
45
  )
 
 
46
 
47
  trading_vol_individual = (
48
  alt.Chart(etf_volumes)
 
55
  y=alt.Y("Volume:Q", title="Trading Volume Individual"),
56
  color="Funds:N",
57
  )
58
+ ).add_params(scale_selection).properties(
59
  width=chart_size["width"],
60
  height=chart_size["height"],
61
  )
 
70
  y=alt.Y("sum(Volume):Q", title="Trading Volume Total"),
71
  color=alt.value("teal"),
72
  )
73
+ ).add_params(scale_selection).properties(
74
  width=chart_size["width"],
75
  height=chart_size["height"],
76
  )
 
88
  y=alt.Y("Net Flow:Q", title="Net Flow Individual"),
89
  color="Funds:N",
90
  )
91
+ ).add_params(scale_selection).properties(
92
  width=chart_size["width"],
93
  height=chart_size["height"],
94
  )
 
104
  alt.value("orangered"), # The negative color
105
  ),
106
  )
107
+ ).add_params(scale_selection).properties(
108
  width=chart_size["width"],
109
  height=chart_size["height"],
110
  )
 
122
  y=alt.Y("Net Flow:Q", title="Cumulative Flow Individual"),
123
  color=alt.Color("Funds:N", scale=alt.Scale(scheme="tableau20")),
124
  )
125
+ ).add_params(scale_selection).properties(
126
  width=chart_size["width"],
127
  height=chart_size["height"],
128
  )
 
141
  "negative:N", title="Negative Flow", scale=alt.Scale(scheme="set2")
142
  ),
143
  )
144
+ ).add_params(scale_selection).properties(
145
  width=chart_size["width"],
146
  height=chart_size["height"],
147
  )
148
 
149
  return SimpleNamespace(
 
 
150
  price=price,
151
  trading_vol_individual=trading_vol_individual,
152
  trading_vol_total=trading_vol_total,
 
158
 
159
  def asset_charts(asset: str, chart_size={"width": "container", "height": 150}):
160
  charts = gen_charts(asset, chart_size)
161
+
162
  # Vertical concat the charts in each asset into single column of that asset
163
  all_charts = (
164
+ charts.price
165
+ & charts.trading_vol_individual
166
+ & charts.trading_vol_total
167
+ & charts.net_flow_individual
168
+ & charts.net_flow_total
169
+ & charts.cum_flow_individual
170
+ & charts.cum_flow_total
171
  ).resolve_scale(
172
  color="independent",
173
  ).properties(