【V5】Tooltip title 设置常量不生效 #5297
BinghuiXie posted onGitHub
<!-- Thank you for reporting an issue. 1. It's RECOMMENDED to submit PR for typo or tiny bug fix. 2. If this's a FEATURE request, please provide: details, pseudo codes if necessary. 3. If this's a BUG, please provide: course repetition, error log and configuration. Fill in as much of the template below as you're able. 4. It will be nice to use to provide a CodePen Link which can reproduce the issue, we provide a CodePen template g2-github-issue. 感谢您向我们反馈问题。 1. 提交问题前,请先阅读 README 中的贡献帮助文档。 2. 我们推荐如果是小问题(错别字修改,小的 bug fix)直接提交 PR。 3. 如果是一个新需求,请提供:详细需求描述,最好是有伪代码实现。 4. 如果是一个 BUG,请提供:复现步骤,错误日志以及相关配置,并尽量填写下面的模板中的条目。 5. 如果可以,请提供尽可能精简的 CodePen 链接,可使用 CodePen 模板 https://codepen.io/leungwensen/pen/WXJgox,方便我们排查问题。 6. 扩展阅读:如何向开源项目提交无法解答的问题 -->
- G2 Version: 官网 Demo
- Platform:
- Mini Showcase(like screenshots):
- CodePen Link:
<!-- Enter your issue details below this comment. -->
V5 中 Tooltip 的 title 设置常量不生效,只有设置 datum
里面的属性才生效
import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
theme: 'classic',
autoFit: true,
});
chart
.interval()
.data({
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/fb9db6b7-23a5-4c23-bbef-c54a55fee580.csv',
})
.encode('x', 'letter')
.encode('y', 'frequency')
.axis('y', { labelFormatter: '.0%' })
.tooltip({
title: '12345678',
items: [{ channel: 'x' }, { channel: 'y' }]
});
chart.interaction('tooltip', {
enterable: true
})
chart.render();
看了下代码感觉是这里有一些问题
// src/runtime/transform.ts
export function extractTooltip(
I: number[],
mark: G2Mark,
context: TransformContext,
): [number[], G2Mark] {
// ...
const valueOf = (item) => {
if (!item) return item;
if (typeof item === 'string') {
// 这里的 value 取的是 data 上面的属性,由于传入的 item 是常量,所以这里拿到的是 undefined
return I.map((i) => ({ name: item, value: data[i][item] }))
}
// ...
}
// 导致在后面 initializeState 的时候,通过 titleOf 方法拿到的每个 element 上的 data 中的 title 就是 undefined
// src/runtime/plot.ts initializeState
function initializeState(
// ...
) {
// ...
const titleOf = (i) => tooltip.title?.[i]?.value;
// ...
const visualData: Record<string, any>[] = I.map((d, i) => {
const datum = {
points: P[i],
transform: T[i],
index: d,
markKey,
viewKey: key,
...(tooltip && {
// 这里拿到的 title 就是 undefined
title: titleOf(d),
items: itemsOf(d),
}),
};
// ...
}
在最终绘制 Tooltip
的时候,会因为当前 hover 的 element
的数据中,title
为 undefined,而不展示提示框标题