I am currently working on implementing a pie chart (donut series) in Angular. Below are my HTML, CSS, and TypeScript files. I am following this tutorial resource:
Link to CodeSandBox - https://codesandbox.io/s/apx-donut-simple-8fnji?from-embed
import { Component, OnInit, ViewChild } from '@angular/core';
import { ChartComponent } from "ng-apexcharts";
import {
ApexNonAxisChartSeries,
ApexResponsive,
ApexChart
} from "ng-apexcharts";
export type ChartOptions = {
series: ApexNonAxisChartSeries;
chart: ApexChart;
responsive: ApexResponsive[];
labels: any;
};
@Component({
selector: 'app-second-page',
templateUrl: './second-page.component.html',
styleUrls: ['./second-page.component.css']
})
export class SecondPageComponent implements OnInit {
@ViewChild("chart") chart: ChartComponent;
public chartOptions: Partial<ChartOptions>;
constructor() {
this.chartOptions = {
series: [44, 55, 13, 43, 22],
chart: {
type: "donut"
},
labels: ["Team A", "Team B", "Team C", "Team D", "Team E"],
responsive: [
{
breakpoint: 480,
options: {
chart: {
width: 200
},
legend: {
position: "bottom"
}
}
}
]
};
}
ngOnInit(): void {
}
}
#chart {
max-width: 380px;
margin: 35px auto;
padding: 0;
}
<div id="chart">
<apx-chart
[series]="chartOptions.series"
[chart]="chartOptions.chart"
[labels]="chartOptions.labels"
[responsive]="chartOptions.responsive"
></apx-chart>
</div>
I am encountering an undefined issue, although it functions properly in the tutorial. I would appreciate any assistance in resolving this problem.
Error: src/app/second-page/second-page.component.html:32:8 - error TS2322: Type 'ApexNonAxisChartSeries | undefined' is not assignable to type 'ApexAxisChartSeries | ApexNonAxisChartSeries'.
Type 'undefined' is not assignable to type 'ApexAxisChartSeries | ApexNonAxisChartSeries'.
32 [series]="chartOptions.series"
~~~~~~
src/app/second-page/second-page.component.ts:19:16
19 templateUrl: './second-page.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component SecondPageComponent.
Error: src/app/second-page/second-page.component.html:33:8 - error TS2322: Type 'ApexChart | undefined' is not assignable to type 'ApexChart'.
Type 'undefined' is not assignable to type 'ApexChart'.
33 [chart]="chartOptions.chart"
~~~~~
src/app/second-page/second-page.component.ts:19:16
19 templateUrl: './second-page.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component SecondPageComponent.
Error: src/app/second-page/second-page.component.html:35:8 - error TS2322: Type 'ApexResponsive[] | undefined' is not assignable to type 'ApexResponsive[]'.
Type 'undefined' is not assignable to type 'ApexResponsive[]'.
35 [responsive]="chartOptions.responsive"
~~~~~~~~~~
src/app/second-page/second-page.component.ts:19:16
19 templateUrl: './second-page.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component SecondPageComponent.
Error: src/app/second-page/second-page.component.ts:23:23 - error TS2564: Property 'chart' has no initializer and is not definitely assigned in the constructor.
23 @ViewChild("chart") chart: ChartComponent;
~~~~~
✖ Failed to compile.