1

For a project I'm working on, I found an interesting library that I would like to import, but I don't know how.

The project is based on Angular 2, it runs through a SPA template through ASP.NET Core (in case it matters) and it gets built by webpack.

Until now I installed both D3 and EventDrops through NPM, than imported them into my component.ts file like this:

import * as d3 from 'd3';
import * as EventDrops from 'event-drops';

First I tested the D3.js import by adding:

ngOnInit() {
    var eventDropsChart = d3.select("#visual");
    console.log(eventDropsChart);
    ...
}

It seems to work, so I tried the next step that is:

var eventDropsChart = d3.chart.eventDrops();

But it does not work, because

TS2339: Property 'chart' does not exist on type 'typeof "mypath/node_modules/@types/d3/index"'

Could you please help me?

Thank you very much!

Bugs
  • 4,491
  • 9
  • 32
  • 41
Brutus
  • 790
  • 3
  • 10
  • 27
  • 1
    Since EventDrop does not have a TypeScript implementation - afaik - and it builds on top of the d3 module, you should mock the missing properties - something like `interface d3{ chart: Function; }`. But to test if the imports work you could just `(d3).chart.eventDrops();` or something like this. Again, only to test, **not the right way to do this** :) – mkaran Jun 27 '17 at 15:59
  • Hi @mkaran, thank you very much! I tried your quick test but then I got `ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'eventDrops' of undefined TypeError: Cannot read property 'eventDrops' of undefined`. Then I wanted to try **eventDrops** alone by adding: `console.log(eventDrops);` but then at runtime the execution failed because it does not find d3, despite I imported it in my component: `Error: Uncaught (in promise): ReferenceError: d3 is not defined ReferenceError: d3 is not defined`. Do you have any idea about how to make d3 visible to eventDrops? – Brutus Jun 28 '17 at 07:52
  • Hmm, are you using angular cli? If yes, have you included the "../node_modules/event-drops/dist/eventDrops.js" to `angular-cli.json` in the scripts section? – mkaran Jun 28 '17 at 08:16
  • I don't think I am using the angular cli. I started from an online example you can find [here](https://github.com/CRANK211/vs17-ng2-dnc/tree/master/4%20-%20completed-sample). – Brutus Jun 28 '17 at 08:32

2 Answers2

0

After some experiments I finally got it working but note that it is a workaround. Event drops hooks to the global d3 variable, not the declared import from the 'd3' module. So, you can use eventsDrop as in your example by using the window.d3.chart.eventsDrop(...) like this:

import {AfterViewInit, Component} from '@angular/core';
import * as d3 from 'd3';
import * as eventDrops from 'event-drops';

declare global {
    interface Window { d3: any; }
}

@Component({
  selector: 'some-selector',
  templateUrl: './some.component.html',
  styleUrls: ['./some.component.css']
})
export class SomeComponent implements AfterViewInit{

  ngAfterViewInit(): void {
    console.log(d3, eventDrops, window.d3.chart.eventDrops)
    // or use window['d3'].chart.eventsDrop
    // or any of the answers here: https://stackoverflow.com/questions/12709074/how-do-you-explicitly-set-a-new-property-on-window-in-typescript
     var eventDropsChart = window.d3.chart.eventDrops();
  }

}

Hope this helps! Good luck!

I'll update the answer if I find a more sane and elegant way to do this!

mkaran
  • 2,528
  • 20
  • 23
  • 1
    Thanks a lot man, I'm going to try this workaround as soon as possible and let you know! :) I really appreciate your big big big help! – Brutus Jun 28 '17 at 13:50
  • It's not working for me: `Uncaught ReferenceError: d3 is not defined at Object.defineProperty.value (eventDrops.js:1) at e (eventDrops.js:1) at Object.defineProperty.value (eventDrops.js:1) at e (eventDrops.js:1) at Object.defineProperty.value (eventDrops.js:1) at eventDrops.js:1 at r (eventDrops.js:1) at Object. (eventDrops.js:1) at __webpack_require__ (bootstrap f9901e4…:657) at fn (bootstrap f9901e4…:85)` I tried to split, `console.log(d3)` alone is ok, but if I add `console.log(eventDrops)` I get the same error. Any typo? – Brutus Jun 28 '17 at 15:02
  • @Brutus hmm I'll look it up, it might have to do with the order the scripts are loaded or something else I am missing right now. Btw did you try with `ngAfterViewInit` ? – mkaran Jun 28 '17 at 19:03
  • Yes, I used ngAfterViewInit. I will try to upload my solution to GitHub asap, it may be useful to share the same "environment". – Brutus Jun 29 '17 at 08:00
  • @Brutus yes that would help a lot! let me know! – mkaran Jun 29 '17 at 10:28
  • I just pushed my solution to [GitHub](https://github.com/ptagl/EventDrops-on-Angular-4) in order to make it easier to solve the issue. The project contains 2 branches: the master contains the solution proposed by you, the "porkaround" contains a very ugly workaround I found (importing D3.js as a script instead as a NPM package). Hope it helps! – Brutus Jun 29 '17 at 12:30
0

Old thread but if anyone runs into this I was able to get it working by assigning global in polyfills.ts. Then you can use it just like the event-drops instructions describe for a Module Bundler:

(window as any).global = window;
Gardner
  • 939
  • 7
  • 6