Skip to content

Commit

Permalink
feat(gauge): 丰富的仪表盘:米轨仪表盘
Browse files Browse the repository at this point in the history
  • Loading branch information
visiky committed Jan 26, 2021
1 parent 510c1b5 commit c159e14
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 6 deletions.
19 changes: 19 additions & 0 deletions docs/api/plots/gauge.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ Dashboard auxiliary arc style.

<playground rid="gauge" path="progress-plots/gauge/demo/basic.ts"></playground>

#### type ✨

<description>**optional** _string_ _default_: `undefined`</description>

Display type of gauge. options: `meter`, default: `undefined`

#### meter ✨

<description>**optional** _object_</description>

It works when `type = 'meter'`. Properties are as followed:

| Properties | Type | Description | Default |
| ------ | -------- | --------------------------------- | -------- |
| steps | _number_ | The total step count | 50 |
| stepRatio | _number_ | 0 ~ 1. Represent the ratio between `step` and `gap`. `gap` is zero when `stepRatio` is setting to `1` | 0.5 |

<img src="https://gw.alipayobjects.com/zos/antfincdn/WBhwhNUzkg/image.png" width="400" align="center" style="display:flex;margin:0 auto;">

#### axis

<description>**optional** _object_</description>
Expand Down
18 changes: 18 additions & 0 deletions docs/api/plots/gauge.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ order: 22

<playground rid="gauge" path="progress-plots/gauge/demo/custom-color.ts"></playground>

#### type ✨

<description>**optional** _string_ _default_: `undefined`</description>

仪表盘的展示类型。可选项为:`meter`,默认为空
#### meter ✨

<description>**optional** _object_</description>

`type = 'meter'` 时生效,具体配置属性如下。

| 配置项 | 类型 | 描述 | 默认值 |
| ------ | -------- | ----------------- | ---------|
| steps | _number_ | 总步数 | 50 |
| stepRatio | _number_ | [0, 1] 范围。代表着 step 和 gap 的比例关系,当 `stepRatio` 为 1 时,gap 为 0 | 0.5,即默认 step 等于 gap 宽度 |

<img src="https://gw.alipayobjects.com/zos/antfincdn/WBhwhNUzkg/image.png" width="400" align="center" style="display:flex;margin:0 auto;">

### 图表组件

#### axis
Expand Down
7 changes: 7 additions & 0 deletions docs/manual/plots/gauge.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,10 @@ Example:
```

<playground path="progress-plots/gauge/demo/custom-color.ts" rid="rect2"></playground>

### 设置仪表盘展示类型

通过设置 `type: 'meter'`, 可以实现一个展示形态为**米轨**的仪表盘。
同时还支持对 `steps` 以及 `stepRatio` 的设置,其中 `stepRatio` 代表着 step 和 gap 的比例关系,默认为:0.5,即默认 step 等于 gap 宽度,当 `stepRatio` 为 1 时,gap 为 0。

<playground path="progress-plots/gauge/demo/custom-meter-step.ts" rid="rect3"></playground>
7 changes: 7 additions & 0 deletions docs/manual/plots/gauge.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,10 @@ Example:
```

<playground path="progress-plots/gauge/demo/custom-color.ts" rid="rect2"></playground>

### 设置仪表盘展示类型

通过设置 `type: 'meter'`, 可以实现一个展示形态为**米轨**的仪表盘。
同时还支持对 `steps` 以及 `stepRatio` 的设置,其中 `stepRatio` 代表着 step 和 gap 的比例关系,默认为:0.5,即默认 step 等于 gap 宽度,当 `stepRatio` 为 1 时,gap 为 0。

<playground path="progress-plots/gauge/demo/custom-meter-step.ts" rid="rect3"></playground>
1 change: 1 addition & 0 deletions examples/progress-plots/gauge/demo/complex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Gauge } from '@antv/g2plot';

const ticks = [0, 1 / 3, 2 / 3, 1];
const color = ['#F4664A', '#FAAD14', '#30BF78'];

