Skip to content

Commit

Permalink
fix(bar): adjust default legend and tooltip order (#2049)
Browse files Browse the repository at this point in the history
* fix(bar): adjust default legend and tooltip order

* fix(bar): fix unit test

* chore: increase size limit

* feat: disable autoRotate by default
  • Loading branch information
lessmost authored Dec 2, 2020
1 parent 2fb711c commit d292545
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
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);
});
});
3 changes: 2 additions & 1 deletion __tests__/unit/plots/bar/legend-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('bar legend', () => {

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

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

Expand Down
2 changes: 1 addition & 1 deletion __tests__/unit/plots/histogram/axis-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('Histogram: axis', () => {
nice: true,
label: {
autoHide: true,
autoRotate: true,
autoRotate: false,
},
});

Expand Down
2 changes: 1 addition & 1 deletion __tests__/unit/plots/histogram/index-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('histogram', () => {
nice: true,
label: {
autoHide: true,
autoRotate: true,
autoRotate: false,
},
});

Expand Down
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -135,7 +135,7 @@
"limit-size": [
{
"path": "dist/g2plot.min.js",
"limit": "870 Kb"
"limit": "900 Kb"
},
{
"path": "dist/g2plot.min.js",
Expand Down
2 changes: 1 addition & 1 deletion src/core/plot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export abstract class Plot<O extends PickOptions> extends EE {
xAxis: {
nice: true,
label: {
autoRotate: true,
autoRotate: false,
autoHide: true,
},
},
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

0 comments on commit d292545

Please sign in to comment.