denisemauldin / d3-timeline

D3 timeline
BSD 3-Clause "New" or "Revised" License
168 stars 72 forks source link

Error Cannot read property 'getBoundingClientRect' of null. #22

Open Akayeshmantha opened 5 years ago

Akayeshmantha commented 5 years ago

Hi all when am trying to create the timeline chart I get the below error.I imported both d3 and d3-timeline

import * as d3timelines from 'd3-timelines';
import * as d3 from 'd3';
export class DashboardThreadedComponent implements OnInit {
 private data: any = [
        {label: 'person a', times: [
            {starting_time: 1355752800000, ending_time: 1355759900000},
            {starting_time: 1355767900000, ending_time: 1355774400000}]},
        {label: 'person b', times: [
            {starting_time: 1355759910000, ending_time: 1355761900000}]},
        {label: 'person c', times: [
            {starting_time: 1355761910000, ending_time: 1355763910000}]},
        ];

private chart: any;
 constructor(
        private cookieService: CookieService,
        private modalService: NgbModal,
        private bpeService: BPEService,
        private userService: UserService,
        private router: Router,
        private route: ActivatedRoute,
        public appComponent: AppComponent
  ) {}

    ngOnInit() {

        this.chart = d3timelines.timelines();

        d3.select('.timeline').append('svg').attr('width', 500)
        .datum(this.data).call(this.chart);

    }
}
}

But am getting the below error ERROR TypeError: Cannot read property 'getBoundingClientRect' of null

Any one here that can help to fix it .Thanks alot

rijvirajib commented 1 year ago

This is usually because the DOM isn't really ready. Wrapping the d3.select in a setTimeout will resolve it. On a large app of mine, I've had to set the timeout to 100 to guarantee the SVG is ready.