plotly / plotly.js

Open-source JavaScript charting library behind Plotly and Dash
https://plotly.com/javascript/
MIT License
17k stars 1.86k forks source link

Button update does work when selecting specific traces #6301

Closed gilbertovilarunc closed 3 months ago

gilbertovilarunc commented 2 years ago

Thanks for your interest in plotly.js!

Before opening an issue, please search for existing and closed issues. If your problem or idea is not addressed yet, please open a new issue.

Bug reports must be accompanied with a reproducible example. We recommend using codepen, jsfiddle or jsbin to share your example. Please use the latest un-minified version of plotly.js in your report unless not applicable.

If you don't know JavaScript and still want to help us by reporting a bug, please attach the "data" and "layout" attributes that describe your graph and updates (if required to detect the bug). One way to retrieve your graph's data and layout attributes is by exporting your graph to Plotly Cloud. To do so, click on the Edit in Chart Studio mode bar button (the 2nd one from the left by default) and follow these instructions, or watch this screencast.

Issues found on the example pages from https://plot.ly/javascript/ should be filed in our documentation repo with the exception of https://plot.ly/javascript/reference which should be filed here.

Note that GitHub issues are reserved for bug reports and feature requests only. Implementation questions should be asked on community.plot.ly (tagged plotly-js) or on Stack Overflow (tagged plotly).

Comments on GitHub issues or pull requests should add content to the discussions. Approbation comments such as +1 or I would like this feature to be implemented as well will be deleted by the maintainers. Please use GitHub reactions instead.

Similar issue reported here: https://chart-studio.plotly.com/~empet/15569/updatemenus-bug-when-a-single-trace-is/#/

My example here:

import pandas as pd
import numpy as np

def data_sample():
    start_date = pd.Timestamp("2020-01-01")
    end_date = pd.Timestamp("2021-01-31")
    index = pd.date_range(start_date, end_date, freq="D")
    df = pd.DataFrame(index=index)
    df["x1"] = (np.random.randn(len(index)) + 1) * 10000
    df["x2"] = (np.random.randn(len(index)) + 1) * 20000
    df["x3"] = (np.random.randn(len(index)) + 1) * 30
    df["x4"] = (np.random.randn(len(index)) + 1) * 40
    df["x5"] = (np.random.randn(len(index)) + 1) * 500
    df['y'] = np.random.randn(len(index)) +\
        5*df["x1"] + 10*df["x2"] + 20*df["x3"] + 30*df["x4"] - 40*df["x5"]
    return df.clip(lower=0)
ts_data = data_sample().iloc[:10]

from plotly.subplots import make_subplots
fig_go = make_subplots(rows=2, cols=1, shared_xaxes=True)
for col in ts_data:
    fig_go.add_trace(
        go.Bar(
            x=ts_data.index, y=ts_data[col], name=col
        ),
        row=1,
        col=1
    )
for col in ts_data:
    fig_go.add_trace(
        go.Bar(
            x=ts_data.index, y=ts_data[col], name=col
        ),
        row=2,
        col=1
    )

my_button_type = [
    {
        'label': "Bar",
        'method': "update",
        'args': [{"type": "bar"}, {}, [0, 1, 2, 3, 4, 5]]
    },
    {
        'label': "Scatter",
        'method': "update",
        'args': [{"type": "scatter", 'mode': 'markers'}, {}, [0, 1, 2, 3, 4, 5]]
    },
    {
        'label': "Line",
        'method': "update",
        'args': [{"type": "scatter", "mode": "lines"}, {}, [0, 1, 2, 3, 4, 5]]
    }
]
my_button_stack = [
    {
        'label': "Non-stacked",
        'method': "update",
        'args': [{"fill": None, "stackgroup": None}, {"barmode": "group"}, [0, 1, 2, 3, 4, 5]]
    },
    {
        'label': "Stacked",
        'method': "update",
        'args': [{'fill': 'tozeroy', "stackgroup": 1}, {"barmode": "relative"}, [0, 1, 2, 3, 4, 5]]
    }
]
my_button_pct = [
    {
        'label': "Absolute",
        'method': "update",
        'args': [{"visible": abs_visible}, {}, [0, 1, 2, 3, 4, 5]],
    },
    {
        'label': "Percentage",
        'method': "update",
        'args': [{"visible": pct_visible}, {}, [0, 1, 2, 3, 4, 5]]
    }
]

fig_go.update_layout({
        'updatemenus': [
            {
                'type': "dropdown",
                'direction': 'down',
                'x': 0.1,
                'y': 1.15,
                'showactive': True,
                'active': 0,
                'buttons': my_button_type
            },
            {
                'type': "dropdown",
                'direction': 'down',
                'x': 0.25,
                'y': 1.15,
                'showactive': True,
                'active': 0,
                'buttons': my_button_stack
            },
            {
                'type': "dropdown",
                'direction': 'down',
                'x': 0.4,
                'y': 1.15,
                'showactive': True,
                'active': 0,
                'buttons': my_button_pct
            }
    ]
})
fig_go.update_layout(hovermode='x unified')
fig_go.show()

The problem seems to be only on tha "Stack/Non-stacked" dropdown. Maybe because its the only one that changes both data and layout of the traces.

gvwilson commented 3 months ago

Hi - we are trying to tidy up the stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for several years, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. Thanks for your help - @gvwilson