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

饼图模式下 color 配置成值对应的字段,生成的图例是连续性图例,这个时候如果配置了label,只显示最后一个 #3160

Closed
1 task
jefferyssy opened this issue Dec 25, 2020 · 2 comments · Fixed by #3366
Labels

Comments

@jefferyssy
Copy link

jefferyssy commented Dec 25, 2020

  • I have searched the issues of this repository and believe that this is not a duplicate.

Reproduction link

Edit on CodeSandbox

Steps to reproduce

const data = [
  { item: "事例一", count: 40, percent: 0.4 },
  { item: "事例二", count: 21, percent: 0.21 },
  { item: "事例三", count: 17, percent: 0.17 },
  { item: "事例四", count: 13, percent: 0.13 },
  { item: "事例五", count: 9, percent: 0.09 }
];
chart
  .interval()
  .adjust("stack")
  .position("percent")
  .color("count")
  .label("percent", (percent) => {
    return {
      content: (data) => {
        return `${data.item}: ${percent * 100}%`;
      }
    };
  })

将 color从item 改成 item
这个时候是连续性图例,展示也正常,当时 label 配置不正确了

Environment Info
g2 4.1.4
System -
Browser -
@jefferyssy
Copy link
Author

查看了源码
G2/src/geometry/base.ts 文件

private initAttributes() {
 *****
        // 获取每一个字段对应的 scale
        const scales = fields.map((field) => {
          const scale = this.scales[field];
          if (scale.isCategory && !tmpMap[field] && GROUP_ATTRS.includes(attrType)) {
            this.groupScales.push(scale);
            tmpMap[field] = true;
          }
          return scale;
        });
****
  }

public getElementId(data: MappingDatum | MappingDatum[]) {
    *****
    for (let index = 0, length = groupScales.length; index < length; index++) {
      const groupScale = groupScales[index];
      const field = groupScale.field;
      id = `${id}-${originData[field]}`;
    }
   ********
    return id;
  }

在color对应字段的scale 为 linear的时候 scale.isCategory = false。
所有groupScales没有生成。后面getElementId的时候id没法加上 label标签字段的关键词,生成的id 都是 "1"

G2/src/component/labels.ts

public render(items: LabelItem[], shapes: Record<string, IShape | IGroup>, isUpdate: boolean = false) {
  ****
      for (const item of items) {
        if (item) {
          this.renderLabel(item, offscreenGroup);
        }
      }
 ****
    }

  private renderLabel(cfg: LabelItem, container: IGroup) {
    const { id, elementId, data, mappingData, coordinate, animate, content } = cfg;
   ***
    this.shapesMap[id] = labelGroup;
  }

由于 lable列表中,id都是“1” 循环litem生成shape的时候总是覆盖。所有一直只显示最后一个的label

@hustcc
Copy link
Member

hustcc commented Jan 29, 2021

@jefferyssy 是否有意愿来一个 pr。

@hustcc hustcc added the Label label Jan 29, 2021
visiky added a commit that referenced this issue Apr 10, 2021
color 字段为 linear 类型时,不会存入 groupScales,导致获取 label-id 不唯一

closed: #3160
hustcc pushed a commit that referenced this issue Apr 12, 2021
* fix(issue-3160): 修复饼图使用连续型图例,标签无法展示

color 字段为 linear 类型时,不会存入 groupScales,导致获取 label-id 不唯一

closed: #3160

* fix(spider): 修复 get 获取属性值,会对 dot 进行解析

* test: 补充 bugs 单测

* fix: 修改 cr 建议,不需要重新生成
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants