Skip to content

Commit

Permalink
small fixes on charts
Browse files Browse the repository at this point in the history
  • Loading branch information
kauevestena committed Oct 14, 2024
1 parent d336ca4 commit 87ecc0a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 32 deletions.
40 changes: 33 additions & 7 deletions dashboard/statistics_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,39 +74,65 @@ def create_barchartV2(
input_gdf,
fieldname,
title,
filter_out_opt="?", # value to filter out
filter_out_opt_text='Include "?" (Unknown)',
str_to_append=" type",
title_fontsize=24,
len_field="length(km)",
):
import altair as alt

# bind = alt.selection_interval(bind='scales')
# .add_selection(bind)

# Create a modified fieldname for plotting
fieldname_v2 = fieldname + str_to_append

# add a dummy "count" if no len_field
if not len_field:
input_gdf["count"] = 1
len_field = "count"

# Define the fields to plot
fields_to_plot = [fieldname, len_field]

# aggregation fields:
agg_fields = {fieldname: "count", len_field: "sum"}

# Aggregate the data for plotting
data_to_plot = (
input_gdf[[len_field, fieldname]]
input_gdf[fields_to_plot]
.groupby([fieldname])
.agg({fieldname: "count", len_field: "sum"})
.agg(agg_fields)
.rename(columns={fieldname: "feature count"})
.reset_index()
.rename(columns={fieldname: fieldname_v2})
)

