Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1235-Dashboard-add radio buttons w moving average breakdown by wk and mo, normalize by population per 10k people and update labels #1242

Closed
wants to merge 13 commits into from
Closed
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/311-data.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 63 additions & 29 deletions server/dash/dashboards/neighborhood.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
# TITLE
title = "NEIGHBORHOODS"


# DATA
print(" * Downloading data for dataframe")
query_string = '/reports?field=type_name&field=council_name&field=created_date'
df = pd.read_json(API_HOST + query_string)
print(" * Dataframe has been loaded")

df["created_date"] = pd.to_datetime(df['created_date'])

fig = px.line()
apply_figure_style(fig)

Expand All @@ -39,27 +42,80 @@ def populate_options():
)
return values

print(populate_options())

layout = html.Div([
html.H1(title),
dcc.Dropdown(
id='council_list',
clearable=False,
value="Arleta",
placeholder="Select a neighborhood",
options=populate_options()
),
dcc.Dropdown(
id='data_type',
clearable=False,
placeholder="Select a timeframe",
options=[
{'label': 'Compare to: Daily Council Average', 'value': 'nc_avg'},
{'label': 'Compare to: Weekly Council Average', 'value': 'nc_avg_wk'},
{'label': 'Compare to: Monthly Council Average', 'value': 'nc_avg_Mo'},
],

value='nc_avg'
),
dcc.Graph(
id='graph1',
figure=fig,
config=CONFIG_OPTIONS
),

dcc.Graph(
id='graph2',
figure=fig,
config=CONFIG_OPTIONS
)
])


# Define callback to update graph
@app.callback(
Output('graph1', 'figure'),
[Input("council_list", "value")]
[Input("council_list", "value"),
Input("data_type", "value")]
)
def update_figure(selected_council):

def update_figure(selected_council, select_timeframe):
neighborhood_sum_df = df[df.council_name == selected_council].groupby(['created_date']).agg('sum').reset_index() # noqa

total_sum_df = df.groupby(['created_date']).agg('sum').reset_index()

total_sum_df["nc_avg"] = total_sum_df["counts"] / 99
merged_df = neighborhood_sum_df.merge(total_sum_df["nc_avg"].to_frame(), left_index=True, right_index=True) # noqa

total_sum_df["week_n"] = total_sum_df['created_date'].apply(lambda x: str(x.isocalendar()[1]))
total_sum_df["month_n"] = total_sum_df['created_date'].dt.month

total_sum_df['nc_avg_Mo'] = total_sum_df.groupby(['month_n'])['counts'].transform('mean')
total_sum_df['nc_avg_Mo'] = total_sum_df['nc_avg_Mo'] / 99
total_sum_df['nc_avg_wk'] = total_sum_df.groupby(['week_n'])['counts'].transform('mean')
total_sum_df['nc_avg_wk'] = total_sum_df['nc_avg_wk'] / 99
merged_df = pd.merge(neighborhood_sum_df, total_sum_df, on=["created_date", "created_date"])



fig = px.line(
merged_df,
x="created_date",
y=['counts', 'nc_avg'],
y=['counts_x', select_timeframe],
color_discrete_sequence=DISCRETE_COLORS,
labels=LABELS,
title="Comparison trend for " + selected_council
)
newnames = {"counts_x": "Count of 311 Requests", "nc_avg_Mo": "Monthly Council Average", "nc_avg_wk": "Weekly Council Average", "nc_avg": "Daily Council Average"}
fig.for_each_trace(lambda t: t.update(name = newnames[t.name],
legendgroup = newnames[t.name],
hovertemplate = t.hovertemplate.replace(t.name, newnames[t.name])
)
)

fig.update_xaxes(
tickformat="%a\n%m/%d",
Expand Down Expand Up @@ -104,26 +160,4 @@ def update_council_figure(selected_council):

apply_figure_style(fig)

return fig


layout = html.Div([
html.H1(title),
dcc.Dropdown(
id='council_list',
clearable=False,
value="Arleta",
placeholder="Select a neighborhood",
options=populate_options()
),
dcc.Graph(
id='graph1',
figure=fig,
config=CONFIG_OPTIONS
),
dcc.Graph(
id='graph2',
figure=fig,
config=CONFIG_OPTIONS
)
])
return fig