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

add valueFormatter in tooltip #16332

Merged
merged 3 commits into from
Jan 6, 2022
Merged

add valueFormatter in tooltip #16332

merged 3 commits into from
Jan 6, 2022

Conversation

pissang
Copy link
Contributor

@pissang pissang commented Jan 6, 2022

Brief Information

This pull request is in the type of:

  • bug fixing
  • new feature
  • others

What does this PR do?

This PR added a new option valueFormatter. It's the most commonly used formatter scenario we've met. See more detail in #14962

API

interface valueFormatter {
  (value: number | string | (string | number)[]) => string
}

Parameter value is the number or string(category) that displayed in each row. For example, in the following tooltip:

Each row will call the valueFormatter once and the value will be 0.3, 12.71, 0 respectively.

NOTE: valueFormatter will take no effect if formatter is used.

Fixed issues

#14962
#15430

Details

Before: What was the problem?

We need to replace the whole HTML with formatter if we only want format the numbers.

After: How is it fixed in this PR?

Use valueFormatter to only format the numbers.

Misc

  • The API has been changed (apache/echarts-doc#xxx).
  • This PR depends on ZRender changes (ecomfe/zrender#xxx).

Related test cases or examples to use the new APIs

Basic usage: adding %

tooltip: {
    position: 'top',
    valueFormatter: (val) => val + '%'
},

Using different formatter in different series.

series: [{
    name: 'CPU',
    type: 'line',
    showSymbol: false,
    tooltip: {
        valueFormatter: (value) => value + ' %'
    },
    ...
}, {
    name: 'RAM',
    type: 'line',
    yAxisIndex: 1,
    tooltip: {
        valueFormatter: (value) => value + ' MB'
    }
    ...
}

148333759-ec875352-8ecc-4a64-a109-6d977021578e

Others

Merging options

  • Please squash the commits into a single one when merging.

Other information

@echarts-bot
Copy link

echarts-bot bot commented Jan 6, 2022

Thanks for your contribution!
The community will review it ASAP. In the meanwhile, please checkout the coding standard and Wiki about How to make a pull request.

The pull request is marked to be PR: author is committer because you are a committer of this project.

Document changes are required in this PR. Please also make a PR to apache/echarts-doc for document changes and update the issue id in the PR description. When the doc PR is merged, the maintainers will remove the PR: awaiting doc label.

@echarts-bot echarts-bot bot added PR: author is committer PR: awaiting doc Document changes is required for this PR. PR: awaiting review labels Jan 6, 2022
@pissang pissang requested review from plainheart, 100pah, susiwen8 and Ovilia and removed request for plainheart January 6, 2022 05:39
@pissang pissang added this to the 5.3 milestone Jan 6, 2022
@plainheart
Copy link
Member

plainheart commented Jan 6, 2022

What if we set a valueFormatter for the global tooltip? Should the valueFormatter callback include all the series values? The first argument in formatter callback will include all the series values. But it seems the current valueFormatter includes only one series value.

global formatter global valueFormatter
image image

@pissang
Copy link
Contributor Author

pissang commented Jan 6, 2022

@plainheart Because formatter is for the whole tooltip. It returns one single HTML string for the tooltip. And valueFormatter will be called for each value. It's also returns one string after formatted. It's more consistent and easier for developer to handle.

If we return an array. Developers need to check if value is array and loop them to process each one. And they may be confused if they should return an array or a combined string.

There are cases that different series need to have different formatted result. The series.tooltip.formatter metioned in the description exists for this situation. We can also add a second param that includes seriesIndex, dataIndex for more information. It's like callback in xAxis.axisLabel.formatter. But I prefer to keep it simple at first.

Copy link
Member

@plainheart plainheart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well. LGTM.

@pissang pissang merged commit 60c0fdf into next Jan 6, 2022
@echarts-bot
Copy link

echarts-bot bot commented Jan 6, 2022

Congratulations! Your PR has been merged. Thanks for your contribution! 👍

@pissang pissang deleted the tooltip-value-formatter branch January 6, 2022 13:00
pissang added a commit to apache/echarts-doc that referenced this pull request Jan 19, 2022
@crazyair
Copy link

等发正式版,刚试了下 rc,控制台一堆报错。

@susiwen8
Copy link
Contributor

@crazyair 能提供下复现示例吗?

@crazyair
Copy link

@crazyair 能提供下复现示例吗?

image

@susiwen8
Copy link
Contributor

@crazyair 能提供下复现示例吗?

image

这个能提供最小复现示例吗?

@pissang
Copy link
Contributor Author

pissang commented Jan 26, 2022

@crazyair 看了下报错信息,可能是渐变色动画引起的,可以检查下配置项中的渐变色配置是否有使用非法的颜色字符串(无法被解析的)

然后最好能够像 @susiwen8 建议的给我们提供一个最小复现的例子,方便我们看是否可以在这块再优化下

@crazyair
Copy link

@crazyair 看了下报错信息,可能是渐变色动画引起的,可以检查下配置项中的渐变色配置是否有使用非法的颜色字符串(无法被解析的)

对,是使用了渐变色,由于示例死循环了非常卡,渐变色我是这样配置的

_options = {
            tooltip: { trigger: 'axis' },
            grid: { left: 5, right: 5, bottom: 5, top: 5 },
            xAxis: [
                {
                    show: false,
                    type: 'category',
                    boundaryGap: false,
                },
            ],
            yAxis: [{ show: false, type: 'value' }],
            series: [
                {
                    type: 'line',
                    smooth: true,
                    lineStyle: { width: 2 },
                    showSymbol: false,
                    areaStyle: {
                        opacity: 0.2,
                        color: new graphic.LinearGradient(0, 0, 0, 1, [
                            { offset: 0, color: `${get(options, ['color', 0])}` },
                            { offset: 1, color: 'rgb(255, 255, 255)' },
                        ]),
                    },
                },
            ],
        };

起始 color 是 options.color[0] 的值 rgb(220 221 251)

@crazyair
Copy link

@crazyair 能提供下复现示例吗?

https://codesandbox.io/s/pensive-cloud-f8n8b?file=/src/index.js

@plainheart
Copy link
Member

rgb 的逗号呢?还是用法不规范啊...

@plainheart plainheart removed the PR: awaiting doc Document changes is required for this PR. label Jan 26, 2022
@crazyair
Copy link

rgb 的逗号呢?还是用法不规范啊...

尴尬,眼花了,vscode 在这里会有背景色,没看到有没有逗号

@AN-535
Copy link

AN-535 commented Jan 27, 2022

image
好像valueFormatter无效

@plainheart
Copy link
Member

可能是缓存,清一下缓存刷新再试。

@AN-535
Copy link

AN-535 commented Jan 27, 2022

感谢,强制刷新后解决了

@Cat1007
Copy link

Cat1007 commented Mar 24, 2023

后续可以添加dataIndex吗

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

Successfully merging this pull request may close these issues.

None yet

6 participants