Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gauge): 仪表盘支持配置 style,支持圆角仪表盘 #2395

Merged
merged 3 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions __tests__/unit/plots/gauge/style-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Gauge } from '../../../../src';
import { createDiv } from '../../../utils/dom';

describe('gauge', () => {
const gauge = new Gauge(createDiv(), {
width: 600,
height: 300,
autoFit: false,
percent: 0.75,
range: {
ticks: [0, 0.2, 0.4, 0.75, 1],
color: ['red', 'yellow', 'green'],
},
indicator: {},
gaugeStyle: {
lineCap: 'round',
lineWidth: 2,
stroke: 'red',
},
});

gauge.render();

it('gaugeStyle', async () => {
const [, v2] = gauge.chart.views;
expect(v2.geometries[0].type).toBe('interval');
// v2 gaugeStyle 生效
expect(v2.geometries[0].elements[0].shape.attr('lineCap')).toBe('round');
expect(v2.geometries[0].elements[0].shape.attr('lineWidth')).toBe(2);
expect(v2.geometries[0].elements[0].shape.attr('stroke')).toBe('red');
});

it('gaugeStyle, with callback', () => {
gauge.update({
percent: 0.9,
gaugeStyle: ({ percent }) => {
return {
lineCap: 'round',
lineWidth: 2,
stroke: percent > 0.8 ? 'green' : 'red',
};
},
});

const [, v2] = gauge.chart.views;
expect(v2.geometries[0].type).toBe('interval');
// v2 gaugeStyle 生效
expect(v2.geometries[0].elements[0].shape.attr('lineCap')).toBe('round');
expect(v2.geometries[0].elements[0].shape.attr('lineWidth')).toBe(2);
expect(v2.geometries[0].elements[0].shape.attr('stroke')).toBe('green');
});

afterAll(() => {
gauge.destroy();
});
});
10 changes: 10 additions & 0 deletions docs/api/plots/gauge.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ It works when `type = 'meter'`. Properties are as followed:

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

#### gaugeStyle

<description>**optional** _StyleAttr | Function_</description>

Gauge graphic style.

`markdown:docs/common/shape-style.en.md`

### Plot Components

#### axis

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

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

#### gaugeStyle

<description>**optional** _StyleAttr | Function_</description>

仪表盘的样式设置。

`markdown:docs/common/shape-style.zh.md`

### 图表组件

#### axis
Expand Down
36 changes: 24 additions & 12 deletions src/plots/gauge/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isString } from '@antv/util';
import { interaction, animation, theme, scale, annotation } from '../../adaptor/common';
import { interval } from '../../adaptor/geometries';
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,
Expand All @@ -13,7 +13,7 @@ import {
RANGE_VIEW_ID,
MASK_VIEW_ID,
} from './constants';
import { GaugeOptions } from './types';
import { GaugeCustomInfo, GaugeOptions } from './types';
import { getIndicatorData, getRangeData } from './utils';

