Skip to content

Commit

Permalink
Adjust hover client side
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoSilvaGomes committed Nov 21, 2024
1 parent d904bdb commit 3c63d11
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
8 changes: 7 additions & 1 deletion painel/callbacks/callbacks_nao_programaticos.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@ def gera_big_numbers(tipo, json, populacao, nivel_geo, ano):
else:
values = [None, None]

return values[0], values[1], total
if values[1]:
return values[0], values[1], total

if values[0]:
return values[0], "", total

return "", "", total


def register_callbacks_nao_programaticos(app):
Expand Down
8 changes: 7 additions & 1 deletion painel/callbacks/callbacks_programaticos.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ def gera_big_numbers(tipo, json, populacao, nivel_geo, ano):
hist_gravidez = store_nivel(hist_gravidez, df, None, nivel_geo, anos)
values = get_values(hist_gravidez, ano, nivel_geo, "mean")

if values[1]:
if values[1] and values[0]:
return f"{int(values[0]*100)} %", f"{int(values[1]*100)} %", f"{total} %"

if values[0]:
return f"{int(values[0]*100)} %", "", f"{total} %"

if values[1]:
return "", f"{int(values[1]*100)} %", f"{total} %"

return "", "", f"{total} %"

df = get_df_from_json(json)
Expand Down Expand Up @@ -107,6 +110,9 @@ def gera_big_numbers(tipo, json, populacao, nivel_geo, ano):
else:
return None, None, None

print("values-------------------")
print(values)

if values[1]:
return values[0], values[1], total

Expand Down
33 changes: 15 additions & 18 deletions painel/callbacks/hover_chart_callback.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
"""Função para registrar os callbacks de hover nos gráficos"""

from dash import Input, Output, State
from dash import Input, Output, State, clientside_callback


def callback(app):
"""Função para registrar os callbacks de hover nos gráficos"""

def hover_event_template(id_chart):
"""Função para criar o callback de hover nos gráficos"""

clientside_callback(
"""
function(hoverData, currentFigure) {
var figureChart = JSON.parse(JSON.stringify(currentFigure));
if (!hoverData || !hoverData.points || hoverData.points.length === 0) {
figureChart.data[0].selectedpoints = null;
} else {
const hoverPoint = hoverData.points[0].pointIndex;
figureChart.data[0].selectedpoints = [hoverPoint];
}
@app.callback(
return figureChart
}
""",
Output(id_chart, "figure", allow_duplicate=True),
Output("loading-graphics", "display", allow_duplicate=True),
Input(id_chart, "hoverData"),
[State(id_chart, "figure")],
allow_duplicate=True,
prevent_initial_call=True,
)
def callback(data, current_figure):
traces = current_figure["data"]

if data:
hover_point = data["points"][0]["pointIndex"]

for idx, trace in enumerate(traces):
trace.update({"selectedpoints": [hover_point]})
current_figure["data"][idx].update(trace)
else:
for idx, trace in enumerate(traces):
trace.update({"selectedpoints": None})
current_figure["data"][idx].update(trace)

return current_figure, "hide"

hover_event_template("mapa")
hover_event_template("chart_by_year")
Expand Down
21 changes: 16 additions & 5 deletions painel/callbacks/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,13 @@ def get_values(hist, ano, nivel, calculo="sum"):
return [None, None]
values = []

for nivel_hist in hist.keys():
niveis_mapped = hist.keys()

for nivel_hist in niveis_mapped:
if nivel == nivel_hist:
continue
if nivel_hist != "brasil" and nivel_hist != "estado":
continue
df = hist[nivel_hist]
if calculo == "sum":
values.append(round(df[df["ano"] == ano]["valor"].sum()))
Expand All @@ -181,9 +185,16 @@ def get_values(hist, ano, nivel, calculo="sum"):
2,
)
)


print("values11--------------------")
print(values)

if not values:
return [None, None]
if nivel == "estado":
return [values[0], None]
return values
if len(values) == 1:
if "brasil" in niveis_mapped:
return [values[0], None]
if "estado" in niveis_mapped:
return [None, values[0]]

return values

0 comments on commit 3c63d11

Please sign in to comment.