如何在直方图的回调被触发时保持选定的图例?

huangapple go评论77阅读模式
英文:

Dash: how to maintain selected legends when callback is triggered on histogram

问题

我正在使用Dash开发一个仪表板应用程序。我有一个回调函数,用于更改直方图的xbins大小,但当触发回调时,选定的图例会被重置(即之前取消选择的类别不再被选中)。

我想知道在触发回调时是否有一种方法可以保持先前选择的图例。

def make_histogram(df, xbin_type):
    now = datetime.now()
    tmr = now + timedelta(days=1)
    fig = go.Figure()
    for name in df['field'].unique():
        df_of_table = df.loc[df['field'] == name]
        fig.add_trace(go.Histogram(name=name, x=df_of_table['timestamp'], 
                                    xbins=dict(
                                        start='2022-01-01',
                                        end=tmr.strftime("%Y-%m-%d"),
                                        size=xbin_type
                                    )))
    fig.update_layout(xaxis_title_text='Date', # x轴标签
                    yaxis_title_text='Count', # y轴标签
                    bargap=0.2, barmode='stack')
    return fig

@callback(
    Output('change-log-histogram', 'figure'),
    Input('xbin-type', 'value')
)
def update_xbin(xbin_type):
    if xbin_type == 'W1':
        return make_histogram(df, 604_800_000)
    return make_histogram(df, xbin_type)

我无法找到有关如何访问go.Figure并检索当前选择的迹线/类别/图例(不确定哪个术语是正确的)的任何信息。

英文:

I'm working on a dashboard app using Dash. I have a callback that changes the xbins size of a histogram but when the callback is fired, the selected legends are reset (ie no previously de-selected categories are selected again).

I'm wondering if there is a way to keep the previously selected legends when the callback is triggered.


def make_histogram(df, xbin_type):
    now = datetime.now()
    tmr = now + timedelta(days=1)
    fig = go.Figure()
    for name in df['field'].unique():
        df_of_table = df.loc[df['field'] == name]
        fig.add_trace(go.Histogram(name=name, x=df_of_table['timestamp'], 
                                    xbins=dict(
                                        start='2022-01-01',
                                        end=tmr.strftime("%Y-%m-%d"),
                                        size=xbin_type
                                    )))
    fig.update_layout(xaxis_title_text='Date', # xaxis label
                    yaxis_title_text='Count', # yaxis label
                    bargap=0.2, barmode='stack')
    return fig

@callback(
    Output('change-log-histogram', 'figure'),
    Input('xbin-type', 'value')
)
def update_xbin(xbin_type):
    if xbin_type == 'W1':
        return make_histogram(df, 604_800_000)
    return make_histogram(df, xbin_type)

I cannot find any information about reaching into go.Figure and retrieve the currently selected trace/category/legend (not sure what's the right lingo)

答案1

得分: 0

为了"保留先前选择的图例"或保留图形的任何当前属性,您需要将图形添加为回调定义中的State(请参阅Basic Dash Callbacks文档中的"Dash App With State"部分)。

State允许您传递额外的值,而不触发回调。

例如:

@callback(
    Output('change-log-histogram', 'figure'),
    Input('xbin-type', 'value'),
    State('change-log-histogram', 'figure'),
)
def update_xbin(xbin_type, current_figure):
    # ...

这样,您可以在回调函数内访问图形的当前"状态"。您可以知道:

  • 创建一个全新的图形,并从当前图形复制/粘贴一些属性(例如可见的跟踪),或者
  • 如果更容易,只需更新当前图形并返回它。

State在需要更新交互式组件而不完全覆盖它时特别有用。

英文:

In order to "keep the previously selected legends", or to keep any current properties of the figure, you need to add the figure as a State in the callback definition (see the "Dash App With State" section of the Basic Dash Callbacks doc)

> State allows you to pass along extra values without firing the callbacks.

, something like:

@callback(
    Output('change-log-histogram', 'figure'),
    Input('xbin-type', 'value'),
    State('change-log-histogram', 'figure'),
)
def update_xbin(xbin_type, current_figure):
    # ...

That way, you have access to the figure's current "state" inside the callback function. You can know

  • create a completely new figure and copy/paste some properties from the current figure to this new one (e.g which traces are visible), or
  • if it's easier, simply update the current figure and return it.

State is particularly useful when one needs to update an interactive component without completely overwriting it.

huangapple
  • 本文由 发表于 2023年7月18日 07:37:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定