Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ranges does not get computed when same data is used but Plot is updated #326

Open
saadlu opened this issue Aug 12, 2023 · 0 comments
Open

Comments

@saadlu
Copy link

saadlu commented Aug 12, 2023

If layout is change but data stays the same, Plot's ranges are not computed correctly.

Consider this React component

(full project can be found here: https://github.com/saad-learns/react-plotly/tree/same_data

function App() {
  const [plotUpdated, setPlotUpdated] = React.useState<number>(0);
  const [plotParams, setPlotParams] = React.useState<PlotParams>({
    data: [
      {
        x: [-3, -2, -1, 0, 1, 2, 3],
        y: [
          -0.1411200080598672, -0.9092974268256817, -0.8414709848078965, 0,
          0.8414709848078965, 0.9092974268256817, 0.1411200080598672,
        ],
      },
    ],
    layout: {},
  });

  const updatePlot = () => {
    setPlotParams((prevState) => ({
      data: prevState.data,
      layout: {},
    }));
  };

  return (
    <div className="App">
      <div style={{ padding: 10 }}>
        <button onClick={updatePlot}>Update Plot</button>
      </div>

      <Plot
        data={plotParams.data}
        layout={plotParams.layout}
        onUpdate={() => setPlotUpdated((prevState) => prevState + 1)}
      />

      <div>Plot updated: {plotUpdated}</div>
    </div>
  );

At the start, ranges are calculated correctly. You can verify that with React Dev Tool's Component tab:

image

Or you can use Zoom In and Zoom Out button to verify that zooming is working.

But if you click on the Update Plot button, the ranges are not computed, in fact they get removed:

image

Thereafter, zooming does not work.

Here, updatePlot will use the same data object, but makes use of a new layout. According to https://github.com/plotly/react-plotly.js#refreshing-the-plot Plot should be refreshed, and it does, but ranges are no longer computed correctly.

There are other variation of the above issue:

  1. Use a new array but same previous data element: https://github.com/saad-learns/react-plotly/blob/new_array/src/App.tsx
  2. Use a new array, and a new element but with same previous data: https://github.com/saad-learns/react-plotly/blob/new_object/src/App.tsx

where ranges are not computed properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant