-
Notifications
You must be signed in to change notification settings - Fork 37
/
BandScale.ts
142 lines (125 loc) · 2.76 KB
/
BandScale.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
133
134
135
136
137
138
139
140
141
142
/*!
* Copyright (c) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project.
*/
import {
ScaleCreationContext,
Dimension,
Scales,
Scale,
} from '@chart-parts/interfaces'
import { scaleBand } from 'd3-scale'
import { DomainRangeScale } from '../DomainRangeScale'
import { getBoundRange } from '../getBoundRange'
export interface BandScaleProps {
/**
* The name of the band-width static scale
*/
bandWidth?: string
/**
* The outer and inner padding value
*/
padding?: number
/**
* The outer and inner padding value
*/
paddingInner?: number
/**
* The outer and inner padding value
*/
paddingOuter?: number
/**
* Bin alignment 0-beginning, 1=end
*/
align?: number
}
export class BandScale extends DomainRangeScale<
string[],
[number, number],
Dimension
> {
private paddingValue?: number
private paddingInnerValue?: number
private paddingOuterValue?: number
private alignValue?: number
private bandwidthValue?: string
private roundValue?: boolean
/**
* The name of the band-width static scale
*/
public bandwidthName(value?: string): this {
this.bandwidthValue = value
return this
}
/**
* The outer and inner padding value
*/
public padding(value?: number): this {
this.paddingValue = value
return this
}
/**
* The inner padding value
*/
public paddingInner(value?: number): this {
this.paddingInnerValue = value
return this
}
/**
* The outer padding value
*/
public paddingOuter(value?: number): this {
this.paddingOuterValue = value
return this
}
/**
* Bin alignment 0-beginning, 1=end
*/
public align(value?: number): this {
this.alignValue = value
return this
}
public round(value?: boolean): this {
this.roundValue = value
return this
}
protected createScale(args: ScaleCreationContext): Scales {
const domain = this.getDomain(args)
const range = this.getRange(args)
const bandscale = scaleBand()
.domain(domain.map(d => '' + d))
.range(range)
if (this.alignValue) {
bandscale.align(this.alignValue)
}
if (this.paddingValue) {
bandscale.padding(this.paddingValue)
}
if (this.paddingOuterValue) {
bandscale.paddingOuter(this.paddingOuterValue)
}
if (this.paddingInnerValue) {
bandscale.paddingInner(this.paddingInnerValue)
}
if (this.roundValue) {
bandscale.round(this.roundValue)
}
if (!this.nameValue) {
throw new Error('scale name must be defined')
}
const result: Scales = { [this.nameValue]: bandscale }
if (this.bandwidthValue) {
result[this.bandwidthValue as string] = bandscale.bandwidth as Scale<
any,
number
>
}
return result
}
protected handleRangeBind(
args: ScaleCreationContext,
rangeBind: Dimension,
): [number, number] {
return getBoundRange(args, rangeBind)
}
}