错误无法读取d3-gantt图表中的null属性'getBoundingClientRect'

时间:2019-05-28 08:52:59

标签: javascript angular d3.js angular6

大家好,当我尝试创建时间线图表时,出现以下错误。我同时导入了d3和d3-时间线

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);

    }
}
}

但是出现以下错误

ERROR TypeError: Cannot read property 'getBoundingClientRect' of null

这里有人可以帮助修复它。非常感谢

1 个答案:

答案 0 :(得分:1)

时间轴元素可能尚未在ngOnInit中完全创建

要检查是否存在这种情况,请尝试:

setTimeout(() => {

        this.chart = d3timelines.timelines();

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

},1000)