I'm currently working on an Angular project that uses server-side rendering to generate D3 charts. Each chart is encapsulated within its own component, such as a line-chart component which consists of TypeScript, spec.ts, HTML, and CSS files for rendering the line chart. D3 library has been successfully installed, and the HTML content of the component includes an SVG tag with an ID attribute for easy selection. However, when attempting to select the element using D3, I encounter the error message "ReferenceError: document is not defined" in
? new _selection_index__WEBPACK_IMPORTED_MODULE_0__["Selection"]([[document.querySelector(selector)]], [document.documentElement])
Here is a simple TypeScript code snippet that triggers this error. How can I resolve this issue?
import { Component, OnInit } from '@angular/core';
import * as d3 from "d3";
var svg = d3.select("#canvas");
@Component({
selector: 'app-line-chart',
templateUrl: './line-chart.component.html',
styleUrls: ['./line-chart.component.css']
})
export class LineChartComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}