Skip to content

Commit

Permalink
feat: support clamp in linear scales
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoluoHe committed Feb 20, 2025
1 parent 5cba48a commit 8febae6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@visactor/vchart",
"comment": "'feat: support `clamp` in linear scales, #3738'",
"type": "none"
}
],
"packageName": "@visactor/vchart"
}
1 change: 1 addition & 0 deletions packages/vchart/src/chart/base/base-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ export class BaseChart<T extends IChartSpec> extends CompilableBase implements I
Object.prototype.hasOwnProperty.call(tempSpec, 'range') && (colorScaleSpec.range = tempSpec.range);
Object.prototype.hasOwnProperty.call(tempSpec, 'specified') &&
(colorScaleSpec.specified = tempSpec.specified);
Object.prototype.hasOwnProperty.call(tempSpec, 'clamp') && (colorScaleSpec.clamp = tempSpec.clamp);
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion packages/vchart/src/scale/global-scale.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IGlobalScale } from './interface';
import { isArray, isEmpty, isEqual, isNil } from '@visactor/vutils';
import type { IBaseScale, OrdinalScale } from '@visactor/vscale';
import type { IBaseScale, LinearScale, OrdinalScale } from '@visactor/vscale';
import { isContinuous } from '@visactor/vscale';
import type { IChart } from '../chart/interface';
import type { IChartSpec } from '../typings/spec';
Expand Down Expand Up @@ -51,6 +51,11 @@ export class GlobalScale implements IGlobalScale {
if (s.specified && (<OrdinalScale>scale).specified) {
(<OrdinalScale>scale).specified(s.specified);
}

if (s.clamp && (<LinearScale>scale).clamp) {
(<LinearScale>scale).clamp(s.clamp);
}

return scale;
}

Expand Down
7 changes: 7 additions & 0 deletions packages/vchart/src/typings/visual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ export interface IVisualSpecBase<D, T> {
* @since 1.1.0
*/
specified?: { [key: string]: unknown };
/**
* enable clamp in linear scale
* If clamp is enabled, the return value of the scale is always within the scale’s range.
* @since 1.13.6
* @default false
*/
clamp?: boolean;
}
// 用来给用户进行mark.style上的映射配置。所以要配置数据维度
export interface IVisualSpecStyle<D, T> extends IVisualSpecBase<D, T> {
Expand Down
4 changes: 4 additions & 0 deletions packages/vchart/src/util/scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ function initScaleWithSpec(scale: IBaseScale, spec: IVisualSpecBase<any, any>) {
if (spec.specified && (<OrdinalScale>scale).specified) {
(<OrdinalScale>scale).specified(spec.specified);
}

if (spec.clamp && (<LinearScale>scale).clamp) {
(<LinearScale>scale).clamp(spec.clamp);
}
}

/**
Expand Down

0 comments on commit 8febae6

Please sign in to comment.