From 3bb629713406119c2ed0dba185ffc63df83c9077 Mon Sep 17 00:00:00 2001 From: wonhochoi1 Date: Fri, 1 Mar 2024 13:20:43 -0500 Subject: [PATCH] Implemented Custom Events Graph on Main Page Unconditionally --- dashboard/app.py | 7 ++++- dashboard/widgets/click_event_widgets.py | 10 +++--- dashboard/widgets/custom_event_graphs.py | 39 ++++++++++++++++++++++++ dashboard/widgets/visit_event_widgets.py | 3 +- 4 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 dashboard/widgets/custom_event_graphs.py diff --git a/dashboard/app.py b/dashboard/app.py index 9cfe29e..6401673 100644 --- a/dashboard/app.py +++ b/dashboard/app.py @@ -18,7 +18,7 @@ init_object_active_users_bar_graph, ) from widgets.input_event_widgets import init_input_object_frequency_graph - +from widgets.custom_event_graphs import init_plot_custom_graphs from utils import EventTypes st.title("Analytics Dashboard") @@ -53,3 +53,8 @@ st.header("Input Events") init_input_object_frequency_graph(st, input_events) pass + + +custom_charts = init_plot_custom_graphs(custom_events, custom_graphs) +for chart in custom_charts: + st.altair_chart(chart, use_container_width=True) \ No newline at end of file diff --git a/dashboard/widgets/click_event_widgets.py b/dashboard/widgets/click_event_widgets.py index eaf7965..05339a2 100644 --- a/dashboard/widgets/click_event_widgets.py +++ b/dashboard/widgets/click_event_widgets.py @@ -14,7 +14,7 @@ def init_object_click_bar_graph(st, click_events): list(object_clicks.items()), columns=["Object Id", "Clicks"] ) - # Create the bar chart + chart = ( alt.Chart(df_visits) .mark_bar() @@ -25,7 +25,7 @@ def init_object_click_bar_graph(st, click_events): .properties(width=600, height=400) ) - # Display the chart using Streamlit + st.altair_chart(chart, use_container_width=True) @@ -45,9 +45,9 @@ def init_object_active_users_bar_graph(st, click_events): pd.DataFrame(list(clicks_data.items()), columns=["User ID", "Clicks"]) .sort_values(by="Clicks", ascending=False) .head(5) - ) # Focus on top 5 users + ) - # Create the bar chart + chart = ( alt.Chart(df_clicks) .mark_bar() @@ -58,5 +58,5 @@ def init_object_active_users_bar_graph(st, click_events): .properties(width=600, height=400) ) - # Display the chart using Streamlit + st.altair_chart(chart, use_container_width=True) diff --git a/dashboard/widgets/custom_event_graphs.py b/dashboard/widgets/custom_event_graphs.py new file mode 100644 index 0000000..48e421b --- /dev/null +++ b/dashboard/widgets/custom_event_graphs.py @@ -0,0 +1,39 @@ +import streamlit as st +import pandas as pd +import altair as alt +from data import visit_events, click_events, input_events, custom_events, custom_graphs + + + +def init_plot_custom_graphs(custom_events, custom_graphs): + charts = [] + for graph in custom_graphs: + matching_events = [ + event for event in custom_events + if event.category == graph.category and event.subcategory == graph.subcategory + ] + + data = { + "x": [event.event_properties[graph.xProperty] for event in matching_events], + "y": [event.event_properties[graph.yProperty] for event in matching_events], + } + df = pd.DataFrame(data) + + base = alt.Chart(df).encode( + x=alt.X('x:Q', axis=alt.Axis(title=graph.xProperty)), + y=alt.Y('y:Q', axis=alt.Axis(title=graph.yProperty)) + ).properties( + title=f"Event Category: {graph.category} - Subcategory: {graph.subcategory}" + ) + + if graph.graphType == "bar": + chart = base.mark_bar().encode( + x=alt.X('x:N', axis=alt.Axis(title=graph.xProperty))) + elif graph.graphType == "line": + chart = base.mark_line() + elif graph.graphType == "scatter": + chart = base.mark_circle() + + charts.append(chart) + + return charts \ No newline at end of file diff --git a/dashboard/widgets/visit_event_widgets.py b/dashboard/widgets/visit_event_widgets.py index 034be23..5806dc1 100644 --- a/dashboard/widgets/visit_event_widgets.py +++ b/dashboard/widgets/visit_event_widgets.py @@ -80,7 +80,6 @@ def init_page_active_users_graph(st, visit_events): .head(5) ) - # Create the bar chart chart = ( alt.Chart(df_user_visits) .mark_bar() @@ -91,7 +90,7 @@ def init_page_active_users_graph(st, visit_events): .properties(width=600, height=400) ) - # Display the chart using Streamlit + st.altair_chart(chart, use_container_width=True)