const gauge = new Gauge('container', {
percent: 0,
range: {
Expand Down
68 changes: 68 additions & 0 deletions examples/progress-plots/gauge/demo/custom-meter-step.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Gauge } from '@antv/g2plot';

const ticks = [0, 1 / 3, 2 / 3, 1];
const color = ['#F4664A', '#FAAD14', '#30BF78'];
const gauge = new Gauge('container', {
percent: 0,
innerRadius: 0.75,
type: 'meter',
// 自定义 meter 总步数 以及 step 占比
meter: {
steps: 50,
stepRatio: 0.6,
},
range: {
ticks: [0, 1],
color: ['l(0) 0:#F4664A 0.5:#FAAD14 1:#30BF78'],
},
indicator: {
pointer: {
style: {
stroke: '#D0D0D0',
},
},
pin: {
style: {
stroke: '#D0D0D0',
},
},
},
statistic: {
title: {
formatter: ({ percent }) => {
if (percent < ticks[1]) {
return '差';
}
if (percent < ticks[2]) {
return '中';
}
return '优';
},
style: ({ percent }) => {
return {
fontSize: '36px',
lineHeight: 1,
color: percent < ticks[1] ? color[0] : percent < ticks[2] ? color[1] : color[2],
};
},
},
content: {
offsetY: 36,
style: {
fontSize: '24px',
color: '#4B535E',
},
formatter: () => '系统表现',
},
},
});

gauge.render();
let data = 0.7;
const interval = setInterval(() => {
if (data >= 1.5) {
clearInterval(interval);
}
data += 0.005;
gauge.changeData(data > 1 ? data - 1 : data);
}, 100);
22 changes: 19 additions & 3 deletions examples/progress-plots/gauge/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,31 @@
"zh": "仪表盘(多色)",
"en": "Gauge plot with custom color"
},
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*2pQHSJvYqwwAAAAAAAAAAAAAARQnAQ"
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/Uev7Dpt5C5/4a829ac9-2fb3-4ac8-8a5d-f17ad4483518.png"
},
{
"filename": "complex.ts",
"title": {
"zh": "自定义配置的仪表盘",
"en": "Animated gauge plot"
"zh": "仪表盘(渐变色)",
"en": "Multiple gradient gauge plot"
},
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*LVeFSYBPDBAAAAAAAAAAAAAAARQnAQ"
},
{
"filename": "meter-gauge.ts",
"title": {
"zh": "米轨仪表盘",
"en": "Meter gauge plot"
},
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/hJeVJVaTpf/6aea13fd-6a5a-4a2b-b612-5c3ceb8a280b.png"
},
{
"filename": "custom-meter-step.ts",
"title": {
"zh": "米轨仪表盘(自定义步数和大小)",
"en": "Meter gauge plot (custom steps and stepRatio)"
},
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/wqWYdQwQlc/demo1.gif"
}
]
}
33 changes: 33 additions & 0 deletions examples/progress-plots/gauge/demo/meter-gauge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Gauge } from '@antv/g2plot';

const gauge = new Gauge('container', {
percent: 0.75,
type: 'meter',
innerRadius: 0.75,
range: {
ticks: [0, 1 / 3, 2 / 3, 1],
color: ['#F4664A', '#FAAD14', '#30BF78'],
},
indicator: {
pointer: {
style: {
stroke: '#D0D0D0',
},
},
pin: {
style: {
stroke: '#D0D0D0',
},
},
},
statistic: {
content: {
style: {
fontSize: '36px',
lineHeight: '36px',
},
},
},
});

gauge.render();
44 changes: 43 additions & 1 deletion src/plots/gauge/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import { AXIS_META_CONFIG_KEYS } from '../../constant';
import { Params } from '../../core/adaptor';
import { Data } from '../../types';
import { deepAssign, flow, pick, renderGaugeStatistic } from '../../utils';
import { RANGE_TYPE, RANGE_VALUE, PERCENT, DEFAULT_COLOR, INDICATEOR_VIEW_ID, RANGE_VIEW_ID } from './constant';
import {
RANGE_TYPE,
RANGE_VALUE,
PERCENT,
DEFAULT_COLOR,
INDICATEOR_VIEW_ID,
RANGE_VIEW_ID,
MASK_VIEW_ID,
} from './constant';
import { GaugeOptions } from './types';
import { getIndicatorData, getRangeData } from './utils';

Expand Down Expand Up @@ -65,6 +73,38 @@ function geometry(params: Params<GaugeOptions>): Params<GaugeOptions> {
return params;
}

