如何使vis.js lib与Angular2一起使用?

时间:2016-10-28 01:55:34

标签: angular angular2-template vis.js vis.js-timeline

我试图使用visjs lib,但无法让他们开始使用示例工作,如下所示:

<script type="text/javascript">
    // DOM element where the Timeline will be attached
    var container = document.getElementById('visualization');

    // Create a DataSet (allows two way data-binding)
    var items = new vis.DataSet([
        {id: 1, content: 'item 1', start: '2013-04-20'},
        {id: 2, content: 'item 2', start: '2013-04-14'},
        {id: 3, content: 'item 3', start: '2013-04-18'},
        {id: 4, content: 'item 4', start: '2013-04-16', end: '2013-04-19'},
        {id: 5, content: 'item 5', start: '2013-04-25'},
        {id: 6, content: 'item 6', start: '2013-04-27'}
    ]);

    // Configuration for the Timeline
    var options = {};

    // Create a Timeline
    var timeline = new vis.Timeline(container, items, options);
</script>

4 个答案:

答案 0 :(得分:17)

请在angular2下使用vis查找以下代码,您只需安装vis并导入即可。 在这个例子中,我为vis库创建了一个angular2组件。

import { Component , OnInit, OnDestroy } from '@angular/core';
import { Network, DataSet, Node, Edge, IdType } from 'vis';
@Component({
  selector: 'network-graph',
  templateUrl: './network-graph.component.html'
})
export class NetworkGraphComponent implements OnInit{
    public nodes: Node;
    public edges: Edge;
    public network : Network;

    public ngOnInit(): void {
          var 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
            var edges = new DataSet([
              {from: 1, to: 3},
              {from: 1, to: 2},
              {from: 2, to: 4},
              {from: 2, to: 5},
              {from: 3, to: 3}
            ]);
           // create a network
          var container = document.getElementById('mynetwork');
          var data = {
            nodes: nodes,
            edges: edges
          };
          var options = {};
          var network = new Network(container, data, options);
    }
}

答案 1 :(得分:9)

这是一个简单的plunker集成了你用Angular 2发布的代码 https://plnkr.co/edit/TbPTfXFk4RSAuPn4BBxP?p=preview

在此示例中,集成位于OnInit

ngOnInit() {
    var container = document.getElementById('visualization');

    // Create a DataSet (allows two way data-binding)
    var items = new vis.DataSet([
        {id: 1, content: 'item 1', start: '2013-04-20'},
        {id: 2, content: 'item 2', start: '2013-04-14'},
        {id: 3, content: 'item 3', start: '2013-04-18'},
        {id: 4, content: 'item 4', start: '2013-04-16', end: '2013-04-19'},
        {id: 5, content: 'item 5', start: '2013-04-25'},
        {id: 6, content: 'item 6', start: '2013-04-27'}
    ]);

    // Configuration for the Timeline
    var options = {};

    // Create a Timeline
    var timeline = new vis.Timeline(container, items, options);
  }

答案 2 :(得分:8)

使用angular指令将是一种解决此问题的简洁方法。让我们从安装vis.js

开始
npm install vis

显示vis图表的组件应该有一个图表的容器元素。比如说

graph-visualization.component.html

<div [appGraphVis]="graphData" class="graph-container"></div>

graph-visualization.component.css

.graph-container {
    height: 25em;
    widows: 100%;
}

图的visualization.component.ts

import { Component, OnInit } from '@angular/core';
import { DataSet } from 'vis';

@Component({
  selector: 'app-graph-visualization',
  templateUrl: './graph-visualization.component.html',
  styleUrls: ['./graph-visualization.component.css']
})
export class GraphVisualizationComponent {
  graphData = {};

  constructor() { }

  ngAfterContentInit(){
    // create an array with nodes
    var 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
    var edges = new DataSet([
      {from: 1, to: 3},
      {from: 1, to: 2},
      {from: 2, to: 4},
      {from: 2, to: 5}
    ]);

    // provide the data in the vis format
    this.graphData["nodes"] = nodes;
    this.graphData["edges"] = edges;
  }

}

注意一些事情:

  • 此组件仅计算需要可视化的数据,并且不执行任何操作,例如直接使用DOM或直接使用vis.js。由于vis.js是一个DOM操作,我们可以有一个处理它的指令
  • 使用ngAfterContentInit而不是ngOnInit。这将延迟图形生成部分并允许其他DOM相关任务完成

graphvis.directive.ts

import { Directive, TemplateRef, ViewContainerRef, Input, Renderer2, ElementRef } from '@angular/core';
import { Network } from 'vis';

@Directive({
  selector: '[appGraphVis]'
})
export class GraphVisDirective {
  network;

  constructor(private el: ElementRef) {}

  @Input() set appGraphVis(graphData){
    console.log('graph data ', graphData);
    var options = {};

    if(!this.network){
      this.network = new Network(this.el.nativeElement, graphData, options);
    }

  }

}

为简单起见,我将选项保留在指令中。在现实世界中,选项将作为高级组件的另一个输入传递。

附加说明,我们可以在开发和调试期间为vis提供typescript的类型以提供帮助。它可以在这里找到:

yarn add @types/vis -D

如果您正在寻找可立即使用的解决方案,请查看此库 - angular-vis。在撰写本文时,它不支持vis.js的所有功能

答案 3 :(得分:1)

嗯,上面提到的代码需要进行一些修改,这需要一段时间才能绕过...所以我分享了这些:

1)您需要通过html部分中的 @ViewChild('netWords')netContainer:ElementRef; #netWords 来访问容器。

2)您需要使用nativeElement属性完全访问容器元素,这样就可以了 this.network = new Vis.Network(this.netContainer.nativeElement,data,options);

它应该适用于这些修改。

相关问题