Hey everyone, I'm currently in the process of incorporating a D3 visualization network graph into an Angular CLI project (http://bl.ocks.org/mbostock/1153292) using the ng2-nvd3 component.
Here's the Angular component:
import { Component, OnInit } from '@angular/core';
declare let d3: any;
@Component({
selector: 'app-visual',
templateUrl: './visual.component.html',
styleUrls: ['./visual.component.css']
})
export class VisualComponent implements OnInit {
private links = [
// List of link data
];
private nodes: Array<Object> = [];
private width = 960;
private height = 500;
private force: any;
public svg: any;
private path: any;
private circle: any;
private text: any;
constructor(/*d3Service: D3Service*/) {
// this.d3 = d3Service.getD3();
}
ngOnInit() {
// let d3 = this.d3;
this.computeLinks(this.nodes);
this.forceLayout();
this.appendGraph();
}
computeLinks(nodes) {
// Function to compute distinct nodes from links.
}
forceLayout() {
// Function for force layout
}
appendGraph() {
// Function to append graph elements
}
tick() {
// Function to handle ticks
}
linkArc(d) {
// Function for generating links between nodes
}
transform(d) {
// Function for transforming node positions
}
}
ERROR TypeError: Cannot read property 'attr' of undefined
at d3_dispatch.VisualComponent.tick (visual.component.ts:124)
at d3_dispatch.event [as tick] (d3.js:504)
at Object.force.tick [as c] (d3.js:6307)
at d3_timer_mark (d3.js:2166)
at d3_timer_step (d3.js:2147)
at ZoneDelegate.invokeTask (zone.js:425)
at Object.onInvokeTask (core.js:4747)
at ZoneDelegate.invokeTask (zone.js:424)
at Zone.runTask (zone.js:192)
at ZoneTask.invokeTask (zone.js:499)
Upon loading the web application, the graph is not visible: Snapshot of generated graph
Debugging by logging this.path in the tick() method returns undefined even though it was created in the appendGraph() method.