Skip to content
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

Export types for formatter callback params #14277

Open
sthenault opened this issue Feb 17, 2021 · 5 comments
Open

Export types for formatter callback params #14277

sthenault opened this issue Feb 17, 2021 · 5 comments

Comments

@sthenault
Copy link

What problem does this feature solve?

would allow to define callback formatter with properly typed arguments: for now I have to type params to any, loosing typing within the formatter function. For instance:

    tooltip.formatter = (params: any) => {
        // access to whatever in params is allowed
        ...
        return content;
    };

What does the proposed API look like?

As I get it, params is expected to be of FormatterParams type, so exporting in within echarts.d.ts would allow something along the line of :

import {FormatterParams} from "echarts";

...
tooltip.formatter = (params: FormatterParams) => {
        // compilation error on access to something to defined in FormatterParams
        return content;
    };
@echarts-bot
Copy link

echarts-bot bot commented Feb 17, 2021

Hi! We've received your issue and please be patient to get responded. 🎉
The average response time is expected to be within one day for weekdays.

In the meanwhile, please make sure that you have posted enough image to demo your request. You may also check out the API and chart option to get the answer.

If you don't get helped for a long time (over a week) or have an urgent question to ask, you may also send an email to [email protected]. Please attach the issue link if it's a technical question.

If you are interested in the project, you may also subscribe our mailing list.

Have a nice day! 🍵

@echarts-bot echarts-bot bot added new-feature pending We are not sure about whether this is a bug/new feature. waiting-for: community labels Feb 17, 2021
@Ovilia Ovilia added this to the TBD milestone Feb 22, 2021
@Ovilia Ovilia removed pending We are not sure about whether this is a bug/new feature. waiting-for: community labels Feb 22, 2021
@plainheart plainheart modified the milestones: TBD, 5.x Apr 21, 2021
@Ovilia Ovilia modified the milestones: 5.x, TBD Sep 1, 2022
@azureabaaba
Copy link

模块“"echarts"”没有导出的成员“FormatterParams”。

@MaewenPell
Copy link

MaewenPell commented Apr 7, 2024

Hello, 👋

Any news regarding this point please ?

@Loongphy
Copy link

import { CallbackDataParams } from 'echarts/types/dist/shared.js'
import { TooltipMarker } from 'echarts/types/src/util/format.js'

export type TooltipCallbackDataParams = CallbackDataParams & {
  axisDim?: string
  axisIndex?: number
  axisType?: string
  axisId?: string
  axisValue?: string | number
  axisValueLabel?: string
  marker?: TooltipMarker
}

type TooltipCallbackDataParams = CallbackDataParams & {

@mkf62
Copy link

mkf62 commented Jun 21, 2024

I'm also stuck on this.

import { TooltipComponentOption } from 'echarts'

let tooltipConfig: TooltipComponentOption = {
    formatter: function (params) {
        return params.value
    },
}

Typescript squiggly lines .value and reports:

Property 'value' does not exist on type 'TopLevelFormatterParams'.
  Property 'value' does not exist on type 'CallbackDataParams[]'.

Well, TopLevelFormatterParams looks like this:

type TopLevelFormatterParams = CallbackDataParams | CallbackDataParams[];

Since properties like value or dimensionNames etc live on CallbackDataParams type (not an array), you can't guarantee you can access it via the TopLevelFormatterParams type which is why Typescript is throwing an error. It's also why you can't do params[0].value because, again, using the type TopLevelFormatterParams does not guarantee the type CallbackDataParams[].

You can't really do anything other than (params: any) since the union of types by TopLevelFormatterParams conflict with each other.

I am new to TypeScript so if there's a solution here that I'm not seeing please let me know...

Edit: I guess instead of any you could assert the type if you know for sure it will be one or the other, then you at least get intellisense (VS Code) working with the various properties available:

import { TooltipComponentOption } from 'echarts'
import { CallbackDataParams } from 'echarts/types/dist/shared.js'

let tooltipConfig: TooltipComponentOption = {
    formatter: function (params) {
        let p = params as CallbackDataParams[]
        return p[0].dimensionNames[0]
    },
}

You could also write a type guard, but you still have to use // @ts-ignore to suppress errors for a few properties due to other typing problems. The code will run fine despite typescript complaining.

import { TooltipComponentOption } from 'echarts'
import { CallbackDataParams } from 'echarts/types/dist/shared.js'

//Type guard for tooltip formatter
function isParamsArray(params: any): params is CallbackDataParams[] {
	return params[0].seriesName !== undefined
}

let tooltipConfig: TooltipComponentOption = {
    formatter: function (params) {
	    if (isParamsArray(params)) {
		    const s1 = params[0]
		    const s2 = params[1]
		    //@ts-ignore
		    const date = new Date(s1.value[s1.encode.x[0]]).toLocaleDateString('en-US', {
			    timeZone: 'UTC', year: 'numeric', month: 'long', day: 'numeric'})
		    //@ts-ignore
		    const s1data = s1.value[s1.encode.y[0]]
		    //@ts-ignore
		    const s2data = s2.value[s2.encode.y[0]]
		    return (
			    `<b>Date:</b> ${date}
			    <br />
			    ${s1.marker} <b>${s1.seriesName}:</b> ${s1data} years
			    <br />
			    ${s2.marker} <b>${s2.seriesName}:</b> ${s2data} years`
		    )
	    }
	    else {
		    //@ts-ignore
		    const date = new Date(params.value[params.encode.x[0]]).toLocaleDateString('en-US', {
			    timeZone: 'UTC', year: 'numeric', month: 'long', day: 'numeric'})
		    //@ts-ignore
		    const sdata = params.value[params.encode.y[0]]
		    return (
			    `<b>Date:</b> ${date}
			    <br />
			    ${params.marker} <b>${params.seriesName}:</b> ${sdata} years`
		    )
	    }
    },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants