I am trying to render a D3 bar chart in angular 5. I have installed d3 graph using command
npm install d3
But when I am using it with angular 5 the chart is not getting rendered. My Html code is :
<button (click)= 'getGraph()'>show</button>
<div class="chart">
</div>
My ts file :
import { Component, OnInit } from '@angular/core';
import * as d3 from 'd3';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent{
data= [10,20,30,40,50,60]
constructor()
{
}
ngOnInit() {
}
getGraph() {
d3.select(".chart")
.selectAll("div")
.data(this.data)
.enter()
.append("div")
.style("width", function(d) { return d + "px"; })
.text(function(d) { return d; });
}
}
My CSS file :
.chart div {
font: 10px sans-serif;
background-color: red;
text-align: right;
padding: 3px;
margin: 1px;
color: white;
}
The values are showing in different positions but graphs are not forming. Is there anything wrong with the execution. Same code is working in pure JS.
Here is in stackblitz: https://stackblitz.com/edit/angular-rxgsv6?embed=1&file=src/app/app.component.html
Note : I took the code from this official link : https://scrimba.com/p/pEKMsN/cast-1953
It works fine in pure JS
ANSWER : Writing the style tags in global CSS instead of component CSS fixed it