question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Full update on Plot data change

See original GitHub issue

I am just starting to work with Plotly as a part of a create react app based project an I wanted to ask a few clarifying questions (plotly.js 1.35.1 and react-plotly.js 1.7.0)

  1. Based on all the documentation I have read and the demo application itself, one needs to eject or directly import plotly.js via a <script> tag to get it to work with create-react-app. However, I have found this latter approach (directly using the demo app) does not work any more as you get an compile error: Module not found: Can’t resolve ‘plotly.js’ when trying to run the demo app. However, I am able to get things to work in a more “standard” fashion by npm installing both plotly.js and react-plotly.js and directly importing Plot from react-plotly.js into my React component. From what I had read in some of the issues, it sounded like moving away from the need for customising the web pack config was planned for early 2018, so maybe what I am seeing is a result of that, but wanted to be sure there was not a hidden gotcha somewhere.

  2. Based on this issue #25, it sounds like the version I am using should be better about doing updated. However, I have found that updating the state of my component (which changes the data in the Plot component) via a click event when zoomed into a specific region causes the whole plot to unzoom. Am i misunderstanding the scope of this new feature (#25) or just making a rookie mistake?


onClick = (data) => {
    let x = data.points[0].x;
    let y = data.points[0].y;
   
    let {selectedPoints} = this.state;
     
    let selectedIndex = selectedPoints.x.indexOf(x)
    if(selectedIndex === -1) {
       newSelected = {
         x:selectedPoints.x.push(x),
         y:selectedPoints.y.push(y)
       }
    } else {
       newSelected = {
          x: selectedPoints.x.delete(selectedIndex),
          y: selectedPoints.y.delete(selectedIndex)
       }
    }

    this.setState({
      selectedPoints:newSelected
    })

  }

render() {
  return (
    <Plot
        data={[
          {
            type: 'scatter',
            mode: 'lines',
            x: plottedData.x,
            y: plottedData.y,
            marker: {color: 'red'}
          },
          {
            type: 'scatter',
            mode: 'markers',
            x: this.state.selectedPoints.x.toJS(),
            y: this.state.selectedPoints.y.toJS(),
            marker: {color: 'blue'}
          },
        ]}
  
        layout={{
          width: 1020,
          height: 540,
          title: 'A Fancy Plot',
          yaxis: {
            showticklabels:false
          }
        }}
        onClick={this.onClick}
        onSelected = {this.onSelect}
      />
  )
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
nicolaskruchtencommented, Mar 9, 2018

Ah I see. Well, this makes a certain amount of sense: the layout you’re passing to <Plot> doesn’t contain the zoom information, so every time React re-renders the plot, it will use the layout you gave it. You’ll likely need to subscribe to stuff like the zoom event from the <Plot> via onUpdate to capture and store edits to layout such that on the following render, that state is preserved.

Does that make sense?

1reaction
ghostcommented, Dec 25, 2018

Hey, everyone! Take a look in this solution with explanations in how to create a graph using React and Plot.ly with the life cycle of the React.

https://medium.com/@jmmccota/plotly-react-and-dynamic-data-d40c7292dbfb

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to update a plot in matplotlib - Stack Overflow
You essentially have two options: Do exactly what you're currently doing, but call graph1.clear() and graph2.clear() before replotting the data.
Read more >
Automatically Refresh Plot After Changing Data - MathWorks
Automatically Refresh Plot After Changing Data. When you plot data from workspace variables, the plots contain copies of the variables.
Read more >
How to update a plot in Matplotlib? - GeeksforGeeks
In this article, let's discuss how to update a plot in Matplotlib. Updating a plot simply means plotting the data, then clearing the ......
Read more >
Update the data in an existing chart - Microsoft Support
Learn how to update the data in an existing chart from its source. Edit a chart in Excel, create a chart from a...
Read more >
How to auto update a chart after entering new data in Excel?
This article is talking about how to auto update a chart while entering new data into the relative range in Excel.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found