antvis / antvis/G2

[Bug]: 双轴分面图的图例只显示最后绘制的轴的图例,且左右轴刻度都一样

Open
#7,167 3 comments 0 reactions 0 assignees View on GitHub
waiting for maintainer
Dominant language
TypeScript
Stars
12.6k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

### Describe the bug / 问题描述

我绘制了一个柱形+折线的双轴分面图,但是
1. 图例只显示了后绘制的折线图的图例,交换绘制顺序后,只显示了柱形图的图例,图例缺失一部分。
2. 左右轴的数据值相差较大,但是刻度却自动同步了,这在单独的双轴图中就是按照各自的值生成的刻度
3. y轴只在第一个分面显示了,后边的分面没有y轴,希望第一个分面显示左轴,最后一个分面显示右轴

如下图所示:

Image

复现代码如下:
[g2-demo.html](https://github.com/user-attachments/files/23057096/g2-demo.html)
```javascript
const data = [
{ name: '薪金', date: '20210121', leftKey: '指标1', leftValue: 2.7, rightKey: '指标A', rightValue: 13 },
{ name: '薪金', date: '20210121', leftKey: '指标2', leftValue: 1.5, rightKey: '指标B', rightValue: 18 },
{ name: '薪金', date: '20210121', leftKey: '指标3', leftValue: 3.3, rightKey: '指标C', rightValue: 22 },
{ name: '薪金', date: '20210121', leftKey: '指标4', leftValue: 1.4, rightKey: '指标D', rightValue: 26 },
{ name: '货币', date: '20210121', leftKey: '指标1', leftValue: 2.2, rightKey: '指标A', rightValue: 17 },
{ name: '货币', date: '20210121', leftKey: '指标2', leftValue: 5.6, rightKey: '指标B', rightValue: 44 },
{ name: '货币', date: '20210121', leftKey: '指标3', leftValue: 3.1, rightKey: '指标C', rightValue: 31 },
{ name: '货币', date: '20210121', leftKey: '指标4', leftValue: 2.5, rightKey: '指标D', rightValue: 18 },
{ name: '现金', date: '20210121', leftKey: '指标1', leftValue: 1.3, rightKey: '指标A', rightValue: 29 },
{ name: '现金', date: '20210121', leftKey: '指标2', leftValue: 3.6, rightKey: '指标B', rightValue: 36 },
{ name: '现金', date: '20210121', leftKey: '指标3', leftValue: 2.1, rightKey: '指标C', rightValue: 66 },
{ name: '现金', date: '20210121', leftKey: '指标4', leftValue: 1.5, rightKey: '指标D', rightValue: 52 },

{ name: '薪金', date: '20210122', leftKey: '指标1', leftValue: 4.2, rightKey: '指标A', rightValue: 48 },
{ name: '薪金', date: '20210122', leftKey: '指标2', leftValue: 2.8, rightKey: '指标B', rightValue: 33 },
{ name: '薪金', date: '20210122', leftKey: '指标3', leftValue: 5.1, rightKey: '指标C', rightValue: 65 },
{ name: '薪金', date: '20210122', leftKey: '指标4', leftValue: 1.9, rightKey: '指标D', rightValue: 21 },
{ name: '货币', date: '20210122', leftKey: '指标1', leftValue: 3.6, rightKey: '指标A', rightValue: 55 },
{ name: '货币', date: '20210122', leftKey: '指标2', leftValue: 4.9, rightKey: '指标B', rightValue: 41 },
{ name: '货币', date: '20210122', leftKey: '指标3', leftValue: 2.3, rightKey: '指标C', rightValue: 38 },
{ name: '货币', date: '20210122', leftKey: '指标4', leftValue: 5.4, rightKey: '指标D', rightValue: 67 },
{ name: '现金', date: '20210122', leftKey: '指标1', leftValue: 1.7, rightKey: '指标A', rightValue: 25 },
{ name: '现金', date: '20210122', leftKey: '指标2', leftValue: 3.3, rightKey: '指标B', rightValue: 59 },
{ name: '现金', date: '20210122', leftKey: '指标3', leftValue: 4.0, rightKey: '指标C', rightValue: 44 },
{ name: '现金', date: '20210122', leftKey: '指标4', leftValue: 2.1, rightKey: '指标D', rightValue: 30 },

{ name: '薪金', date: '20210123', leftKey: '指标1', leftValue: 3.8, rightKey: '指标A', rightValue: 52 },
{ name: '薪金', date: '20210123', leftKey: '指标2', leftValue: 1.2, rightKey: '指标B', rightValue: 27 },
{ name: '薪金', date: '20210123', leftKey: '指标3', leftValue: 4.5, rightKey: '指标C', rightValue: 39 },
{ name: '薪金', date: '20210123', leftKey: '指标4', leftValue: 2.6, rightKey: '指标D', rightValue: 43 },
{ name: '货币', date: '20210123', leftKey: '指标1', leftValue: 2.9, rightKey: '指标A', rightValue: 22 },
{ name: '货币', date: '20210123', leftKey: '指标2', leftValue: 5.2, rightKey: '指标B', rightValue: 50 },
{ name: '货币', date: '20210123', leftKey: '指标3', leftValue: 3.7, rightKey: '指标C', rightValue: 29 },
{ name: '货币', date: '20210123', leftKey: '指标4', leftValue: 1.8, rightKey: '指标D', rightValue: 15 },
{ name: '现金', date: '20210123', leftKey: '指标1', leftValue: 4.1, rightKey: '指标A', rightValue: 61 },
{ name: '现金', date: '20210123', leftKey: '指标2', leftValue: 2.4, rightKey: '指标B', rightValue: 34 },
{ name: '现金', date: '20210123', leftKey: '指标3', leftValue: 3.9, rightKey: '指标C', rightValue: 70 },
{ name: '现金', date: '20210123', leftKey: '指标4', leftValue: 1.3, rightKey: '指标D', rightValue: 24 },
];

// 初始化图表实例
const chart = new G2.Chart({
container: 'chart',
width: 800,
height: 400,
paddingTop: 120,
paddingRight: 60,
paddingBottom: 60,
paddingLeft: 60,
});

const facetRect = chart.facetRect().data(data).encode('x', 'name');
const facetView = facetRect.view();
facetView.attr('frame', false)
.interaction('tooltip', {crosshairs: false, shared: true})
.interaction('elementHighlight', {background: true, region: true});
facetView.interval()
.encode('x', 'date')
.encode('y', 'leftValue')
.encode('color', 'leftKey')
.axis('x', {title: null})
.axis('y', {title: null})
.transform({type: 'stackY'});
facetView.line()
.encode('x', 'date')
.encode('y', 'rightValue')
.encode('color', 'rightKey')
.axis('x', {title: null})
.axis('y', {position: 'right',title: null})
.scale('y', {independent: true});

chart.render();
```

### Reproduction link / 复现链接

_No response_

### Steps to Reproduce the Bug or Issue / 重现步骤

_No response_

### Version / 版本

🆕 5.x

### OS / 操作系统

- [ ] macOS
- [x] Windows
- [ ] Linux
- [ ] Others / 其他

### Browser / 浏览器

- [x] Chrome
- [ ] Edge
- [ ] Firefox
- [ ] Safari (Limited support / 有限支持)
- [ ] IE (Nonsupport / 不支持)
- [ ] Others / 其他

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied g2-demo.html reproduction and inspect the facetRect/facetView configuration, especially the interval and line layers and their y scales. The fix should preserve both layers' legend entries, keep the left and right scales independent, and show the requested left and right axes on the first and last facets.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.