-
Notifications
You must be signed in to change notification settings - Fork 37
/
SequentialScale.ts
49 lines (42 loc) · 1.55 KB
/
SequentialScale.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*!
* Copyright (c) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project.
*/
import { ScaleCreationContext, Scales, Scale } from '@chart-parts/interfaces'
import { scaleSequential } from 'd3-scale'
import { DomainScale } from '../DomainScale'
import { QuantitativeValue, QuantitativeSpan } from './QuantitativeScale'
export class SequantialScale extends DomainScale<QuantitativeSpan> {
private clampValue?: boolean
private interpolatorValue?: (t: QuantitativeValue) => any
public interpolator(value?: (t: QuantitativeValue) => any): this {
this.interpolatorValue = value
return this
}
/**
* A boolean indicating if output values should be clamped to the range (default false).
* If clamping is disabled and the scale is passed a value outside the domain, the scale
* may return a value outside the range through extrapolation. If clamping is enabled,
* the output value of the scale is always within the scale’s range.
*/
public clamp(value?: boolean): this {
this.clampValue = value
return this
}
protected createScale(args: ScaleCreationContext): Scales {
if (!this.interpolatorValue) {
throw new Error('interpolator must be set')
}
const domain = this.getDomain(args)
const result = scaleSequential(this.interpolatorValue).domain(domain)
if (this.clampValue !== undefined) {
result.clamp(this.clampValue)
}
if (!this.nameValue) {
throw new Error('scale name must be set')
}
return {
[this.nameValue]: result as Scale<QuantitativeValue, any>,
} as any as Scales
}
}