-
Notifications
You must be signed in to change notification settings - Fork 37
/
QuantitativeScale.ts
132 lines (116 loc) · 4 KB
/
QuantitativeScale.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*!
* Copyright (c) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project.
*/
import { ScaleCreationContext, Dimension } from '@chart-parts/interfaces'
import { extent } from 'd3-array'
import { interpolateRound } from 'd3-interpolate'
import { DomainRangeScale } from '../DomainRangeScale'
import { getBoundRange } from '../getBoundRange'
import { optionalArgument } from '../util'
export type TimeValue = QuantitativeValue | Date
export type QuantitativeValue = number | { valueOf(): number }
export type QuantitativeSpan = [QuantitativeValue, QuantitativeValue]
export abstract class QuantitativeScale<
DomainValue extends QuantitativeValue,
RangeValue extends QuantitativeValue,
> extends DomainRangeScale<
[DomainValue, DomainValue],
[RangeValue, RangeValue],
Dimension
> {
protected defaultZero = false
protected clampValue?: boolean
protected niceValue?: boolean | number | string | Record<string, any>
protected zeroValue?: boolean
protected paddingValue?: number
protected roundValue?: boolean
/**
* 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 = optionalArgument(value, arguments.length, true, false)
return this
}
/**
* Extends the domain so that it starts and ends on nice round values.
*/
public nice(value?: boolean | number | string | Record<string, any>): this {
this.niceValue = optionalArgument(value, arguments.length, true, false)
return this
}
/**
* Extends the domain so that it starts and ends on nice round values.
*/
public round(value?: boolean): this {
this.roundValue = optionalArgument(value, arguments.length, true, false)
return this
}
/**
* Adjusts the range of the scale to account for a pixel-padding on each end.
* @param paddingValue The number of pixels to pad each end of the scale's range with.
*/
public padding(paddingValue?: number): this {
this.paddingValue = paddingValue
return this
}
/**
* Boolean flag indicating if the scale domain should include zero.
* The default value is true for linear, sqrt and pow, and false otherwise.
*/
public zero(value?: boolean): this {
this.zeroValue = optionalArgument(value, arguments.length, true, false)
return this
}
protected getRange(args: ScaleCreationContext): [RangeValue, RangeValue] {
const range = super.getRange(args)
if (this.paddingValue !== undefined) {
const [start, end]: any = range
const pad = this.paddingValue
const newStart = start > end ? start - pad : start + pad
const newEnd = end > start ? end - pad : end + pad
return [newStart, newEnd] as [RangeValue, RangeValue]
} else {
return range
}
}
protected processDomainValues(
values: QuantitativeValue[],
): [DomainValue, DomainValue] {
const result = extent(values as any[]) as [DomainValue, DomainValue]
if (this.getZero() && !this.domainContainsZero(result)) {
const [min, max] = result
const zero = 0 as DomainValue
return 0 < min ? [zero, max] : [min, zero]
}
return result
}
protected domainContainsZero(domain: [DomainValue, DomainValue]) {
return 0 >= domain[0] && 0 <= domain[1]
}
protected getZero() {
return this.zeroValue !== undefined ? this.zeroValue : this.defaultZero
}
protected handleRangeBind(
args: ScaleCreationContext,
rangeBind: Dimension,
): [RangeValue, RangeValue] {
return getBoundRange(args, rangeBind) as [RangeValue, RangeValue]
}
protected addCommonProperties(scale: any) {
if (this.niceValue === true) {
scale.nice()
} else if (this.niceValue !== undefined) {
scale.nice(this.niceValue)
}
if (this.clampValue !== undefined) {
scale.clamp(this.clampValue)
}
if (this.roundValue === true) {
scale.interpolate(interpolateRound)
}
}
}