InNoobWeTrust commited on
Commit
60beffb
·
1 Parent(s): 5b87a9a

feat: enhance visual design

Browse files
Files changed (1) hide show
  1. streamlit_app.py +48 -31
streamlit_app.py CHANGED
@@ -25,7 +25,7 @@ def gen_charts(asset, chart_size={"width": 560, "height": 300}):
25
  .transform_fold(
26
  etf_volumes.drop(columns="Date").columns.to_list(), as_=["Funds", "Volume"]
27
  )
28
- .mark_bar()
29
  .encode(
30
  x=alt.X("Date:T", axis=alt.Axis(tickCount="day")),
31
  y=alt.Y("Volume:Q"),
@@ -37,15 +37,16 @@ def gen_charts(asset, chart_size={"width": 560, "height": 300}):
37
  .transform_fold(
38
  etf_volumes.drop(columns="Date").columns.to_list(), as_=["Funds", "Volume"]
39
  )
40
- .mark_line()
41
  .encode(
42
  x=alt.X("Date:T", axis=alt.Axis(tickCount="day")),
43
  y=alt.Y("sum(Volume):Q", title="Total Volume"),
44
- color=alt.value("crimson"),
 
45
  )
46
  )
47
  # Combine trading volume and average trading volume
48
- trading_vol_fig += trading_vol_total_fig
49
  trading_vol_fig = trading_vol_fig.properties(
50
  title=f"{asset} ETF trading volume",
51
  **chart_size,
@@ -58,26 +59,35 @@ def gen_charts(asset, chart_size={"width": 560, "height": 300}):
58
  etf_flow_individual.drop(columns="Date").columns.to_list(),
59
  as_=["Funds", "Net Flow"],
60
  )
61
- .mark_bar()
62
  .encode(
63
- x=alt.X("Date:T", axis=alt.Axis(tickCount="day")),
64
  y=alt.Y("Net Flow:Q"),
65
  color="Funds:N",
66
  )
 
 
 
67
  )
68
- net_flow_total_line_fig = (
69
  alt.Chart(etf_flow_total)
70
- .mark_line()
71
  .encode(
72
  x=alt.X("Date:T", axis=alt.Axis(tickCount="day")),
73
  y=alt.Y("Total:Q"),
74
- color=alt.value("crimson"),
 
 
 
 
75
  )
 
 
 
76
  )
77
- net_flow_individual_fig += net_flow_total_line_fig
78
- net_flow_individual_fig = net_flow_individual_fig.properties(
79
  title=f"{asset} ETF net flow of individual funds",
80
- **chart_size,
81
  )
82
 
83
  net_flow_total_fig = (
@@ -92,24 +102,27 @@ def gen_charts(asset, chart_size={"width": 560, "height": 300}):
92
  alt.value("orangered"), # The negative color
93
  ),
94
  )
 
 
 
95
  )
96
  # Line chart of price
97
  price_fig = (
98
  alt.Chart(price)
99
  .mark_line()
100
  .encode(
101
- x=alt.X("Date:T", axis=alt.Axis(tickCount="day")),
102
- y=alt.Y("Price:Q"),
103
  color=alt.value("crimson"),
104
  )
 
 
 
105
  )
106
 
107
- net_flow_total_fig += price_fig
108
- net_flow_total_fig = net_flow_total_fig.resolve_scale(
109
- y="independent",
110
- ).properties(
111
  title=f"{asset} ETF net flow total vs asset price",
112
- **chart_size,
113
  )
114
 
115
  # Stacking area chart of flow from individual funds
@@ -125,13 +138,13 @@ def gen_charts(asset, chart_size={"width": 560, "height": 300}):
125
  y=alt.Y("Net Flow:Q"),
126
  color=alt.Color("Funds:N", scale=alt.Scale(scheme="tableau20")),
127
  )
128
- )
129
- cum_flow_individual_net_fig += price_fig
130
- cum_flow_individual_net_fig = cum_flow_individual_net_fig.resolve_scale(
131
- y="independent",
132
  ).properties(
 
 
 
 
 
133
  title=f"{asset} ETF cumulative flow of individual funds vs asset price",
134
- **chart_size,
135
  )
136
 
137
  # Area chart for cumulative flow
@@ -148,13 +161,13 @@ def gen_charts(asset, chart_size={"width": 560, "height": 300}):
148
  "negative:N", title="Negative Flow", scale=alt.Scale(scheme="set2")
149
  ),
150
  )
151
- )
152
- cum_flow_total_fig += price_fig
153
- cum_flow_total_fig = cum_flow_total_fig.resolve_scale(
154
- y="independent",
155
  ).properties(
 
 
 
 
 
156
  title=f"{asset} ETF cumulative flow total vs asset price",
157
- **chart_size,
158
  )
159
 
160
  return SimpleNamespace(
@@ -213,8 +226,12 @@ if __name__ == "__main__":
213
  'Asset to view',
214
  ('BTC', 'ETH'),
215
  )
216
- chart = asset_charts(asset)
217
- st.altair_chart(chart, use_container_width = True)
 
 
 
 
218
  with flow_tab:
219
  btc_flow, eth_flow = btc.etf_flow, eth.etf_flow
220
  btc_flow['Asset'] = 'BTC'
 
25
  .transform_fold(
26
  etf_volumes.drop(columns="Date").columns.to_list(), as_=["Funds", "Volume"]
27
  )
