如何使网络可视化在 vis.js 和 Angular 中工作?
How to make Network visualization work in vis.js with Angular?
我已经安装了我需要的依赖:
vis.js: npm install vis --save
@types/vis: npm install @types/vis --save-dev
代码片段:
import { Component, AfterViewInit, ElementRef, ViewChild } from '@angular/core';
import { Network, DataSet } from 'vis';
@Component({
selector: 'test',
template: '<div #network></div>'
})
export class TestComponent implements AfterViewInit {
@ViewChild('network') el: ElementRef;
private networkInstance: any;
ngAfterViewInit() {
const container = this.el.nativeElement;
const nodes = new DataSet<any>([
{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'}
]);
const edges = new DataSet<any>([
{from: 1, to: 3},
{from: 1, to: 2},
{from: 2, to: 4},
{from: 2, to: 5}
]);
const data = { nodes, edges };
this.networkInstance = new Network(container, data);
}
}
我像上面那样尝试只是为了尝试,但它给了我这个错误:
Cannot read property 'solve' of undefined
我错过了什么?
尝试添加超时,如下所示:
ngAfterViewInit() {
const nodes = new DataSet<any>([
{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'}
]);
const edges = new DataSet<any>([
{from: 1, to: 3},
{from: 1, to: 2},
{from: 2, to: 4},
{from: 2, to: 5}
]);
setTimeout(() => {
const data = { nodes, edges };
const container = this.el.nativeElement;
this.networkInstance = new Network(container, data);
}, 0);
}
我有一个类似的问题,我想这与我试图在选项卡中显示图表这一事实有关,我必须像这样引用它才能将它添加到事件队列中,所以它是在之后执行的当前调用堆栈中的所有其他调用。希望对您有所帮助。
我在 Angular 项目中使用 Vis.js 网络模块时遇到了同样的问题。经过一番搜索,我发现问题是由网络对象的构造方式引起的。如果替换此行:
this.networkInstance = new Network(container, data);
...这一行:
this.networkInstance = new Network(container, data, {});
那么问题就解决了
背景
Vis.js 的 Typescript 类型定义在构造函数的定义中存在错误,该定义声称
"options" argument to the constructor is optional: constructor(container: HTMLElement, data: Data, options?: Options);
.
但是如果您查看 Vis.js 代码,您会注意到传递未定义的选项属性会导致重要的初始化代码被跳过,从而导致您遇到的错误。
相反,如果您传递一个 empty 选项对象,则网络会正确初始化。
我已经安装了我需要的依赖:
vis.js: npm install vis --save
@types/vis: npm install @types/vis --save-dev
代码片段:
import { Component, AfterViewInit, ElementRef, ViewChild } from '@angular/core';
import { Network, DataSet } from 'vis';
@Component({
selector: 'test',
template: '<div #network></div>'
})
export class TestComponent implements AfterViewInit {
@ViewChild('network') el: ElementRef;
private networkInstance: any;
ngAfterViewInit() {
const container = this.el.nativeElement;
const nodes = new DataSet<any>([
{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'}
]);
const edges = new DataSet<any>([
{from: 1, to: 3},
{from: 1, to: 2},
{from: 2, to: 4},
{from: 2, to: 5}
]);
const data = { nodes, edges };
this.networkInstance = new Network(container, data);
}
}
我像上面那样尝试只是为了尝试,但它给了我这个错误:
Cannot read property 'solve' of undefined
我错过了什么?
尝试添加超时,如下所示:
ngAfterViewInit() {
const nodes = new DataSet<any>([
{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'}
]);
const edges = new DataSet<any>([
{from: 1, to: 3},
{from: 1, to: 2},
{from: 2, to: 4},
{from: 2, to: 5}
]);
setTimeout(() => {
const data = { nodes, edges };
const container = this.el.nativeElement;
this.networkInstance = new Network(container, data);
}, 0);
}
我有一个类似的问题,我想这与我试图在选项卡中显示图表这一事实有关,我必须像这样引用它才能将它添加到事件队列中,所以它是在之后执行的当前调用堆栈中的所有其他调用。希望对您有所帮助。
我在 Angular 项目中使用 Vis.js 网络模块时遇到了同样的问题。经过一番搜索,我发现问题是由网络对象的构造方式引起的。如果替换此行:
this.networkInstance = new Network(container, data);
...这一行:
this.networkInstance = new Network(container, data, {});
那么问题就解决了
背景
Vis.js 的 Typescript 类型定义在构造函数的定义中存在错误,该定义声称
"options" argument to the constructor is optional:
constructor(container: HTMLElement, data: Data, options?: Options);
.
但是如果您查看 Vis.js 代码,您会注意到传递未定义的选项属性会导致重要的初始化代码被跳过,从而导致您遇到的错误。
相反,如果您传递一个 empty 选项对象,则网络会正确初始化。