Skip to content

X-axis type lost after restyle #7225

Open
@tomwhoiscontrary

Description

@tomwhoiscontrary

If i create a bar chart with a categorical x-axis, where the x-values are strings of digits, then restyle it with new y-values, the categoricality of the x-axis is lost, and it becomes linear, as if the type had been freshly inferred from the values.

Here is a self-contained example (i'm afraid i don't have a codepen account):

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Restyle</title>
        <script src="https://cdn.plot.ly/plotly-latest.js"></script>
        <script>
            addEventListener("DOMContentLoaded", () => {
                const plot = document.getElementById("plot");

                Plotly.newPlot(plot,
                               {
                                   data: [{
                                       type: "bar",
                                       x: ["2", "5", "10", "30", "50"],
                                       y: [1, 2, 3, 4, 5]
                                   }],
                                   layout: {
                                       xaxis: {type: "category"}
                                   },
                                   config: {
                                       responsive: true
                                   }
                               });

                document.getElementById("restyle").addEventListener("click", () => {
                    Plotly.restyle(plot, {y: [[5, 4, 3, 2, 1]]}, 0);
                });
            });
        </script>
    </head>
    <body>
        <div id="plot"></div>
        <button id="restyle">Restyle</button>
    </body>
</html>

When the page loads, the x-axis is correctly categorical, but when you click the button, it becomes incorrectly linear.

I believe this is the same problem as described in the forum post "X-axis “category” type not honored after Plotly.restyle".

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3backlogbugsomething broken

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions