-
Notifications
You must be signed in to change notification settings - Fork 488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Import error: "Module 'chartjs-plugin-datalabels/types/index' has no default export." #130
Comments
You can fix this issue with installing the types for Chart.js via |
@danielwstrimpel I installed this, but now get further errors as well as the original. So I still have the error at the import... But I still get the import error... And the following compile error..
|
Those last two errors listed look like problems in your usage. The For the second one, you should import the library like |
@danielwstrimpel - yes some of those errors are coming from another (test) component where I was trying the Chart as a line graph. Funny everything I set in there worked, and now it is telling me some of the options are not valid... I wonder if the typedef is up to date with the library? At any rate, I commented out these, so can just look at the error we are concerned with here. So now, I still have the import error, and now the only compile error is
Yes, I certainly want to know how to import this into a TypeScript project (if that is different for some reason) |
@pjc2007 How do you import this plugin? can you share the code of your project so it will be easier to debug? |
@simonbrunel - yes for sure, I'll include all my component code here. This is just a test app for my to try out chart.js, so is a little adhoc and untidy with me trying stuff out. But all has worked so far except for using this plugin I created a new Ionic 4 blank application, and have installed the following... dependencies:
devDependencies
And the component code...
I can share the whole project if that is of any help |
Per the documentation: import ChartDataLabels from 'chartjs-plugin-datalabels'; |
@simonbrunel - oh ok, yes I did add the braces when I I got the error it without them. I changed back so now I have... IF we required, whole repo is at https://github.com/pjc2007/testChartjs |
I think that's because the TS declaration doesn't export the plugin, so |
@simonbrunel - ok thanks for that! I tried the
and now at least my I now have
So I can now get my percentage labels! I am sure I had tried the |
@simonbrunel the correct way to import the library and get a reference to the output in Typescript is |
@danielwstrimpel - I tried this and seems to work 100%! Thanks for that. Wonder if that could be put in the doco for other Angular / TypeScript users (there are a lot of people in this category that use this library)? So, I can now type everything which helps a lot with getting setup correct. So, the full code is now as follows. import { Component, OnInit, ViewChild } from '@angular/core';
import { Chart, ChartOptions, ChartData } from 'chart.js';
import * as ChartDataLabels from 'chartjs-plugin-datalabels';
import { sum } from 'lodash';
@Component({
selector: 'app-pie-graph',
templateUrl: './pie-graph.component.html',
styleUrls: ['./pie-graph.component.scss'],
})
export class PieGraphComponent implements OnInit {
@ViewChild('mygraph') private chartRef;
chart: Chart;
data: ChartData;
constructor() {
Chart.plugins.unregister(ChartDataLabels);
}
public updateClick(evt) : void {
try {
let activePoints = this.chart.getElementsAtEvent(evt);
let i = 0;
this.data.datasets[0].data[0] = 44;
this.chart.update();
} catch (error) {
alert(error);
}
}
public ngOnInit() : void {
this.data = {
datasets: [{
data: [10, 20, 30, 50],
backgroundColor: [
'green',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
}],
// These labels appear in the legend and in the tooltips when hovering different arcs
labels: [
'Red',
'Yellow',
'Blue',
'another'
]
};
let options : ChartOptions = {
responsive: true,
maintainAspectRatio: false,
legend: {
position: 'bottom',
labels: {
boxWidth: 20
}
},
tooltips: {
enabled: false
},
plugins: {
datalabels: {
formatter: (value, ctx) => {
let dataArr = ctx.chart.data.datasets[0].data;
let total = sum(dataArr);
let percentage = (value * 100 / total).toFixed(2) + "%";
return percentage;
},
color: 'black',
}
}
};
this.chart = new Chart(this.chartRef.nativeElement, {
plugins: [ChartDataLabels],
type: 'pie',
data: this.data,
options: options
});
}
} |
@pjc2007 @danielwstrimpel I'm not sure why declare var plugin: object;
export default plugin; So users can import using: import ChartDataLabels from 'chartjs-plugin-datalabels'; Note that this change would break |
@simonbrunel Imports are the one thing that always "get me" in TypeScript , and I can spend hours trying to work it out when they are not "standard", so yes I agree with you. The imports syntax from the import { Chart, ChartOptions, ChartData } from 'chart.js'; So you can import individual classes (listing each inside of the Also in some cases, from what I understand, this can also have the potential to help with "tree shaking" i.e. not having to import everything. |
+1 |
Adding this as @pjc2007 says work out for me in Dev Builds
|
Temporarily I resolved the error by changing the type of chart to any instead of Chart. |
Thanks its working... |
Released in version 0.7.0. |
I just installed this and using
I get the error
The only import that works is |
@Cimmeron you need to install |
i dont know, until now, i still can't using this plugin when import error response |
When I had installed @types/chart.js, it gave me another error for
inside I had to comment this one out and also I commented |
npm i chartjs-plugin-datalabels |
Here is a solution that will always work: You will need @types/chartjs Importing Some example inside the code
You can put as any if you dont know the required type |
Hello All, How to build a doughnut chart only using chart.js and typescript? I have the following versions I am able to display the chart. But I want to achieve the below requirements as well.
I am getting the dependency error when I try to install npm chartjs-plugin-datalabels. npm ERR! node_modules/chart.js Any suggestion please to resolve this issue? Thanks! |
I also got some error try to use the lib with TS.
|
Just copy chart import 'assets/js/chartjs-plugin-datalabels.js'; |
Trying out Chart.js, and have been told I need to use the chartjs-plugin-datalabels to be able to write the percentage text on the piew pieces.
I installed and imported the
chartjs-plugin-datalabels
as per the documentation into my Angular 7 project (actually Ionic 4, which uses Angular 7), but vscode reports the following error on the import...Module '"../../../../node_modules/chartjs-plugin-datalabels/types"' has no exported member 'ChartDataLabels'.ts(2
I have the following versions
"chart.js": "^2.8.0", "chartjs-plugin-datalabels": "^0.6.0",
If I go to the node_modules\chartjs-plugin-datalabels\types\index.d.ts file, I also see a similar error here for the line
declare module 'chart.js
Invalid module name in augmentation. Module 'chart.js' resolves to an untyped module at 'd:/dev/ionic/chartjstest/node_modules/chart.js/dist/Chart.js', which cannot be augmented.ts
If I proceed, and try to run, I then get the compile error
[ng] ERROR in node_modules/chartjs-plugin-datalabels/types/index.d.ts(5,16): error TS2665: Invalid module name in augmentation. Module 'chart.js' resolves to an untyped module at 'D:/dev/ionic/chartjstest/node_modules/chart.js/dist/Chart.js', which cannot be augmented. [ng] src/app/home/pie-graph/pie-graph.component.ts(3,10): error TS2305: Module '"D:/dev/ionic/chartjstest/node_modules/chartjs-plugin-datalabels/types/index"' has no exported member 'ChartDataLabels'.
The text was updated successfully, but these errors were encountered: