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

refactor(tooltip): enhance tooltip #4691

Merged
merged 2 commits into from
Feb 27, 2023
Merged

refactor(tooltip): enhance tooltip #4691

merged 2 commits into from
Feb 27, 2023

Conversation

pearmini
Copy link
Member

@pearmini pearmini commented Feb 24, 2023

Tooltip

修改了配置 Tooltip Items 和 Title 的方式,参考这个 #4491 (comment) 中的讨论。

开始使用

const before = {
  encode: {
    tooltip: 'a',
    tooltip1: 'b',
    title: 'c',
  },
};
// Spec
const after = {
  tooltip: {
    title: 'c',
    items: ['a', 'b']
  }
};

// API
mark.tooltip({
  title: 'c',
  items: ['a', 'b'],
});

结构

tooltip 完整的结构如下:

// Spec
const mark = {
  tooltip: {
    title: 'c',
    items: ['a', 'b']
  }
};

// API
mark.tooltip({
  title: 'c',
  items: ['a', 'b'],
});

有以下两种语法糖:

// Spec
const mark = {
  tooltip: 'a',
};

// API
mark.tooltip('a');

// 等效于
const mark = {
  tooltip: {
    items: ['a'],
  },
};
// Spec
const mark = {
  tooltip: ['a', 'b'],
};

// API
mark.tooltip('a').tooltip('b');

// 等效于
const mark = {
  tooltip: {
    items: ['a', 'b'],
  },
};

title 和 item

title 和每一个 item 除了上述的字符串形式之外,还支持如下形式:

  • 对象形式
// 指定 item 为字段 a
const mark = {
  tooltip: [{ color: 'red', field: 'a' }],
};

// 指定 item 为通道 y 的值
const mark = {
  tooltip: [{ color: 'red', channel: 'y', name: 'Y' }],
};
  • 回调形式
// 指定为字段 a
const mark = {
  tooltip: [(d) => ({ value: d.a, color: 'red' })],
};

// 指定为通道 y
const mark = {
  tooltip: [(d, i, data, value) => ({ value: value.y.value[i], color: 'red' })],
};

@pearmini pearmini force-pushed the refactor/spec-tooltip branch from 03dbf02 to 6eddc23 Compare February 24, 2023 10:57
@pearmini pearmini requested review from hustcc and Aarebecca February 24, 2023 11:25
@pearmini pearmini changed the title [WIP] refactor(tooltip): enhance tooltip refactor(tooltip): enhance tooltip Feb 24, 2023
@pearmini pearmini force-pushed the refactor/spec-tooltip branch from 3e922e3 to 158a79b Compare February 25, 2023 12:15
@pearmini pearmini force-pushed the refactor/spec-tooltip branch from 158a79b to d0b3ac9 Compare February 25, 2023 12:40
@Aarebecca
Copy link
Contributor

const after = {
  tooltip: {
    title: 'c',
    items: ['a', 'b']
  }
};

中 items 是指的映射字段而不是 GUI 里的数据对吧

@pearmini
Copy link
Member Author

const after = {
  tooltip: {
    title: 'c',
    items: ['a', 'b']
  }
};

中 items 是指的映射字段而不是 GUI 里的数据对吧

嗯嗯,不是,是 G2 消费的格式。不过最后会转换成 GUI 里面的格式。

@Aarebecca
Copy link
Contributor

回调形式为啥要用数组包裹呢

// 指定为字段 a
const mark = {
  tooltip: [(d) => ({ value: d.a, color: 'red' })],
};

// 指定为通道 y
const mark = {
  tooltip: [(d, i, data, value) => ({ value: value.y.value[i], color: 'red' })],
};

@pearmini
Copy link
Member Author

回调形式为啥要用数组包裹呢

// 指定为字段 a
const mark = {
  tooltip: [(d) => ({ value: d.a, color: 'red' })],
};

// 指定为通道 y
const mark = {
  tooltip: [(d, i, data, value) => ({ value: value.y.value[i], color: 'red' })],
};

其实是否是回调形式都不需要数组包裹,只不过例子写的数组。

// 也是可以的
const mark = {
  tooltip: (d) => ({ value: d.a, color: 'red' }),
};

@pearmini pearmini merged commit 6b55834 into v5 Feb 27, 2023
@pearmini pearmini deleted the refactor/spec-tooltip branch February 27, 2023 06:21
Aarebecca pushed a commit that referenced this pull request Feb 27, 2023
* refactor(tooltip): enhance tooltip

* feat(tooltip): add mark.tooltip
Aarebecca pushed a commit that referenced this pull request Mar 1, 2023
* refactor(tooltip): enhance tooltip

* feat(tooltip): add mark.tooltip
hustcc pushed a commit that referenced this pull request May 16, 2023
* refactor(tooltip): enhance tooltip

* feat(tooltip): add mark.tooltip
hustcc pushed a commit that referenced this pull request May 16, 2023
* refactor(tooltip): enhance tooltip

* feat(tooltip): add mark.tooltip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants