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

fix(label): 修复label显示错误的问题 #6172

Merged
merged 1 commit into from
Apr 22, 2024
Merged

Conversation

BENcorry
Copy link
Contributor

@BENcorry BENcorry commented Apr 12, 2024

close: #5960

解决问题

借助图形标注展示总计 label 显示错误

解决办法

原因:label显示问题的主要原因是在处理valueOf时,没有对datum类型的判断导致取值时datum为字符串,value为数字字符串‘3’,结果就是字符串的第三位,显示异常
解决办法:给datum增加类型判断即可

####### Case

import { G2Spec } from '../../../src';

export const data = [
  {
    time: '1991/01',
    value: 0,
    type: 'Lon',
  },
  {
    time: '1992/01',
    value: 4,
    type: 'Lon',
  },
  {
    time: '1993/01',
    value: 3.5,
    type: 'Lon',
  },
  {
    time: '1994/01',
    value: 1,
    type: 'Lon',
  },
  {
    time: '1995/01',
    value: 2,
    type: 'Lon',
  },
  {
    time: '1996/01',
    value: 3,
    type: 'Lon',
  },
  {
    time: '1997/01',
    value: 4,
    type: 'Lon',
  },
  {
    time: '1998/01',
    value: 9,
    type: 'Lon',
  },
  {
    time: '1999/01',
    value: 1,
    type: 'Lon',
  },
  {
    time: '1991/01',
    value: 3,
    type: 'Bor',
  },
  {
    time: '1992/01',
    value: 4,
    type: 'Bor',
  },
  {
    time: '1993/01',
    value: 3.5,
    type: 'Bor',
  },
  {
    time: '1994/01',
    value: 5,
    type: 'Bor',
  },
  {
    time: '1995/01',
    value: 2,
    type: 'Bor',
  },
  {
    time: '1996/01',
    value: 6,
    type: 'Bor',
  },
  {
    time: '1997/01',
    value: 7,
    type: 'Bor',
  },
  {
    time: '1998/01',
    value: 9,
    type: 'Bor',
  },
  {
    time: '1999/01',
    value: 1,
    type: 'Bor',
  },
];

export function stackBarAnnotationStyleText(): G2Spec {
  const groupData = data.reduce((acc, item) => {
    if (acc[item.time]) {
      acc[item.time] = [...acc[item.time], item];
    } else {
      acc[item.time] = [item];
    }
    return acc;
  }, {});

  const annotations = Object.keys(groupData).map((key) => {
    const val = groupData[key].reduce((a, b) => a + b.value, 0);
    console.log('val-=====>', val);
    return {
      type: 'text',
      data: [key, val],
      encode: {
        x: 'time',
        y: 'value',
      },
      style: {
        text: `${val}`,
        textBaseline: 'bottom',
        position: 'top',
        textAlign: 'center',
        fontSize: 14,
        fill: 'rgba(0,0,0,0.85)',
      },
      tooltip: false,
    };
  });
  return {
    type: 'view',
    height: 300,
    width: 640,
    data,
    children: [
      {
        type: 'interval',
        axis: { x: { labelAutoHide: true } },
        encode: {
          x: 'time',
          y: 'value',
          color: 'type',
        },
        transform: [
          {
            type: 'stackY',
          },
        ],
        labels: [
          {
            text: 'value',
            textBaseline: 'bottom',
            position: 'inside',
          },
        ],
      },
      ...annotations,
    ],
  };
}

处理前
image
处理后
image

@BENcorry BENcorry force-pushed the v5 branch 3 times, most recently from 3d60e98 to 37714c0 Compare April 13, 2024 07:48
@BENcorry
Copy link
Contributor Author

BENcorry commented Apr 13, 2024

比较奇怪我本地测试能过,github CI过不了
image

@coveralls
Copy link

coveralls commented Apr 13, 2024

Pull Request Test Coverage Report for Build 8673974096

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.003%) to 86.431%

Totals Coverage Status
Change from base Build 8611256665: 0.003%
Covered Lines: 10290
Relevant Lines: 11533

💛 - Coveralls

@BENcorry
Copy link
Contributor Author

比较奇怪我本地测试能过,github CI过不了 image

node版本必须和ci环境一致才行,16.20.2

src/runtime/plot.ts Outdated Show resolved Hide resolved
src/runtime/plot.ts Outdated Show resolved Hide resolved
Copy link
Member

@pearmini pearmini left a comment

Choose a reason for hiding this comment

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

LGTM,解决的很简洁!

@pearmini pearmini merged commit 51d1c51 into antvis:v5 Apr 22, 2024
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

借助图形标注展示总计 label 显示错误
3 participants