I'm not sure about drewmoore's suggestiong either. It didn't seem to work for me.
I've got two suggestions for continuing.
Best Answer
The first is an addendum to Arlo's answer. When I originally wrote this comment, I couldn't get Arlo's answer to work. Now I have understood why. I was trying to use the d3-hierarchy package. So installing:
npm install d3 --save
npm install @types/d3 --save-dev
npm install @types/d3-hierarchy --save-dev
And then using the modules this way:
import * as d3 from "d3";
d3.hierarchy(blah);
And then it would complain that it didn't know about the d3.hierarchy member. Now I understand that I must use that object (I don't know why it didn't register before). So, updated:
import * as d3 from "d3";
import * as d3Hierarchy from "d3-hierarchy";
d3Hierarchy.hierarchy(blah);
Original Answer
What seems to be the only answer I've found so far is using the d3-ng2-service module located at the link. It's not a great switch, but it does allow you to use d3v4 in your angular 2 project.
An excerpt from the readme can be found below of how to use it in an angular 2 component:
import { Component, OnInit, ElementRef } from '@angular/core';
import { D3Service, D3, Selection } from 'd3-ng2-service'; // <-- import the D3 Service, the type alias for the d3 variable and the Selection interface
@Component({
selector: 'app-test-d3',
templateUrl: 'test-d3.component.html',
styleUrls: ['test-d33.component.css']
})
export class TestD3Component implements OnInit {
private d3: D3; // <-- Define the private member which will hold the d3 reference
private parentNativeElement: any;
constructor(element: ElementRef, d3Service: D3Service) { // <-- pass the D3 Service into the constructor
this.d3 = d3Service.getD3(); // <-- obtain the d3 object from the D3 Service
this.parentNativeElement = element.nativeElement;
}
ngOnInit() {
let d3 = this.d3; // <-- for convenience use a block scope variable
let d3ParentElement: Selection<any, any, any, any>; // <-- Use the Selection interface (very basic here for illustration only)
// ...
if (this.parentNativeElement !== null) {
d3ParentElement = d3.select(this.parentNativeElement); // <-- use the D3 select method
// Do more D3 things
}
}
}
import * as D3 from 'd3';
butimport * as D3:any from 'd3';
doesn't work... – TommyFd3: any
property. – Ethan Jewettnpm install @types/d3v4
– drew moore