Using D3.js with Angular 2

后端 未结 4 511
情书的邮戳
情书的邮戳 2020-12-12 11:06

I have successfully integrated Angular 2 (Alpha 44) with D3.js:



Angular 2 QuickStart


        
相关标签:
4条回答
  • 2020-12-12 11:33

    Try this:

    npm install d3@3.5.36 --save to set the version you need

    npm install @types/d3@3.5.36 --save or a higher version if you want d3 4+

    and then in your ts do

    import * as d3 from 'd3';

    Should work just fine

    0 讨论(0)
  • 2020-12-12 11:33
    npm install --save d3
    

    check d3 version in package.json and check it in node_modules too.

    then, in the component.ts, import it as below

    import * as d3 from 'd3';
    
    0 讨论(0)
  • 2020-12-12 11:38

    I was having trouble using ElementRef, I'm not sure if that API has changed. So I ended up using ViewContainRef to get the nativeElement.

    import {Component, ViewContainerRef, OnInit} from '@angular/core';
    declare var d3:any;
    @Component({
        selector: 'line-chart',
        directives: [],
        template: `<div class="sh-chart">chart</div>`
    })
    export class LineChart implements OnInit{
        elem ;
        constructor(private viewContainerRef:ViewContainerRef) {}
        ngOnInit(){
            this.elem = this.viewContainerRef.element.nativeElement;
    
            d3.select(this.elem).select("div").style("background-color", "yellow");
        };
    }
    
    0 讨论(0)
  • 2020-12-12 11:47

    To use Renderer, you need the raw HTML element (aka nativeElement). So basically you have to use whatever your library is, get the raw element and pass it to Renderer.

    For example

    // h3[0][0] contains the raw element
    var h3 = d3.select(this.el.nativeElement).select('h3');
    this.renderer.setElementStyle(h3[0][0], 'background-color', 'blue');
    

    The warning about ElementRef is about using it directly. That means that this is discouraged

    elementRef.nativeElement.style.backgroundColor = 'blue';
    

    But this is fine

    renderer.setElementStyle(elementRef.nativeElement, 'background-color', 'blue');
    

    For showing purposes you can use it as well with jQuery

    // h2[0] contains the raw element
    var h2 = jQuery(this.el.nativeElement).find('h2');
    this.renderer.setElementStyle(h2[0], 'background-color', 'blue');
    

    My recommendation though is to stick to use what angular2 provides you to do this easily without depending on another libraries.

    With pure angular2 you have two easy ways

    • Using directives
    // This directive would style all the H3 elements inside MyComp
    @Directive({
        selector : 'h3',
        host : {
            '[style.background-color]' : "'blue'"
        }
    })
    class H3 {}
    
    @Component({
        selector : 'my-comp',
        template : '<h3>some text</h3>',
        directives : [H3]
    })
    class MyComp {}
    
    • Using ViewChild with local variables
    @Component({
        selector : 'my-comp',
        template : '<h3 #myH3>some text</h3>',
    })
    class MyComp {
        @ViewChild('myH3') myH3;
        ngAfterViewInit() {
            this.renderer.setElementStyle(this.myH3.nativeElement, 'background-color', 'blue');
        }
    }
    

    Here's a plnkr with all the cases I mentioned in this answer. Your requirements may differ, of course, but try to use angular2 whenever you can.

    0 讨论(0)
提交回复
热议问题