Skip to content

Commit 581a2ac

Browse files
tiger5wangwangyuhu
andauthored
feat(charts): 条形图扩展
* test: 多级矩形树图 * feat: 条形图右侧数值间距问题 * test: render添加到demo中 * test: 条形图demo * feat: 条形图扩展 * feat: 条形图类型&导出 --------- Co-authored-by: wangyuhu <wangyuhu@sensoro.com>
1 parent c505098 commit 581a2ac

File tree

11 files changed

+709
-10
lines changed

11 files changed

+709
-10
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { Bar as BaseBar } from '@sensoro-design/plots';
2+
import type { BarConfig as BaseBarConfig } from '@sensoro-design/plots';
3+
import { deepMix } from '@antv/util';
4+
5+
export interface BarConfig extends BaseBarConfig {
6+
// 自定义属性
7+
aloneLabel?: boolean; // label是否单独一行
8+
}
9+
10+
const ALONE_TITLE_AXIS = {
11+
x: {
12+
size: 0,
13+
label: false,
14+
},
15+
};
16+
17+
const ALONE_TITLE_LABEL = {
18+
text: 'name',
19+
position: 'left',
20+
transform: [{ type: 'overlapDodgeY' }],
21+
dy: -18,
22+
lineHeight: 20,
23+
};
24+
25+
export function Bar(props: BarConfig) {
26+
const { aloneLabel, data, axis, yField, label, width, height, paddingRight, markBackground, ...rest } = props;
27+
const valueLength = data?.reduce(
28+
(len: number, next: BaseBarConfig['data']) => Math.max(next[yField as string], len),
29+
0,
30+
).toString().length;
31+
32+
const axisConfig = aloneLabel ? deepMix({}, ALONE_TITLE_AXIS, axis) : axis;
33+
34+
const labelConfig = aloneLabel
35+
? deepMix({}, ALONE_TITLE_LABEL, label)
36+
: label;
37+
38+
const heightConfig
39+
= height || data.length * 8 + (data.length - 1) * (aloneLabel ? 36 : 24) + 92 + 48;
40+
41+
const paddingRightConfig = paddingRight || valueLength * 7;
42+
43+
const markBackgroundConfig = deepMix(
44+
{},
45+
{ label: { dx: width! - 48 - 16 - (aloneLabel ? 0 : 110) } },
46+
markBackground,
47+
);
48+
49+
return (
50+
<BaseBar
51+
width={width}
52+
height={heightConfig}
53+
yField={yField}
54+
axis={axisConfig}
55+
label={labelConfig}
56+
paddingRight={paddingRightConfig}
57+
markBackground={markBackgroundConfig}
58+
data={data}
59+
{...rest}
60+
/>
61+
);
62+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Bar } from './Bar';
2+
3+
export type { BarConfig } from './Bar';
4+
export default Bar;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "./token.less";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './index.less';

@sensoro-design/charts/src/bar/style/token.less

Whitespace-only changes.

@sensoro-design/charts/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ export type { LineConfig } from './line';
66

77
export { default as Area } from './area';
88
export type { AreaConfig } from './area';
9+
10+
export { default as Bar } from './bar';
11+
export type { BarConfig } from './bar';

@sensoro-design/plots/src/components/Bar/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export const Bar = forwardRef<Chart, BarConfig>((props, ref) => {
2020
scale,
2121
interaction,
2222
markBackground,
23-
paddingRight = 44,
23+
paddingRight = 0, // TODO 需要再charts中抹平 paddingRight 和数据值的位数关系:最大值的长度 * 3
24+
// @ts-expect-error 暂时忽略
2425
...rest
2526
} = props;
2627

@sensoro-design/plots/src/components/Bar/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ export const getDefaultMarkBackgroundConfig: (
88
text: ({ originData }: { originData: any }) => {
99
return `${originData[yField]}`;
1010
},
11-
position: 'right',
12-
dx: 44,
11+
position: 'left',
12+
// TODO 需要再charts中抹平dx和数据值的位数关系:图表宽度 - 图表内部左右padding(默认24) - 图表左侧x轴label宽度(默认110 || 0) - 值距离条形图的距离(默认16)
13+
dx: 0,
1314
dy: 0,
1415
style: {
1516
fill: colorGrey10,

packages/storybook/stories/Charts/Bar.stories.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default meta;
1111
const data = [
1212
{
1313
name: '蓝领',
14-
value: 10,
14+
value: 8,
1515
},
1616
{
1717
name: '白领',
@@ -34,11 +34,16 @@ export function Basic() {
3434
data,
3535
xField: 'name',
3636
yField: 'value',
37+
// 需要传入宽度
38+
width: 480, // 480 - 48 - 110 - 16,
3739
scale: {
3840
y: {
39-
domain: [0, 800],
41+
domain: [0, 1200],
4042
},
4143
},
44+
// TODO 以下内容需要再 charts 中实现
45+
paddingRight: 21,
46+
markBackground: { label: { dx: 316 } },
4247
};
4348

4449
// @ts-expect-error 暂时忽略
@@ -48,15 +53,20 @@ export function Basic() {
4853
export function AloneTitle() {
4954
const config: BarConfig = {
5055
title: '独立标题行条形图',
51-
height: data.length * 8 + (data.length - 1) * 24 + 92 + 48,
56+
height: data.length * 8 + (data.length - 1) * 36 + 92 + 48,
5257
data,
5358
xField: 'name',
5459
yField: 'value',
60+
// 需要传入宽度
61+
width: 480,
5562
scale: {
5663
y: {
57-
domain: [0, 800],
64+
domain: [0, 1200],
5865
},
5966
},
67+
// TODO 以下内容需要再 charts 中实现
68+
paddingRight: 21,
69+
markBackground: { label: { dx: 427 } },
6070
axis: {
6171
x: {
6272
size: 0,
@@ -72,5 +82,6 @@ export function AloneTitle() {
7282
},
7383
};
7484

75-
return <Bar {...config} />;
85+
// @ts-expect-error 暂时忽略
86+
return <Bar {...config} renderer={new SVGRenderer()} />;
7687
}

packages/storybook/stories/Charts/Pie.stories.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export function Basic() {
3535
innerRadius: 0,
3636
};
3737

38-
return <Pie {...config} />;
38+
// @ts-expect-error 暂时忽略
39+
return <Pie {...config} renderer={new SVGRenderer()} />;
3940
}
4041

4142
export function Ring() {

0 commit comments

Comments
 (0)