28
+ .mark_line()
29
  .encode(
30
  x=alt.X("Date:T", axis=alt.Axis(tickCount="day")),
31
  y=alt.Y("Volume:Q"),
 
37
  .transform_fold(
38
  etf_volumes.drop(columns="Date").columns.to_list(), as_=["Funds", "Volume"]
39
  )
40
+ .mark_bar()
41
  .encode(
42
  x=alt.X("Date:T", axis=alt.Axis(tickCount="day")),
43
  y=alt.Y("sum(Volume):Q", title="Total Volume"),
44
+ color=alt.value("gray"),
45
+ opacity=alt.value(0.5),
46
  )
47
  )
48
  # Combine trading volume and average trading volume
49
+ trading_vol_fig = trading_vol_total_fig + trading_vol_fig
50
  trading_vol_fig = trading_vol_fig.properties(
51
  title=f"{asset} ETF trading volume",
52
  **chart_size,
 
59
  etf_flow_individual.drop(columns="Date").columns.to_list(),
60
  as_=["Funds", "Net Flow"],
61
  )
62
+ .mark_line()
63
  .encode(
64
+ x=alt.X("Date:T", axis=alt.Axis(tickCount="day", title='', labels=False)),
65
  y=alt.Y("Net Flow:Q"),
66
  color="Funds:N",
67
  )
68
+ ).properties(
69
+ width=chart_size['width'],
70
+ height=chart_size['height'] / 2,
71
  )
72
+ net_flow_total_fig = (
73
  alt.Chart(etf_flow_total)
74
+ .mark_bar()
75
  .encode(
76
  x=alt.X("Date:T", axis=alt.Axis(tickCount="day")),
77
  y=alt.Y("Total:Q"),
78
+ color=alt.condition(
79
+ alt.datum.Total > 0,
80
+ alt.value("seagreen"), # The positive color
81
+ alt.value("orangered"), # The negative color
82
+ ),
83
  )
84
+ ).properties(
85
+ width=chart_size['width'],
86
+ height=chart_size['height'] / 2,
87
  )
88
+ net_flow_individual_fig = net_flow_individual_fig & net_flow_total_fig
89
+ net_flow_individual_fig = net_flow_individual_fig.resolve_scale(x='shared').properties(
90
  title=f"{asset} ETF net flow of individual funds",
 
91
  )
92
 
93
  net_flow_total_fig = (
 
102
  alt.value("orangered"), # The negative color
103
  ),
104
  )
105
+ ).properties(
106
+ width=chart_size['width'],
107
+ height=chart_size['height'] / 2,
108
  )
109
  # Line chart of price
110
  price_fig = (
111
  alt.Chart(price)
112
  .mark_line()
113
  .encode(
114
+ x=alt.X("Date:T", axis=alt.Axis(tickCount="day", title='', labels=False)),
115
+ y=alt.Y("Price:Q").scale(zero=False),
116
  color=alt.value("crimson"),
117
  )
118
+ ).properties(
119
+ width=chart_size['width'],
120
+ height=chart_size['height'] / 2,
121
  )
122
 
123
+ net_flow_total_fig = price_fig & net_flow_total_fig
124
+ net_flow_total_fig = net_flow_total_fig.resolve_scale(x='shared').properties(
 
 
125
  title=f"{asset} ETF net flow total vs asset price",
 
126
  )
127
 
128
  # Stacking area chart of flow from individual funds
 
138
  y=alt.Y("Net Flow:Q"),
139
  color=alt.Color("Funds:N", scale=alt.Scale(scheme="tableau20")),
140
  )
 
 
 
 
141
  ).properties(
142
+ width=chart_size['width'],
143
+ height=chart_size['height'] / 2,
144
+ )
145
+ cum_flow_individual_net_fig = price_fig & cum_flow_individual_net_fig
146
+ cum_flow_individual_net_fig = cum_flow_individual_net_fig.resolve_scale(x='shared').properties(
147
  title=f"{asset} ETF cumulative flow of individual funds vs asset price",
 
148
  )
149
 
150
  # Area chart for cumulative flow
 
161
  "negative:N", title="Negative Flow", scale=alt.Scale(scheme="set2")
162
  ),
163
  )
 
 
 
 
164
  ).properties(
165
+ width=chart_size['width'],
166
+ height=chart_size['height'] / 2,
167
+ )
168
+ cum_flow_total_fig = price_fig & cum_flow_total_fig
169
+ cum_flow_total_fig = cum_flow_total_fig.resolve_scale(x='shared').properties(
170
  title=f"{asset} ETF cumulative flow total vs asset price",
 
171
  )
172
 
173
  return SimpleNamespace(
 
226
  'Asset to view',
227
  ('BTC', 'ETH'),
228
  )
229
+ charts = gen_charts(asset, chart_size={"width": 'container', "height": 600})
230
+ st.altair_chart(charts.trading_vol_fig, use_container_width = True)
231
+ st.altair_chart(charts.net_flow_individual_fig, use_container_width = True)
232
+ st.altair_chart(charts.net_flow_total_fig, use_container_width = True)
233
+ st.altair_chart(charts.cum_flow_individual_net_fig, use_container_width = True)
234
+ st.altair_chart(charts.cum_flow_total_fig, use_container_width = True)
235
  with flow_tab:
236
  btc_flow, eth_flow = btc.etf_flow, eth.etf_flow
237
  btc_flow['Asset'] = 'BTC'