-
Notifications
You must be signed in to change notification settings - Fork 37
/
PointScale.ts
86 lines (74 loc) · 1.86 KB
/
PointScale.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
/*!
* Copyright (c) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project.
*/
import {
Dimension,
ScaleCreationContext,
Scales,
Scale,
} from '@chart-parts/interfaces'
import { scalePoint } from 'd3-scale'
import { DomainRangeScale } from '../DomainRangeScale'
import { getBoundRange } from '../getBoundRange'
import { optionalArgument } from '../util'
export class PointScale extends DomainRangeScale<
string[],
[number, number],
Dimension
> {
private alignValue?: number
private paddingValue?: number
private roundValue?: boolean
private stepNameValue?: string
/**
* Bin alignment 0-beginning, 1=end
*/
public align(value?: number): this {
this.alignValue = value
return this
}
/**
* The outer and inner padding value
*/
public padding(value?: number): this {
this.paddingValue = value
return this
}
public round(value?: boolean): this {
this.roundValue = optionalArgument(value, arguments.length, true, false)
return this
}
public stepName(value?: string): this {
this.stepNameValue = value
return this
}
protected createScale(args: ScaleCreationContext): Scales {
const scale = scalePoint()
.domain(this.getDomain(args))
.range(this.getRange(args))
if (this.paddingValue !== undefined) {
scale.padding(this.paddingValue)
}
if (this.alignValue !== undefined) {
scale.align(this.alignValue)
}
if (this.roundValue !== undefined) {
scale.round(this.roundValue)
}
if (!this.nameValue) {
throw new Error('scale name must be defined')
}
const result: Scales = { [this.nameValue]: scale }
if (this.stepNameValue) {
result[this.stepNameValue as string] = scale.step as Scale<any, number>
}
return result
}
protected handleRangeBind(
args: ScaleCreationContext,
rangeBind: Dimension,
): [number, number] {
return getBoundRange(args, rangeBind)
}
}