|
2 | 2 | from dash import html, dcc |
3 | 3 | import plotly.express as px |
4 | 4 | import pandas as pd |
| 5 | +from cassandra.cluster import Cluster |
5 | 6 |
|
6 | 7 | app = dash.Dash(__name__) |
7 | 8 |
|
8 | 9 |
|
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 |
11 | 14 |
|
12 | 15 |
|
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 |
14 | 22 |
|
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') |
18 | 23 |
|
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) |
21 | 39 |
|
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') |
30 | 40 |
|
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') |
33 | 67 |
|
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 💵🕊️'), |
35 | 85 | dcc.Graph(id='line-chart', figure=line_fig), |
36 | 86 | dcc.Graph(id='scatter-plot', figure=scatter_fig), |
37 | 87 | dcc.Graph(id='area-chart', figure=area_fig), |
38 | 88 | 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 |
41 | 98 | ]) |
42 | 99 |
|
43 | 100 | 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