chart.js has no exported member named 'ChartDataSets'. Did you mean 'ChartDataset'? ts(2724)
Asked Answered
E

4

11

When I import ChartDataSets I have an error message on line-chart.component.ts

'chart.js' has no exported member named 'ChartDataSets'. Did you mean 'ChartDataset'?

enter image description here

I don't understand where is the problem? I forgot to install a library?

My code is presented like this on line-chart.component.ts

import { Component, } from '@angular/core';
import { ChartDataSets, ChartOptions } from 'chart.js';
import { Color, Label } from 'ng2-charts';

@Component({
  selector: 'app-line-chart',
  templateUrl: './line-chart.component.html',
  styleUrls: ['./line-chart.component.css']
})

export class LineChartComponent {

  lineChartData: ChartDataSets[] = [
    { data: [85, 72, 78, 75, 77, 75], label: 'Crude oil prices' },
  ];

  lineChartLabels: Label[] = ['January', 'February', 'March', 'April', 'May', 'June'];

  lineChartOptions = {
    responsive: true,
  };

  lineChartColors: Color[] = [
    {
      borderColor: 'black',
      backgroundColor: 'rgba(255,255,0,0.28)',
    },
  ];

  lineChartLegend = true;
  lineChartPlugins = [];
  lineChartType = 'line';
  
}

On line-chart.component.html I have this:

<div class="chart-wrapper">
    <canvas baseChart 
        [datasets]="lineChartData" 
        [labels]="lineChartLabels" 
        [options]="lineChartOptions"
        [colors]="lineChartColors" 
        [legend]="lineChartLegend" 
        [chartType]="lineChartType" 
        [plugins]="lineChartPlugins">
    </canvas>
</div>

Then on app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ChartsModule } from 'ng2-charts';

import { AppComponent } from './app.component';
import { LineChartComponent } from './line-chart/line-chart.component';

@NgModule({
  declarations: [
    AppComponent,
    LineChartComponent
  ],
  imports: [
    BrowserModule,
    ChartsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

I thank you in advance for your help.

Elise answered 6/8, 2021 at 10:32 Comment(2)
Can you specify which chart.js version is used? Thanks.Care
@Yong Shun: It's the version -> 3.5.0 I follow the tutoriel hereElise
W
2

Install type chart

npm install @types/chart.js
Whit answered 2/2, 2023 at 19:3 Comment(0)
B
9

That happened because of version incompatibility. Try to change ChartDataSets to ChartDataSet.

Also try to install npm install @types/chart.js

Here is working sample on StackBlitz

Also you can see this

Bohun answered 6/8, 2021 at 10:59 Comment(2)
Thank you Alireza,. I will use your graph. For information: ChartDataSets to ChartDataSet. do not change anything.Elise
I managed to fix the problem switching from "ChartDataSets" to "ChartDataset"Zared
C
2

"ng2-charts": "^2.4.2", "chart.js": "^2.9.3",

Cormophyte answered 18/3, 2022 at 22:41 Comment(0)
W
2

Install type chart

npm install @types/chart.js
Whit answered 2/2, 2023 at 19:3 Comment(0)
H
1

you need to upgrade the version:

   "chart.js": "^4.0.0",
    "react-chartjs-2": "^5.0.0",

see here: https://codesandbox.io/s/github/reactchartjs/react-chartjs-2/tree/master/sandboxes/doughnut/default?from-embed=&file=/package.json

Package JSON file.

Horsy answered 29/7, 2023 at 20:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.