我想創建一個簡單的強制圖形(2個節點與關係)與角2和類型的腳本。並不能使其工作。d3.js強制圖形與腳本和angular2
我會很感激這個工作演示。
這一點,我想出了」 idont代碼知道,如果它的正確與否
export class SysRelationsComponent implements OnInit {
private d3: D3;
private parentNativeElement: any;
private width:number=1200;
private height:number=600;
constructor(element: ElementRef, d3Service: D3Service) {
this.d3 = d3Service.getD3();
this.parentNativeElement = element.nativeElement;
}
ngOnInit() {
this.d3.select(this.parentNativeElement).style("color", "red");
let color = this.d3.scaleOrdinal(this.d3.schemeCategory20);
var nodes = [
{"id": "Alice"},
{"id": "Bob"},
{"id": "Carol"}
];
var links = [
{"source": "0", "target": "1"},
];
var simulation = this.d3.forceSimulation(nodes)
.force("charge", this.d3.forceManyBody())
.force("link", this.d3.forceLink(links) .id(function(d){return d.id}))
.force("center",this.d3.forceCenter());
var link = this.parentNativeElement.append("g")
.attr("class", "links")
.selectAll("line")
.data(links)
.enter().append("line")
.attr("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = this.parentNativeElement.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("r", 25)
.attr("fill", function(d) { return color(d.group); });
node.append("title")
.text(function(d) { return d.id; });
simulation
.nodes(nodes)
.on("tick", this.ticked);
/* .call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));*/
/*this.sdps.getSystemsWithRelataions();*/
}
}
貌似類型聲明'forceLink'可能不正確。它似乎缺少一個泛型類型參數。這只是看了一眼,我不完全確定。您可以使用類型「{id:string}'註釋您的回調函數參數作爲臨時解決方案,但如果這樣做有用,則應修改聲明。 –
非常感謝,在編譯時禁用了錯誤,但仍然在運行時得到它 – yaronmil
這表明聲明實際上是正確的,並且您使用的庫錯誤 –