Skip to content

Commit f465ef7

Browse files
authored
Merge pull request #3750 from VisActor/feat/linear-scale-clamp
feat: support `clamp` in linear scales
2 parents 79f1e89 + 1c23deb commit f465ef7

File tree

7 files changed

+74
-1
lines changed

7 files changed

+74
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@visactor/vchart",
5+
"comment": "'feat: support `clamp` in linear scales, #3738'",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@visactor/vchart"
10+
}

docs/assets/option/en/common/visual-scale-spec.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,26 @@ const scale = {
189189
// scale('b') => 'blue'
190190
// scale('d') => 'red'
191191
```
192+
193+
#${prefix} clamp(boolean)
194+
195+
If clamp is enabled, the return value of the scale is always within the scale’s range.
196+
197+
`1.13.6` version supported, only supported in `type=linear`.
198+
199+
Example:
200+
201+
```ts
202+
const scale = {
203+
type: 'linear',
204+
domain: [100, 200],
205+
range: [0, 50],
206+
clamp: true
207+
};
208+
//
209+
// scale(100) => 0
210+
// scale(150) => 25
211+
// scale(200) => 50
212+
// scale(0) => 0
213+
// scale(300) => 50
214+
```

docs/assets/option/zh/common/visual-scale-spec.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,26 @@ const scale = {
189189
// scale('b') => 'blue'
190190
// scale('d') => 'red'
191191
```
192+
193+
#${prefix} clamp(boolean)=false
194+
195+
如果 clamp 为 true,则 scale 的返回值总是处于 scale 的 range 范围内。
196+
197+
`1.13.6`版本开始支持,仅在 `type=linear` 时生效。
198+
199+
示例:
200+
201+
```ts
202+
const scale = {
203+
type: 'linear',
204+
domain: [100, 200],
205+
range: [0, 50],
206+
clamp: true
207+
};
208+
//
209+
// scale(100) => 0
210+
// scale(150) => 25
211+
// scale(200) => 50
212+
// scale(0) => 0
213+
// scale(300) => 50
214+
```

packages/vchart/src/chart/base/base-chart.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ export class BaseChart<T extends IChartSpec> extends CompilableBase implements I
709709
Object.prototype.hasOwnProperty.call(tempSpec, 'range') && (colorScaleSpec.range = tempSpec.range);
710710
Object.prototype.hasOwnProperty.call(tempSpec, 'specified') &&
711711
(colorScaleSpec.specified = tempSpec.specified);
712+
Object.prototype.hasOwnProperty.call(tempSpec, 'clamp') && (colorScaleSpec.clamp = tempSpec.clamp);
712713
}
713714
}
714715
}

packages/vchart/src/scale/global-scale.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { IGlobalScale } from './interface';
22
import { isArray, isEmpty, isEqual, isNil } from '@visactor/vutils';
3-
import type { IBaseScale, OrdinalScale } from '@visactor/vscale';
3+
import type { IBaseScale, LinearScale, OrdinalScale } from '@visactor/vscale';
44
import { isContinuous } from '@visactor/vscale';
55
import type { IChart } from '../chart/interface';
66
import type { IChartSpec } from '../typings/spec';
@@ -51,6 +51,11 @@ export class GlobalScale implements IGlobalScale {
5151
if (s.specified && (<OrdinalScale>scale).specified) {
5252
(<OrdinalScale>scale).specified(s.specified);
5353
}
54+
55+
if (s.clamp && (<LinearScale>scale).clamp) {
56+
(<LinearScale>scale).clamp(s.clamp);
57+
}
58+
5459
return scale;
5560
}
5661

packages/vchart/src/typings/visual.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ export interface IVisualSpecBase<D, T> {
3838
* @since 1.1.0
3939
*/
4040
specified?: { [key: string]: unknown };
41+
/**
42+
* enable clamp in linear scale
43+
* If clamp is enabled, the return value of the scale is always within the scale’s range.
44+
* @since 1.13.6
45+
* @default false
46+
*/
47+
clamp?: boolean;
4148
}
4249
// 用来给用户进行mark.style上的映射配置。所以要配置数据维度
4350
export interface IVisualSpecStyle<D, T> extends IVisualSpecBase<D, T> {

packages/vchart/src/util/scale.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ function initScaleWithSpec(scale: IBaseScale, spec: IVisualSpecBase<any, any>) {
6464
if (spec.specified && (<OrdinalScale>scale).specified) {
6565
(<OrdinalScale>scale).specified(spec.specified);
6666
}
67+
68+
if (spec.clamp && (<LinearScale>scale).clamp) {
69+
(<LinearScale>scale).clamp(spec.clamp);
70+
}
6771
}
6872

6973
/**

0 commit comments

Comments
 (0)