score:3
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");
};
}
score:4
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';
score:6
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
score:58
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.
Source: stackoverflow.com
Related Query
- Using D3.js with Angular 2
- Uncaught TypeError: (void 0) is not a function using d3 with angular 7
- Using Angular ng-class with d3.js on svg element
- Using d3 Timeline with Angular
- Using D3.js + click events with Angular 6
- Angular NVD3 Multibar Chart with dual y-axis to showup only line using json data
- injecting the d3.js graphs correctly when using Angular directive multiple times with bootstrap
- How to draw a multi line series graph using d3.js with json data using angular directives
- Display specific data in a td with multiple Arrays using Angular
- Rotate rectangle with 4 points using Angular
- d3 with using angular drag and drop directive
- How to draw line with arrow using d3.js
- Using arrow functions with d3
- How do I resolve "[ERR_REQUIRE_ESM]: Must use import to load ES Module" when using D3.js 7.0.0 with Next.js 11.0.1?
- D3 with Backbone / D3 with Angular / D3 with Ember?
- Using $filter in Angular directive
- How to use 'this' in Angular with D3?
- Using d3.js with Apache Zeppelin
- Using D3 transition method with data for scatter plot
- Graph with auto layout using d3 (a tree with multiple parent nodes)
- Using D3 with Elm
- Using d3-3d with pan & zoom while retaining rotation
- Make simple bar chart using C3 with separate columns on the x axis
- D3 fill shape with image using pattern
- D3 using classed() to add and remove class with checkbox
- Using d3.js svg clipPath not clipping in Angular
- How can I show a graph using d3 force layout with initial positions set and no movement at all?
- Using D3 with a multi-dimensional array/object
- Using AngularJS / AngularUI with d3.js and DOM effects
- Using server-sent events with cubism.js graphs
More Query from same tag
- What is the difference between D3 and jQuery?
- SVG loading issue in react native WebView (iOS)
- When adding d3.behavior.zoom, the .on("zoom", zoom), zoom undefined
- D3: selecting a <g> in an SVG does not work in Safari
- How to render links as elbow connectors in d3 force directed graph
- d3.js catch the nearest line with mouse click
- How to get all overlapping elements on 'mouseenter' in D3?
- SOLVE: C3 - Labels not showing in C3 pie chart whit narrow arcs
- embedding an image via d3 does not work
- Charting tool similar to Google Visualization
- Clear text in svg before adding in d3.js
- D3's scale not working properly
- Smooth update to x axis in a D3 line chart?
- error in D3.js code regarding particular place
- nvd3 - how to set y axis to default values
- How to add labels into the arc of a chord diagram in d3.js
- D3: Draw merged lines as a single
- removing all svg elements in D3 save some specified
- How to smooth two curves at joint point in d3.js v4?
- How to turn a hclust-object into JSON for D3?
- Parsing specific columns/data from multiple CSV file
- How do I scale in d3.js
- D3.js Dynamic connector between objects
- Why is the D3 chart not appearing in my LeafletJS popups?
- Access the 'value' in JS Object using D3.JS
- Labels on d3.js time series chart
- How plot and symbolize only selected columns from csv in plotting in d3?
- Invert yAxis using NVD3.js
- How to format array of values to form a distribution for google-charts histogram?
- How can I use the latest version of the d3 js library with Angular Dart?