javascript - add multiple node box selector on the canvas of VisJS network graph in ReactJS - Stack Overflow

ExamplesHere is a jQuery example of canvas drawing on the network to select multiple nodes: (or Githu

Examples

Here is a jQuery example of canvas drawing on the network to select multiple nodes:

  • / (or Github)

How can this be translated to React?

Sandbox

I setup react-graph-vis(source) in this sandbox, and added a ref to the Graph:

Attempts

  • having trouble adding eventLisnters to the Graph/canvas using a React.createRef() on the ''
  • having trouble using this method on the canvas: .getContext("2d")
  • I feel I don't understand how to access the react-graph-vis methods mentioned here

End Goal

  • draw box selector on left-click + mouse-drag
  • on mouseup, select nodes in the box drawn on the canvas, and clear drawing

Maybe I'm going in the wrong direction with this.

Examples

Here is a jQuery example of canvas drawing on the network to select multiple nodes:

  • http://jsfiddle/kbj54bas/ (or Github)

How can this be translated to React?

Sandbox

I setup react-graph-vis(source) in this sandbox, and added a ref to the Graph: https://codesandbox.io/s/5m2vv1yo04

Attempts

  • having trouble adding eventLisnters to the Graph/canvas using a React.createRef() on the ''
  • having trouble using this method on the canvas: .getContext("2d")
  • I feel I don't understand how to access the react-graph-vis methods mentioned here

End Goal

  • draw box selector on left-click + mouse-drag
  • on mouseup, select nodes in the box drawn on the canvas, and clear drawing

Maybe I'm going in the wrong direction with this.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Aug 15, 2018 at 16:36 Chance SmithChance Smith 1,2752 gold badges16 silver badges33 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

I threw this together using the JSSampler example you shared.

Solution

You just needed to use the ref to connect the Network and canvas. Everything else pretty much fell into place. https://codesandbox.io/s/5m2vv1yo04

Cleanup suggestions

  • move global variables into react class
  • split VisJS highlight code into own file

vis.js docs reveal that the Network object has methods exposed (http://visjs/docs/network). react-graph-vis shows that you can get access to these methods by passing in a callback to the getNetwork prop of Graph. That callback will be invoked with a reference to the underlying Network object at which point you should assign that argument to a class property as described by @Irecknagel in the Github issue.

Adding a ref to the Graph ponent as you are doing wraps the Component. However, it will not necessarily provide a reference to the Network because the Network is an object constructed by the Graph ponent.

In regards to adding event listeners, the react-graph-vis demo source code might help. They define their event listeners as an object and then pass it in as a prop like so.

// in render or elsewhere depending on scoping needs
const events = {
  select: function(event) {
    var { nodes, edges } = event;
    console.log("Selected nodes:");
    console.log(nodes);
    console.log("Selected edges:");
    console.log(edges);
  }
};
// return of render
<Graph graph={graph} options={options} events={events} style={{ height: "640px" }} />

Due to my lack of familiarity with the packages, I'm not sure what the end goal of calling .getContext("2d") is. I'm not sure if your defined 'end goals' are within the scope of the packages you're using.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744240176a4564668.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信