Skip to content

fix(bar): adjust default legend and tooltip order #2049

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

Merged
merged 4 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions __tests__/unit/plots/bar/index-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,40 @@ describe('bar', () => {
expect(theme.defaultColor).toBe('#FF6B3B');
expect(theme.columnWidthRatio).toBe(0.1);
});

it('legend/tooltip reversed, grouped', () => {
const bar = new Bar(createDiv('group'), {
width: 300,
height: 400,
data: subSalesByArea,
yField: 'area',
xField: 'sales',
seriesField: 'series',
isGroup: true,
});
bar.render();

// @ts-ignore
expect(bar.chart.getOptions().legends['series'].reversed).toBe(true);
// @ts-ignore
expect(bar.chart.getOptions().tooltip.reversed).toBe(true);
});

it('legend/tooltip reversed, stacked', () => {
const bar = new Bar(createDiv('group'), {
width: 300,
height: 400,
data: subSalesByArea,
yField: 'area',
xField: 'sales',
seriesField: 'series',
isStack: true,
});
bar.render();

// @ts-ignore
expect(bar.chart.getOptions().legends['series'].reversed).toBe(false);
// @ts-ignore
expect(bar.chart.getOptions().tooltip?.reversed).toBe(false);
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
"dependencies": {
"@antv/event-emitter": "^0.1.2",
"@antv/g2": "^4.1.0-beta.21",
"@antv/g2": "^4.1.0",
"d3-hierarchy": "^2.0.0",
"d3-regression": "^1.3.5",
"dayjs": "^1.8.36",
Expand Down
16 changes: 15 additions & 1 deletion src/plots/bar/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export function adaptor(params: Params<BarOptions>) {
if (legend !== false) {
legend = {
position: isStack ? 'top-left' : 'right-top',
...legend,
reversed: isStack ? false : true,
...(legend || {}),
};
}
} else {
Expand All @@ -31,6 +32,19 @@ export function adaptor(params: Params<BarOptions>) {
// @ts-ignore 直接改值
params.options.legend = legend;

// 默认 tooltip 配置
let { tooltip } = options;
if (seriesField) {
if (tooltip !== false) {
tooltip = {
reversed: isStack ? false : true,
...(tooltip || {}),
};
}
}
// @ts-ignore 直接改值
params.options.tooltip = tooltip;

// transpose column to bar
chart.coordinate().transpose();

Expand Down