opt*_*nal 5 javascript reactjs vis.js
如何将vis.js网络渲染到具体的html容器中?
我尝试了以下内容,但它不起作用:
<div id="network">
{new vis.Network("network", data, options)}
</div>
Run Code Online (Sandbox Code Playgroud)
或者我应该按照以下方式呈现它?
ReactDOM.render(<App />, document.getElementById('app'));
ReactDOM.render(<Network />, document.getElementById('network'));
Run Code Online (Sandbox Code Playgroud)
虽然元素"app"和"network"位于包装器html容器中.
我希望将vis.js网络呈现到根文档中的解决方案:
ReactDOM.render(<App />, document.getElementById('root'));
Run Code Online (Sandbox Code Playgroud)
干杯!
Gin*_*ena 12
这对我有用:
var nodes = new vis.DataSet([
{id: 1, label: 'Node 1'},
{id: 2, label: 'Node 2'},
{id: 3, label: 'Node 3'},
{id: 4, label: 'Node 4'},
{id: 5, label: 'Node 5'}
]);
// create an array with edges
var edges = new vis.DataSet([
{from: 1, to: 3},
{from: 1, to: 2},
{from: 2, to: 4},
{from: 2, to: 5}
]);
var data = {
nodes: nodes,
edges: edges
};
var options = {};
// initialize your network!
var VisNetwork = React.createClass({
componentDidMount(){
var network = new vis.Network(this.refs.myRef, data, options);
},
render: function() {
return <div ref="myRef"></div>;
}
});
ReactDOM.render(
<VisNetwork />,
document.getElementById('container')
);
Run Code Online (Sandbox Code Playgroud)
https://jsfiddle.net/ginollerena/69z2wepo/63263/
pra*_*ann 10
从 React 16.3 开始,推荐使用React.createRef Docs
用 16.3 风格更新@Gino 的答案。
import { DataSet, Network } from 'vis';
import React, { Component, createRef } from "react";
const nodes = new DataSet([
{ id: 1, label: 'Node 1' },
{ id: 2, label: 'Node 2' },
{ id: 3, label: 'Node 3' },
{ id: 4, label: 'Node 4' },
{ id: 5, label: 'Node 5' }
]);
// create an array with edges
const edges = new DataSet([
{ from: 1, to: 3 },
{ from: 1, to: 2 },
{ from: 2, to: 4 },
{ from: 2, to: 5 }
]);
const data = {
nodes: nodes,
edges: edges
};
const options = {};
// initialize your network!
class VisNetwork extends Component {
constructor() {
super();
this.network = {};
this.appRef = createRef();
}
componentDidMount() {
this.network = new Network(this.appRef.current, data, options);
}
render() {
return (
<div ref={this.appRef} />
);
}
}Run Code Online (Sandbox Code Playgroud)
这是一个使用函数式组件和 React 钩子的现代示例。
package.json
{
"dependencies": {
"react": "16.13.0",
"react-dom": "16.13.0",
"vis-network": "7.4.0"
}
}
Run Code Online (Sandbox Code Playgroud)
VisNetwork.js
import React, { useEffect, useRef } from 'react';
import { DataSet, Network} from 'vis-network/standalone/esm/vis-network';
const VisNetwork = () => {
// A reference to the div rendered by this component
const domNode = useRef(null);
// A reference to the vis network instance
const network = useRef(null);
// An array of nodes
const nodes = new DataSet([
{ id: 1, label: 'Node 1' },
{ id: 2, label: 'Node 2' },
{ id: 3, label: 'Node 3' },
{ id: 4, label: 'Node 4' },
{ id: 5, label: 'Node 5' }
]);
// An array of edges
const edges = new DataSet([
{ from: 1, to: 3 },
{ from: 1, to: 2 },
{ from: 2, to: 4 },
{ from: 2, to: 5 }
]);
const data = {
nodes,
edges
};
const options = {};
useEffect(
() => {
network.current = new Network(domNode.current, data, options);
},
[domNode, network, data, options]
);
return (
<div ref = { domNode } />
);
};
export default VisNetwork;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4346 次 |
| 最近记录: |