Hello everyone, with this example code I ask someone to help me make the interaction between the chart, the table and the indicator.
import sqlalchemy as sa
from sqlalchemy import asc, create_engine
import fdb
import pandas as pd
import plotly
import plotly.graph_objects as go
import dash
from dash import Dash, dcc, html, Input, Output, no_update
import dash_bootstrap_components as dbc
filb = ['fil1','fil2','fil3']
estoque = [10,20,25]
tupla_estoque = list(zip(filb,estoque))
df_estoque = pd.DataFrame(tupla_estoque, columns=['fil','estoque'])
filc = ['fil1','fil2','fil3']
cli = [15,30,25]
tupla_cli = list(zip(filc,cli))
df_cli = pd.DataFrame(tupla_estoque, columns=['fil','cli'])
fig_estoque = go.Figure(data=[
go.Bar(x=df_estoque['fil'], y=df_estoque['estoque'],
text=df_estoque['estoque'],
textposition='auto',
textfont_color='white',
marker_color='#00B0FF',
)
])
fig_estoque.update_traces(hoverinfo="none", hovertemplate=None)
fig_estoque.update_layout(title='Estoque por Filial Lily Belle',title_y=0.85, title_x=0.1, title_font_color='white', paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')
fig_estoque.update_yaxes(showticklabels=False, showgrid=False)
fig_estoque.update_xaxes(tickfont_color='#ffffff', showgrid=False)
fig_table = go.Figure(data=[go.Table(
header=dict(values=['Filial', 'Clientes novos c/ compra'],
line_color='darkslategray',
fill_color='lightskyblue',
align='left'),
cells=dict(values=[df_cli['fil'], # 1st column
df_cli['cli']], # 2nd column
line_color='darkslategray',
fill_color='lightcyan',
align='left'))
])
fig_table.update_layout( paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')
fig_new_clients = go.Figure(data=[go.Indicator(
mode = "number",
value = df_cli['cli'].sum(),
title = {'text': "Novos clientes",'font':{'size': 20}},
number= {'font': {'size': 18}}
)])
fig_new_clients.update_layout ( showlegend = False, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='white', font={'color':'white'})
app = Dash(__name__,external_stylesheets=[dbc.themes.SLATE])
app.layout = html.Div([
html.H1('Dashboard interação', id='h1'),
dbc.Col([
dbc.Row([
dbc.Col(dcc.Graph(id="graph", figure=fig_estoque, clear_on_unhover=True,style={'width':'75vh','margin-right':'20px'} ),lg=4)
]),
]),
dbc.Row(dcc.Graph(id='cli_novo',style={'height':'10vh'},figure=fig_new_clients)),
dbc.Row(dcc.Graph(id='tabela_clientes_compra',style={'width':'65vh', 'height':'35vh'},figure=fig_table))
], style={'margin-top':'55px'})
if __name__ == "__main__":
app.run_server(debug=True)
For example, when clicking on one of the bars, the indicator shows new customers only from the branch of the respective bar and the table as well, and if you click on the information in the table, the same effect occurs on the bar chart and the indicator