return (
# Create an interactive selection for filtering (boolean checkbox)
filter_checkbox = alt.binding_checkbox(name=filter_out_opt_text)
selection = alt.param(
name="include_filter_out_opt", bind=filter_checkbox, value=True
)

# Create the bar chart with conditional filtering
chart = (
alt.Chart(data_to_plot, title=title)
.mark_bar()
.encode(
x=alt.X(fieldname_v2, sort="-y"),
y=len_field,
tooltip=len_field,
tooltip=[fieldname_v2, len_field, "feature count"],
color="feature count",
)
.properties(width=650, height=300)
.configure_title(fontSize=title_fontsize)
.add_params(selection)
.transform_filter(
f"include_filter_out_opt || datum['{fieldname_v2}'] != '{filter_out_opt}'"
)
.interactive()
)

return chart


def print_relevant_columnames(
input_df, not_include=("score", "geometry", "type", "id")
Expand Down
2 changes: 1 addition & 1 deletion dashboard/statistics_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@

category_homepage = get_url(generated_list_dict[category][0])

topbar += f'<a href="{category_homepage}">{category.capitalize()} Charts</a>\n'
topbar += f'<a href="{category_homepage}">{category.replace("_", " ").title()} Charts</a>\n'


topbar += """
Expand Down
92 changes: 68 additions & 24 deletions dashboard/statistics_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"yh2": "smoothness",
"hcolor": "length(km)",
"fontsize": 24,
"tooltip_fields": ["element_type", "id"],
"tooltip_fields": ["id"],
},
"title": "Surface x Smoothness",
},
Expand Down Expand Up @@ -92,7 +92,7 @@
"df": gdfs_dict["sidewalks"],
"column": "age",
"boxplot_title": "Sidewalks Age",
"tooltip_fields": ["element_type", "id"],
"tooltip_fields": ["id"],
},
"title": "Sidewalks Age",
},
Expand All @@ -102,28 +102,38 @@
"df": gdfs_dict["sidewalks"],
"column": "length(km)",
"boxplot_title": "Sidewalks Length (km)",
"tooltip_fields": ["element_type", "id"],
"tooltip_fields": ["id"],
},
"title": "Sidewalks Length (km)",
},
"sidewalks_yr_moth_update": {
"function": create_barchart,
"sidewalks_n_revs": {
"function": create_linked_boxplot_histogram,
"params": {
"input_df": updating_dicts["sidewalks"],
"fieldname": "year_month",
"title": "Year and Month Of Update (Sidewalks)",
"df": gdfs_dict["sidewalks"],
"column": "n_revs",
"boxplot_title": "Sidewalks Number of Revisions",
"tooltip_fields": ["id"],
},
"title": "Year and Month Of Update",
"title": "Number of Revisions",
},
"sidewalks_number_revisions": {
"sidewalks_yr_moth_update": {
"function": create_barchart,
"params": {
"input_df": updating_dicts["sidewalks"],
"fieldname": "n_revs",
"fieldname": "year_month",
"title": "Year and Month Of Update (Sidewalks)",
},
"title": "Number Of Revisions",
"title": "Year and Month Of Update",
},
# "sidewalks_number_revisions": {
# "function": create_barchart,
# "params": {
# "input_df": updating_dicts["sidewalks"],
# "fieldname": "n_revs",
# "title": "Number of Revisions (Sidewalks)",
# },
# "title": "Number Of Revisions",
# },
},
"crossings": {
"crossing_types": {
Expand All @@ -135,14 +145,25 @@
},
"title": "Crossing Type",
},
# "crossing_surface": {
# "function": create_barchart,
# "params": {
# "input_df": gdfs_dict["crossings"],
# "fieldname": "surface",
# "title": "Crossing Surface",
# },
# "title": "Crossing Surface",
# },
"crossing_surface": {
"function": create_barchart,
"function": create_barchartV2,
"params": {
"input_df": gdfs_dict["crossings"],
"input_gdf": gdfs_dict["crossings"],
"fieldname": "surface",
"title": "Crossing Surface",
"title": "Crossings Surface Type",
"str_to_append": " type",
"title_fontsize": 24,
},
"title": "Crossing Surface",
"title": "Surface Type",
},
"crossings_smoothness_x_surface": {
"function": create_double_scatter_bar,
Expand All @@ -157,7 +178,7 @@
"yh2": "smoothness",
"hcolor": "crossing",
"fontsize": 24,
"tooltip_fields": ["element_type", "id"],
"tooltip_fields": ["id"],
},
"title": "Surface x Smoothness",
},
Expand All @@ -175,7 +196,7 @@
"params": {
"input_df": updating_dicts["crossings"],
"fieldname": "n_revs",
"title": "Year and Month Of Update (crossings)",
"title": "Number of Revisions (crossings)",
},
"title": "Number Of Revisions",
},
Expand All @@ -194,7 +215,7 @@
"yh2": "tactile_paving",
"hcolor": "wheelchair",
"fontsize": 24,
"tooltip_fields": ["element_type", "id"],
"tooltip_fields": ["id"],
},
"title": "Surface x Smoothness",
},
Expand Down Expand Up @@ -225,6 +246,18 @@
},
"title": "Wheelchair Acessibility",
},
"kerbs_surface": {
"function": create_barchartV2,
"params": {
"input_gdf": gdfs_dict["kerbs"],
"fieldname": "surface",
"title": "Kerbs Surface Type",
"str_to_append": " type",
"title_fontsize": 24,
"len_field": None,
},
"title": "Surface Type",
},
"kerbs_yr_moth_update": {
"function": create_barchart,
"params": {
Expand Down Expand Up @@ -254,14 +287,25 @@
},
"title": "Incline Values",
},
# "other_footways_surface": {
# "function": create_barchart,
# "params": {
# "input_df": gdfs_dict["other_footways"],
# "fieldname": "surface",
# "title": "other_footways Surface",
# },
# "title": "Other Footways Surface",
# },
"other_footways_surface": {
"function": create_barchart,
"function": create_barchartV2,
"params": {
"input_df": gdfs_dict["other_footways"],
"input_gdf": gdfs_dict["other_footways"],
"fieldname": "surface",
"title": "other_footways Surface",
"title": "Other Footways Surface Type",
"str_to_append": " type",
"title_fontsize": 24,
},
"title": "Other Footways Surface",
"title": "Surface Type",
},
"other_footways_smoothness_x_surface": {
"function": create_double_scatter_bar,
Expand All @@ -276,7 +320,7 @@
"yh2": "smoothness",
"hcolor": "crossing",
"fontsize": 24,
"tooltip_fields": ["element_type", "id"],
"tooltip_fields": ["id"],
},
"title": "Surface x Smoothness",
},
Expand Down

0 comments on commit 87ecc0a

Please sign in to comment.