Com*_*ode 9 vis.js angular2-template angular 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>
Run Code Online (Sandbox Code Playgroud)
mee*_*een 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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个简单的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);
}
Run Code Online (Sandbox Code Playgroud)
使用角度指令将是一种解决此问题的简洁方法.让我们从安装vis.js开始
npm install vis
Run Code Online (Sandbox Code Playgroud)
显示vis图表的组件应该有一个图表的容器元素.比方说
图的visualization.component.html
<div [appGraphVis]="graphData" class="graph-container"></div>
Run Code Online (Sandbox Code Playgroud)
图的visualization.component.css
.graph-container {
height: 25em;
widows: 100%;
}
Run Code Online (Sandbox Code Playgroud)
图的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;
}
}
Run Code Online (Sandbox Code Playgroud)
注意几件事:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
为简单起见,我将选项保留在指令中.在现实世界中,选项将作为高级组件的另一个输入传递.
附加说明,我们可以在开发和调试过程中为vis添加typescript的类型.它可以在这里找到:
yarn add @types/vis -D
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找即用型解决方案,请查看此库 - angular-vis.在撰写本文时,它不支持vis.js的所有功能
| 归档时间: |
|
| 查看次数: |
10197 次 |
| 最近记录: |