/**
* meter 类型的仪表盘 有一层 mask
* @param params
*/
function meterView(params: Params<GaugeOptions>): Params<GaugeOptions> {
const { chart, options } = params;

const { type, meter } = options;
if (type === 'meter') {
const { innerRadius, radius, startAngle, endAngle } = options;

const { background } = chart.getTheme();

let color = background;
if (!color || color === 'transparent') {
color = '#fff';
}

const v3 = chart.createView({ id: MASK_VIEW_ID });
v3.data([{ [RANGE_TYPE]: '1', [RANGE_VALUE]: 1 }]);
v3.interval()
.position(`1*${RANGE_VALUE}`)
.color(color)
.adjust('stack')
.shape('meter-gauge')
.customInfo(meter || {});
v3.coordinate('polar', { innerRadius, radius, startAngle, endAngle }).transpose();
}

return params;
}

/**
* meta 配置
* @param params
Expand Down Expand Up @@ -156,6 +196,8 @@ export function adaptor(params: Params<GaugeOptions>) {
statistic,
interaction,
theme,
// meterView 需要放到主题之后
meterView,
other
// ... 其他的 adaptor flow
)(params);
Expand Down
3 changes: 3 additions & 0 deletions src/plots/gauge/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ export const DEFAULT_COLOR = '#f0f0f0';
/** 仪表盘由 指针和表盘 组成 */
export const INDICATEOR_VIEW_ID = 'indicator-view';
export const RANGE_VIEW_ID = 'range-view';

/** meter 类型的仪表盘 带 mask 的 view */
export const MASK_VIEW_ID = 'range-mask-view';
1 change: 1 addition & 0 deletions src/plots/gauge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { RANGE_VALUE, PERCENT, INDICATEOR_VIEW_ID, RANGE_VIEW_ID } from './const
import { getIndicatorData, getRangeData } from './utils';
// 注册 shape
import './shapes/gauge';
import './shapes/meter-gauge';

export { GaugeOptions };

Expand Down
3 changes: 1 addition & 2 deletions src/plots/gauge/shapes/gauge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { registerShape } from '@antv/g2';
import { Types } from '@antv/g2';
import { registerShape, Types } from '@antv/g2';
import { Indicator } from '../types';

// 自定义Shape 部分
Expand Down
60 changes: 60 additions & 0 deletions src/plots/gauge/shapes/meter-gauge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { registerShape, Types, Util } from '@antv/g2';
import { getSectorPath } from '@antv/g2/lib/util/graphics';

// 自定义Shape 部分
registerShape('interval', 'meter-gauge', {
draw(cfg: Types.ShapeInfo, container) {
// 使用 customInfo 传递参数
const { steps: STEP = 50, stepRatio = 0.5 } = cfg.customInfo;

const total = this.coordinate.endAngle - this.coordinate.startAngle;
let interval = total / STEP;
let gap = 0;

/**
* 1: interval : gap = stepRatio : (1 - stepRatio)
* 2: interval * STEP + stepRatio * (STEP - 1) = total
*/
if (stepRatio >= 0 && stepRatio < 1) {
interval = total / (((1 - stepRatio) / stepRatio) * (STEP - 1) + STEP);
gap = (interval * (1 - stepRatio)) / stepRatio;
}

const group = container.addGroup();
// 绘制 gap
if (gap > 0) {
const center = this.coordinate.getCenter();
const radius = this.coordinate.getRadius();
const { startAngle, endAngle } = Util.getAngle(cfg, this.coordinate);
for (let i = startAngle, j = 0; i < endAngle; j++) {
if (j % 2) {
const path = getSectorPath(
center.x,
center.y,
radius,
i,
Math.min(i + gap, endAngle),
radius * this.coordinate.innerRadius
);
group.addShape('path', {
name: 'meter-gauge-mask',
attrs: {
path,
fill: cfg.color,
stroke: cfg.color,
lineWidth: 0.5,
},
// mask 不需要捕捉事件
capture: false,
});

i += gap;
} else {
i += interval;
}
}
}

return group;
},
});
11 changes: 11 additions & 0 deletions src/plots/gauge/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,15 @@ export interface GaugeOptions
readonly indicator?: false | Indicator;
/** 统计文本 */
readonly statistic?: Statistic;

// meter gauge 相关配置
/** 仪表盘类型, 可选项: 'meter', default 为空 */
readonly type?: string;
/** 当仪表盘类型 = 'meter' 生效 */
readonly meter?: {
/** 仪表盘总步数, default: 50 */
readonly steps?: number;
/** step 与 gap 的宽度占比, default: 0.5 */
readonly stepRatio?: number;
};
}

0 comments on commit c159e14

Please sign in to comment.