Skip to content

Commit

Permalink
fix: 调整 bar 默认的柱子顺序 (#1828)
Browse files Browse the repository at this point in the history
* fix(bar): adjust bar data sequence

* docs: update bar screenshot

Co-authored-by: lingdao.lzq <lingdao.lzq@antfin.com>
  • Loading branch information
lessmost and lingdao.lzq authored Oct 30, 2020
1 parent a1c7d52 commit 5e13404
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 25 deletions.
16 changes: 5 additions & 11 deletions __tests__/unit/adaptor/conversion-tag-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ describe('bar conversion tag', () => {
animation: false,
};
const plot = new Bar(container, options);
const DATA_REVERSED = DATA.slice().reverse();

it('render', async () => {
plot.render();
Expand All @@ -175,12 +174,10 @@ describe('bar conversion tag', () => {
// 文本
const texts = group.findAllByName('conversion-tag-text');
expect(texts).toHaveLength(DATA.length - 1);
DATA_REVERSED.forEach((datum, idx) => {
DATA.forEach((datum, idx) => {
if (idx > 0) {
expect(texts[idx - 1].get('type')).toBe('text');
expect(texts[idx - 1].attr('text')).toBe(
((DATA_REVERSED[idx].y / DATA_REVERSED[idx - 1].y) * 100).toFixed(2) + '%'
);
expect(texts[idx - 1].attr('text')).toBe(((DATA[idx].y / DATA[idx - 1].y) * 100).toFixed(2) + '%');
}
});

Expand Down Expand Up @@ -222,12 +219,10 @@ describe('bar conversion tag', () => {
// 文本
const texts = group.findAllByName('conversion-tag-text');
expect(texts).toHaveLength(DATA.length - 1);
DATA_REVERSED.forEach((datum, idx) => {
DATA.forEach((datum, idx) => {
if (idx > 0) {
expect(texts[idx - 1].get('type')).toBe('text');
expect(texts[idx - 1].attr('text')).toBe(
((DATA_REVERSED[idx].y / DATA_REVERSED[idx - 1].y) * 100).toFixed(2) + '%'
);
expect(texts[idx - 1].attr('text')).toBe(((DATA[idx].y / DATA[idx - 1].y) * 100).toFixed(2) + '%');
}
});

Expand Down Expand Up @@ -261,9 +256,8 @@ describe('bar conversion tag', () => {
describe('zero data no NaN', () => {
const container = createDiv();

const DATA_REVERSED = DATA_WITH_ZERO.slice().reverse();
const plot = new Bar(container, {
data: DATA_REVERSED,
data: DATA_WITH_ZERO,
autoFit: false,
width: 600,
height: 400,
Expand Down
4 changes: 3 additions & 1 deletion __tests__/unit/plots/bar/label-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ describe('bar label', () => {
});
expect(labelGroups).toHaveLength(salesByArea.length);
labelGroups.forEach((label, index) => {
expect(label.get('children')[0].attr('text')).toBe(`${Math.floor(salesByArea[index].sales / 10000)}万`);
expect(label.get('children')[0].attr('text')).toBe(
`${Math.floor(salesByArea[salesByArea.length - index - 1].sales / 10000)}万`
);
});
});

Expand Down
7 changes: 5 additions & 2 deletions __tests__/unit/plots/column/percent-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Column } from '../../../../src';
import { createDiv } from '../../../utils/dom';
import { delay } from '../../../utils/delay';
const data = [
{ country: 'Europe', year: '1750', value: 163 },
{ country: 'Europe', year: '1800', value: 203 },
Expand All @@ -20,7 +21,7 @@ const data = [
];

describe('column percent', () => {
it('percent: render', () => {
it('percent: render', async () => {
const column = new Column(createDiv('percent'), {
width: 400,
height: 300,
Expand All @@ -43,6 +44,7 @@ describe('column percent', () => {
});

column.render();
await delay(300);
const geometry = column.chart.geometries[0];
const labelGroups = geometry.labelsContainer.getChildren();
const elements = geometry.elements;
Expand All @@ -60,7 +62,7 @@ describe('column percent', () => {
expect(cfg.position).toBe('middle');
expect(cfg.content).not.toBeUndefined();
});
it('percent: custom content', () => {
it('percent: custom content', async () => {
const column = new Column(createDiv('percent'), {
width: 400,
height: 300,
Expand All @@ -87,6 +89,7 @@ describe('column percent', () => {
});

column.render();
await delay(300);
const geometry = column.chart.geometries[0];
const elements = geometry.elements;
const bbox = elements[elements.length - 1].getBBox();
Expand Down
2 changes: 1 addition & 1 deletion docs/common/slider.en.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
object 类型的请参考[绘图属性](../../docs/manual/graphic-style)
object 类型的请参考[绘图属性](../../manual/graphic-style)

| 配置项 | 类型 | 功能描述 |
| --------------- | -------- | ------------------ |
Expand Down
2 changes: 1 addition & 1 deletion docs/common/slider.zh.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
object 类型的请参考[绘图属性](../../docs/manual/graphic-style)
object 类型的请参考[绘图属性](../../manual/graphic-style)

| 配置项 | 类型 | 功能描述 |
| --------------- | -------- | ------------------ |
Expand Down
16 changes: 12 additions & 4 deletions examples/bar/basic/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,39 @@
"zh": "基础条形图",
"en": "Basic Bar chart"
},
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*uSKqQo2lCPoAAAAAAAAAAABkARQnAQ"
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*cnRsQo-Yqg0AAAAAAAAAAAAAARQnAQ"
},
{
"filename": "color.ts",
"title": {
"zh": "基础条形图 - 自定义颜色",
"en": "Bar chart color"
},
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*ZQIiR6SyS9oAAAAAAAAAAAAAARQnAQ"
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*U-D1S4w5HNsAAAAAAAAAAAAAARQnAQ"
},
{
"filename": "width.ts",
"title": {
"zh": "基础条形图 - 柱子宽度",
"en": "Bar chart Bar width"
},
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*7rbzT5Fx5H8AAAAAAAAAAAAAARQnAQ"
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*uqtcQJ-N-qUAAAAAAAAAAAAAARQnAQ"
},
{
"filename": "scrollbar.ts",
"title": {
"zh": "基础条形图 - 滚动条",
"en": "Bar chart with scrollbar"
},
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*01-mRqASP78AAAAAAAAAAAAAARQnAQ"
},
{
"filename": "conversion-tag.ts",
"title": {
"zh": "基础条形图 - 转化率",
"en": "Bar chart conversion tag"
},
"screenshot": "https://gw.alicdn.com/tfs/TB107ZbvKT2gK0jSZFvXXXnFXXa-1344-828.png"
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*-9CLRItZrPMAAAAAAAAAAAAAARQnAQ"
}
]
}
21 changes: 21 additions & 0 deletions examples/bar/basic/demo/scrollbar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Bar } from '@antv/g2plot';

fetch('https://gw.alipayobjects.com/os/bmw-prod/be63e0a2-d2be-4c45-97fd-c00f752a66d4.json')
.then((res) => res.json())
.then((data) => {
const column = new Bar('container', {
data,
yField: '城市',
xField: '销售额',
yAxis: {
label: {
autoRotate: false,
},
},
scrollbar: {
type: 'vertical',
},
});

column.render();
});
2 changes: 1 addition & 1 deletion examples/bar/grouped/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"zh": "分组条形图",
"en": "Grouped Bar chart"
},
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*s-oXTqRDBegAAAAAAAAAAAAAARQnAQ"
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*Yda9RZwgU1AAAAAAAAAAAAAAARQnAQ"
}
]
}
2 changes: 1 addition & 1 deletion examples/bar/percent/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"zh": "百分百条形图",
"en": "Percent Bar chart"
},
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*E7YZS5gGHJsAAAAAAAAAAAAAARQnAQ"
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*l8JuSrkrOIkAAAAAAAAAAAAAARQnAQ"
}
]
}
2 changes: 1 addition & 1 deletion examples/bar/range/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"zh": "区间条形图",
"en": "Range Bar chart"
},
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*tCDCQaO1ZZsAAAAAAAAAAAAAARQnAQ"
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*1is6RbXnUI4AAAAAAAAAAAAAARQnAQ"
}
]
}
2 changes: 1 addition & 1 deletion examples/bar/stacked/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"zh": "堆叠条形图",
"en": "StackedBar chart"
},
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*N8smQYy45fAAAAAAAAAAAABkARQnAQ"
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*BHyQS6T_qucAAAAAAAAAAAAAARQnAQ"
}
]
}
5 changes: 4 additions & 1 deletion src/plots/bar/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {} from '@antv/util';
import { Params } from '../../core/adaptor';
import { adaptor as columnAdaptor } from '../column/adaptor';
import { BarOptions } from './types';
Expand All @@ -8,7 +9,7 @@ import { BarOptions } from './types';
*/
export function adaptor(params: Params<BarOptions>) {
const { chart, options } = params;
const { xField, yField, xAxis, yAxis, barStyle, barWidthRatio, label } = options;
const { xField, yField, xAxis, yAxis, barStyle, barWidthRatio, label, data } = options;

// label of bar charts default position is left, if plot has label
if (label && !label.position) {
Expand All @@ -32,6 +33,8 @@ export function adaptor(params: Params<BarOptions>) {
// rename attrs as column
columnStyle: barStyle,
columnWidthRatio: barWidthRatio,
// bar 调整数据顺序
data: data ? data.slice().reverse() : data,
},
},
true
Expand Down

0 comments on commit 5e13404

Please sign in to comment.