/**
Expand All @@ -22,7 +22,7 @@ import { getIndicatorData, getRangeData } from './utils';
*/
function geometry(params: Params<GaugeOptions>): Params<GaugeOptions> {
const { chart, options } = params;
const { percent, range, radius, innerRadius, startAngle, endAngle, axis, indicator } = options;
const { percent, range, radius, innerRadius, startAngle, endAngle, axis, indicator, gaugeStyle } = options;
const { color } = range;

// 指标 & 指针
Expand Down Expand Up @@ -54,14 +54,30 @@ function geometry(params: Params<GaugeOptions>): Params<GaugeOptions> {
}

// 辅助 range
// [{ range: 1, type: '0' }]
const rangeData: Data = getRangeData(percent, options.range);
// [{ range: 1, type: '0', percent: 原始进度百分比 }]
const rangeData = getRangeData(percent, options.range);
const v2 = chart.createView({ id: RANGE_VIEW_ID });
v2.data(rangeData);

const rangeColor = isString(color) ? [color, DEFAULT_COLOR] : color;

v2.interval().position(`1*${RANGE_VALUE}`).color(RANGE_TYPE, rangeColor).adjust('stack');
interval({
chart: v2,
options: {
xField: '1',
yField: RANGE_VALUE,
seriesField: RANGE_TYPE,
rawFields: [PERCENT],
isStack: true,
interval: {
color: rangeColor,
style: gaugeStyle,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

替换为通用的 interval geometry 的 adaptor,且支持 gaugeStyle

},
args: {
zIndexReversed: true,
},
},
});

v2.coordinate('polar', {
innerRadius,
Expand Down Expand Up @@ -93,12 +109,8 @@ function meterView(params: Params<GaugeOptions>): Params<GaugeOptions> {

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 || {});
const customInfo: GaugeCustomInfo = { meter };
v3.interval().position(`1*${RANGE_VALUE}`).color(color).adjust('stack').shape('meter-gauge').customInfo(customInfo);
v3.coordinate('polar', { innerRadius, radius, startAngle, endAngle }).transpose();
}

Expand Down
13 changes: 9 additions & 4 deletions src/plots/gauge/shapes/meter-gauge.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { registerShape, Types, Util } from '@antv/g2';
import { getSectorPath } from '@antv/g2/lib/util/graphics';
import { GaugeCustomInfo } from '../types';

type ShapeCfg = Omit<Types.ShapeInfo, 'customInfo'> & {
customInfo: GaugeCustomInfo;
};

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

const total = this.coordinate.endAngle - this.coordinate.startAngle;
let interval = total / STEP;
Expand All @@ -30,7 +35,7 @@ registerShape('interval', 'meter-gauge', {
for (let i = startAngle, j = 0; i < endAngle && j < 2 * STEP - 1; j++) {
const drawn = j % 2;
if (drawn) {
const path = getSectorPath(
const path = Util.getSectorPath(
center.x,
center.y,
radius,
Expand Down
22 changes: 21 additions & 1 deletion src/plots/gauge/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Options, ShapeStyle, Statistic } from '../../types';
import { Options, ShapeStyle, Statistic, StyleAttr } from '../../types';
import { Axis } from '../../types/axis';
import { PERCENT, RANGE_TYPE, RANGE_VALUE } from './constants';

/** 指标指标的配置 */
export type Indicator = {
Expand All @@ -20,6 +21,15 @@ type Range = {
readonly color?: string | string[];
};

/**
* 仪表盘辅助生成的 rangeData
*/
export type GaugeRangeData = {
readonly [RANGE_VALUE]?: number;
readonly [RANGE_TYPE]: string;
readonly [PERCENT]: number;
}[];

/** 仪表盘配置类型定义 */
export interface GaugeOptions
extends Omit<Options, 'data' | 'tooltip' | 'legend' | 'xAxis' | 'yAxis' | 'xField' | 'yField' | 'color'> {
Expand All @@ -41,6 +51,8 @@ export interface GaugeOptions
readonly indicator?: false | Indicator;
/** 统计文本 */
readonly statistic?: Statistic;
/** 仪表盘样式 */
readonly gaugeStyle?: StyleAttr;

// meter gauge 相关配置
/** 仪表盘类型, 可选项: 'meter', default 为空 */
Expand All @@ -53,3 +65,11 @@ export interface GaugeOptions
readonly stepRatio?: number;
};
}

/**
* 仪表盘 自定义 shape 使用的 customInfo
*/
export type GaugeCustomInfo = {
/** 仪表盘 meter 类型的相关配置 */
readonly meter?: GaugeOptions['meter'];
};
11 changes: 6 additions & 5 deletions src/plots/gauge/utils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { clamp, get, size } from '@antv/util';
import { Data, Datum } from '../../types';
import { RANGE_VALUE, RANGE_TYPE, PERCENT } from './constants';
import { GaugeOptions } from './types';
import { GaugeOptions, GaugeRangeData } from './types';

/**
* 将 range 生成为 data 数据
* @param range
* @param key
* @returns {GaugeRangeData}
*/
export function processRangeData(range: number[]): Data {
export function processRangeData(range: number[], percent: GaugeOptions['percent']): GaugeRangeData {
return (
range
// 映射为 stack 的数据
.map((r: number, idx: number) => {
return { [RANGE_VALUE]: r - (range[idx - 1] || 0), [RANGE_TYPE]: `${idx}` };
return { [RANGE_VALUE]: r - (range[idx - 1] || 0), [RANGE_TYPE]: `${idx}`, [PERCENT]: percent };
})
// 去掉 0 的数据
.filter((d: Datum) => !!d[RANGE_VALUE])
Expand All @@ -33,9 +34,9 @@ export function getIndicatorData(percent: GaugeOptions['percent']): Data {
* @param percent
* @param range
*/
export function getRangeData(percent: GaugeOptions['percent'], range?: GaugeOptions['range']): Data {
export function getRangeData(percent: GaugeOptions['percent'], range?: GaugeOptions['range']): GaugeRangeData {
const ticks = get(range, ['ticks'], []);

const clampTicks = size(ticks) ? ticks : [0, clamp(percent, 0, 1), 1];
return processRangeData(clampTicks as number[]);
return processRangeData(clampTicks as number[], percent);
}