chartjs / chartjs-plugin-datalabels

Chart.js plugin to display labels on data elements
https://chartjs-plugin-datalabels.netlify.app
MIT License
874 stars 473 forks source link

TypeError of datalabels in case stacked100 is enabled #388

Closed arunfancraze closed 1 year ago

arunfancraze commented 1 year ago

@simonbrunel @y-takey I am facing 1 problem, while writing it in typescript. I am getting a typeError Property 'calculatedData' does not exist on type 'ChartData<keyof ChartTypeRegistry, (number | ScatterDataPoint | BubbleDataPoint | null)[], unknown>' You can see the screenshot attached below.

Screenshot 2023-07-21 at 4 35 54 PM

Originally posted by @arunfancraze in https://github.com/chartjs/chartjs-plugin-datalabels/issues/89#issuecomment-1645405415

y-takey commented 1 year ago

@arunfancraze Although I haven't tested it, I think you can achieve a simple solution using the following approach:

import type { ExtendedChartData } from 'chartjs-plugin-stacked100';

formatter: (value, context) {
  const data = context.dataset.data[context.dataIndex] as ExtendedChartData;
}
Arun-chaitanya commented 1 year ago

Hi @y-takey, This solution works. You can mark this as closed. Thanks.

y-takey commented 1 year ago

Hi @Arun-chaitanya, Thank you for trying it out! I'm glad it worked and the issue was resolved. :) but, it seems that I am unable to close it due to insufficient permissions.

simonbrunel commented 1 year ago

I would say it's an issue with the chartjs-plugin-stacked100 package which should probably augment the ChartData core type. @y-takey Maybe you could try something like that in your types.ts:

import {ChartData} from 'chart.js';

declare module 'chart.js' {
  interface ChartData< 
    TType extends ChartType = ChartType,
    TData = DefaultDataPoint<TType>,
    TLabel = unknown
  > {
    calculatedData?: number[][];
    originalData?: PluginDataPoint[];
  }
}

// Backward compatibility
export type ExtendedChartData = ChartData; 
y-takey commented 1 year ago

@simonbrunel Thank you for the advice. I would like to try it when I have some time! :)