Skip to content

Commit

Permalink
feat(legend): 连续型图例支持 label 进行 formatter & 单测 & 文档 (#225)
Browse files Browse the repository at this point in the history
* feat(legend): 连续型图例支持 label 进行 formatter & 单测 & 文档

* chore: 锁定 G 版本在 0.5.6, 待 0.5.7 的 text shape 问题修复后升级

* chore: v0.8.11
  • Loading branch information
visiky authored Apr 6, 2021
1 parent 912fb58 commit 4f7e78b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@ interface ContinueLegendLabelCfg {
* @type {string}
*/
align: string;
/**
* 文本格式化
* @type {string}
*/
formatter?: (text: string | number | null) => string;
/**
* 文本同滑轨的距离
* @type {number}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/component",
"version": "0.8.10",
"version": "0.8.11",
"description": "The component module for antv",
"author": "https://github.com/orgs/antvis/people",
"license": "MIT",
Expand Down Expand Up @@ -42,7 +42,7 @@
},
"devDependencies": {
"@antv/color-util": "^2.0.3",
"@antv/g-canvas": "^0.5.0",
"@antv/g-canvas": "0.5.6",
"@types/jest": "^24.0.18",
"husky": "^3.0.4",
"jest": "^24.9.0",
Expand All @@ -58,7 +58,7 @@
},
"dependencies": {
"@antv/dom-util": "~2.0.1",
"@antv/g-base": "~0.5.0",
"@antv/g-base": "0.5.6",
"@antv/matrix-util": "^3.1.0-beta.1",
"@antv/path-util": "~2.0.7",
"@antv/scale": "~0.3.1",
Expand Down
5 changes: 3 additions & 2 deletions src/legend/continuous.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IElement, IGroup } from '@antv/g-base';
import { clone, mix, upperFirst } from '@antv/util';
import { clone, isFunction, mix, upperFirst } from '@antv/util';
import { ISlider } from '../interfaces';
import { BBox, ContinueLegendCfg } from '../types';
import Theme from '../util/theme';
Expand Down Expand Up @@ -205,6 +205,7 @@ class ContinueLegend extends LegendBase<ContinueLegendCfg> implements ISlider {
const labelCfg = this.get('label');
const style = labelCfg.style;
const labelAlign = labelCfg.align;
const labelFormatter = labelCfg.formatter;
const value = this.get(name);
const alignAttrs = this.getLabelAlignAttrs(name, labelAlign);
const localId = `label-${name}`;
Expand All @@ -215,7 +216,7 @@ class ContinueLegend extends LegendBase<ContinueLegendCfg> implements ISlider {
attrs: {
x: 0,
y: 0,
text: value,
text: isFunction(labelFormatter) ? labelFormatter(value) : value,
...style,
...alignAttrs,
},
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,11 @@ export interface ContinueLegendLabelCfg {
* @type {string}
*/
align?: string;
/**
* 文本格式化
* @type {string}
*/
formatter?: (text: string | number | null) => string;
/**
* 文本同滑轨的距离
* @type {number}
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/legend/continuous-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,24 @@ describe('test continue legend', () => {
);
});

it('add label formatter', () => {
let minLabel = legend.getElementById('b-legend-label-min');
let maxLabel = legend.getElementById('b-legend-label-max');
expect(minLabel.attr('text')).toBe(100);
expect(maxLabel.attr('text')).toBe(500);

legend.update({
label: {
formatter: (text) => `${text}%`,
},
});
legend.render();
minLabel = legend.getElementById('b-legend-label-min');
maxLabel = legend.getElementById('b-legend-label-max');
expect(minLabel.attr('text')).toBe('100%');
expect(maxLabel.attr('text')).toBe('500%');
});

it('change value', () => {
legend.update({
min: 100,
Expand Down

0 comments on commit 4f7e78b

Please sign in to comment.