Skip to content

Commit d292545

Browse files
authored
fix(bar): adjust default legend and tooltip order (#2049)
* fix(bar): adjust default legend and tooltip order * fix(bar): fix unit test * chore: increase size limit * feat: disable autoRotate by default
1 parent 2fb711c commit d292545

File tree

7 files changed

+58
-7
lines changed

7 files changed

+58
-7
lines changed

__tests__/unit/plots/bar/index-spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,40 @@ describe('bar', () => {
365365
expect(theme.defaultColor).toBe('#FF6B3B');
366366
expect(theme.columnWidthRatio).toBe(0.1);
367367
});
368+
369+
it('legend/tooltip reversed, grouped', () => {
370+
const bar = new Bar(createDiv('group'), {
371+
width: 300,
372+
height: 400,
373+
data: subSalesByArea,
374+
yField: 'area',
375+
xField: 'sales',
376+
seriesField: 'series',
377+
isGroup: true,
378+
});
379+
bar.render();
380+
381+
// @ts-ignore
382+
expect(bar.chart.getOptions().legends['series'].reversed).toBe(true);
383+
// @ts-ignore
384+
expect(bar.chart.getOptions().tooltip.reversed).toBe(true);
385+
});
386+
387+
it('legend/tooltip reversed, stacked', () => {
388+
const bar = new Bar(createDiv('group'), {
389+
width: 300,
390+
height: 400,
391+
data: subSalesByArea,
392+
yField: 'area',
393+
xField: 'sales',
394+
seriesField: 'series',
395+
isStack: true,
396+
});
397+
bar.render();
398+
399+
// @ts-ignore
400+
expect(bar.chart.getOptions().legends['series'].reversed).toBe(false);
401+
// @ts-ignore
402+
expect(bar.chart.getOptions().tooltip?.reversed).toBe(false);
403+
});
368404
});

__tests__/unit/plots/bar/legend-spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('bar legend', () => {
3232

3333
bar.render();
3434
// @ts-ignore
35-
expect(bar.chart.getOptions().legends.series).toEqual({ position: 'right-top' });
35+
expect(bar.chart.getOptions().legends.series).toEqual({ position: 'right-top', reversed: true });
3636
expect(bar.chart.getComponents().filter((co) => co.type === 'legend').length).toBe(1);
3737

3838
bar.update({
@@ -46,6 +46,7 @@ describe('bar legend', () => {
4646
expect(bar.chart.getOptions().legends.series).toEqual({
4747
position: 'right-top',
4848
flipPage: true,
49+
reversed: true,
4950
});
5051
expect(bar.chart.getComponents().filter((co) => co.type === 'legend').length).toBe(1);
5152

__tests__/unit/plots/histogram/axis-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('Histogram: axis', () => {
113113
nice: true,
114114
label: {
115115
autoHide: true,
116-
autoRotate: true,
116+
autoRotate: false,
117117
},
118118
});
119119

__tests__/unit/plots/histogram/index-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('histogram', () => {
3737
nice: true,
3838
label: {
3939
autoHide: true,
40-
autoRotate: true,
40+
autoRotate: false,
4141
},
4242
});
4343

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
},
5656
"dependencies": {
5757
"@antv/event-emitter": "^0.1.2",
58-
"@antv/g2": "^4.1.0-beta.21",
58+
"@antv/g2": "^4.1.0",
5959
"d3-hierarchy": "^2.0.0",
6060
"d3-regression": "^1.3.5",
6161
"dayjs": "^1.8.36",
@@ -135,7 +135,7 @@
135135
"limit-size": [
136136
{
137137
"path": "dist/g2plot.min.js",
138-
"limit": "870 Kb"
138+
"limit": "900 Kb"
139139
},
140140
{
141141
"path": "dist/g2plot.min.js",

src/core/plot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export abstract class Plot<O extends PickOptions> extends EE {
112112
xAxis: {
113113
nice: true,
114114
label: {
115-
autoRotate: true,
115+
autoRotate: false,
116116
autoHide: true,
117117
},
118118
},

src/plots/bar/adaptor.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export function adaptor(params: Params<BarOptions>) {
2222
if (legend !== false) {
2323
legend = {
2424
position: isStack ? 'top-left' : 'right-top',
25-
...legend,
25+
reversed: isStack ? false : true,
26+
...(legend || {}),
2627
};
2728
}
2829
} else {
@@ -31,6 +32,19 @@ export function adaptor(params: Params<BarOptions>) {
3132
// @ts-ignore 直接改值
3233
params.options.legend = legend;
3334

35+
// 默认 tooltip 配置
36+
let { tooltip } = options;
37+
if (seriesField) {
38+
if (tooltip !== false) {
39+
tooltip = {
40+
reversed: isStack ? false : true,
41+
...(tooltip || {}),
42+
};
43+
}
44+
}
45+
// @ts-ignore 直接改值
46+
params.options.tooltip = tooltip;
47+
3448
// transpose column to bar
3549
chart.coordinate().transpose();
3650

0 commit comments

Comments
 (0)