【问题标题】:How to make vis.js lib to work with Angular2?如何使 vis.js lib 与 Angular2 一起工作?
【发布时间】:2016-10-28 01:55:33
【问题描述】:

我正在尝试使用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>

【问题讨论】:

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


    【解决方案1】:

    请在下面找到使用angular2vis 的代码,您只需要安装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);
        }
    }
    

    【讨论】:

    • 虽然欢迎使用此代码 sn-p,并且可能会提供一些帮助,但它会是 greatly improved if it included an explanation of 如何解决这个问题。没有这个,你的回答就没有多少教育价值了——记住你是在为未来的读者回答这个问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。
    • 这个答案对于有 Angular 2 背景的人来说是完全可以的。
    • 我很想知道你是如何导入vis的,以及你是否使用闭包来编译项目......
    • 不,它是简单的 Angular cli 应用程序,在 npm install vis 中,只需将其包含在我的组件中。我没有在模块文件中导入 vis。
    【解决方案2】:

    这是一个简单的 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);
      }
    

    【讨论】:

    • 如何获得vis的引用?
    • 感谢您让它工作,但我收到错误:app.module.ts:40:24 找不到名称“vis”。
    • 好的,终于成功了,非常感谢!有没有办法把它放在一个 angular 2 组件中?
    • 我将组件更新为 vis 库的目标元素
    • 还添加了 declare var vis: any;它声明了 vis 变量。如果该库存在,您也许可以为它安装类型,但将其声明为 any 可能没问题,除非您需要在其上调用多个方法
    【解决方案3】:

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

    npm install vis
    

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

    图形可视化.component.html

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

    图形可视化.component.css

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

    图形可视化.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 的所有功能

    【讨论】:

      【解决方案4】:

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

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

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

      它应该适用于这些修改。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-08
        • 1970-01-01
        • 1970-01-01
        • 2014-06-08
        相关资源
        最近更新 更多