Skip to content

Commit 27245c2

Browse files
committed
feat: update plotly dashboard
1 parent 56b065e commit 27245c2

File tree

5 files changed

+1593
-685
lines changed

5 files changed

+1593
-685
lines changed

plotly/dashboard.py

Lines changed: 79 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,100 @@
22
from dash import html, dcc
33
import plotly.express as px
44
import pandas as pd
5+
from cassandra.cluster import Cluster
56

67
app = dash.Dash(__name__)
78

89

9-
def fetch_data():
10-
return pd.read_excel('stockdata_stocks.xlsx', engine='openpyxl')
10+
def create_cassandra_session():
11+
cluster = Cluster(['localhost'], port=9042)
12+
session = cluster.connect('stockdata')
13+
return session
1114

1215

13-
df = fetch_data()
16+
def fetch_data_from_cassandra(table_name):
17+
session = create_cassandra_session()
18+
query = f"SELECT * FROM {table_name}"
19+
rows = session.execute(query)
20+
df = pd.DataFrame(list(rows))
21+
return df
1422

15-
bar_fig = px.bar(df, x='trade_date', y='price', color='trade_type',
16-
barmode='group', title='Price by Trade Date and Type')
17-
line_fig = px.line(df, x='trade_date', y='price', title='Price Over Time')
1823

19-
scatter_fig = px.scatter(df, x='quantity', y='price',
20-
color='trade_type', title='Price vs Quantity')
24+
def create_figure(df, chart_type, **kwargs):
25+
if df.empty:
26+
print(f"No data available for {kwargs.get('title')}")
27+
return dcc.Graph()
28+
else:
29+
if chart_type == 'bar':
30+
return px.bar(df, **kwargs)
31+
elif chart_type == 'line':
32+
return px.line(df, **kwargs)
33+
elif chart_type == 'scatter':
34+
return px.scatter(df, **kwargs)
35+
elif chart_type == 'area':
36+
return px.area(df, **kwargs)
37+
elif chart_type == 'pie':
38+
return px.pie(df, **kwargs)
2139

22-
area_fig = px.area(df, x='trade_date', y='price',
23-
title='Price Trends Over Time')
24-
pie_fig = px.pie(df, names='trade_type', values='price',
25-
title='Price Distribution by Trade Type')
26-
histogram_fig = px.histogram(
27-
df, x='price', nbins=20, title='Price Distribution')
28-
box_fig = px.box(df, y='price', x='trade_type',
29-
title='Price Distribution by Trade Type')
3040

31-
app.layout = html.Div(children=[
32-
html.H1(children=' RealTime StockStream 💵🕊️'),
41+
stocks_df = fetch_data_from_cassandra('stocks')
42+
grouped_stocks_df = fetch_data_from_cassandra('grouped_stocks')
43+
pivoted_stocks_df = fetch_data_from_cassandra('pivoted_stocks')
44+
ranked_stocks_df = fetch_data_from_cassandra('ranked_stocks')
45+
analytics_stocks_df = fetch_data_from_cassandra('analytics_stocks')
46+
47+
bar_fig = create_figure(stocks_df, 'bar', x='trade_date', y='price',
48+
color='trade_type', title='Price by Trade Date and Type')
49+
line_fig = create_figure(grouped_stocks_df, 'line', x='trade_type',
50+
y='avg_price', title='Average Price by Trade Type')
51+
scatter_fig = create_figure(ranked_stocks_df, 'scatter', x='quantity',
52+
y='price', color='trade_type', title='Price vs Quantity')
53+
area_fig = create_figure(pivoted_stocks_df, 'area', x='stock', y=[
54+
'avg_price_buy', 'avg_price_sell'], title='Average Buy vs Sell Price')
55+
pie_fig = create_figure(analytics_stocks_df, 'pie', names='trade_type',
56+
values='avg_price_overall', title='Overall Average Price Distribution by Trade Type')
57+
58+
59+
histogram_fig = create_figure(
60+
stocks_df, 'bar', x='quantity', title='Stock Quantity Distribution')
61+
62+
box_plot_fig = create_figure(
63+
stocks_df, 'box', x='trade_type', y='price', title='Price Analysis by Trade Type')
64+
65+
heatmap_fig = create_figure(stocks_df, 'heatmap', x='price',
66+
y='quantity', title='Price and Quantity Correlation')
3367

34-
dcc.Graph(id='bar-chart', figure=bar_fig),
68+
time_series_fig = create_figure(pivoted_stocks_df, 'line', x='stock', y=[
69+
'avg_price_buy', 'avg_price_sell'], title='Time Series of Buy vs Sell Prices')
70+
71+
candlestick_fig = create_figure(stocks_df, 'candlestick', x='trade_date', open='open_price',
72+
high='high_price', low='low_price', close='close_price', title='Stock Price Movements')
73+
74+
treemap_fig = create_figure(analytics_stocks_df, 'treemap', path=[
75+
'trade_type', 'stock'], values='quantity', title='Treemap of Stock Distribution')
76+
77+
sunburst_fig = create_figure(grouped_stocks_df, 'sunburst', path=[
78+
'trade_type', 'stock'], values='avg_price', title='Sunburst Chart of Stock Categories')
79+
80+
violin_fig = create_figure(ranked_stocks_df, 'violin', x='trade_type',
81+
y='price', title='Violin Plot of Price Distribution')
82+
83+
app.layout = html.Div(children=[
84+
html.H1(children='RealTime StockStream 💵🕊️'),
3585
dcc.Graph(id='line-chart', figure=line_fig),
3686
dcc.Graph(id='scatter-plot', figure=scatter_fig),
3787
dcc.Graph(id='area-chart', figure=area_fig),
3888
dcc.Graph(id='pie-chart', figure=pie_fig),
39-
dcc.Graph(id='histogram', figure=histogram_fig),
40-
dcc.Graph(id='box-plot', figure=box_fig)
89+
dcc.Graph(id='histogram-chart', figure=histogram_fig),
90+
dcc.Graph(id='box-plot-chart', figure=box_plot_fig),
91+
dcc.Graph(id='heatmap-chart', figure=heatmap_fig),
92+
dcc.Graph(id='time-series-chart', figure=time_series_fig),
93+
dcc.Graph(id='candlestick-chart', figure=candlestick_fig),
94+
dcc.Graph(id='treemap-chart', figure=treemap_fig),
95+
dcc.Graph(id='sunburst-chart', figure=sunburst_fig),
96+
dcc.Graph(id='violin-chart', figure=violin_fig)
97+
# Add more graphs here as needed
4198
])
4299

43100
if __name__ == '__main__':
44-
app.run_server(debug=True, host='0.0.0.0')
101+
app.run_server(debug=True, host='0.0.0.0', port=6060)

0 commit comments

Comments
 (0)