-
Notifications
You must be signed in to change notification settings - Fork 37
/
densityBuilder.ts
76 lines (64 loc) · 1.81 KB
/
densityBuilder.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
/*!
* Copyright (c) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project.
*/
/* eslint-disable @typescript-eslint/no-var-requires */
import { DatasetManager } from '../dataset'
import {
DensityBuilder,
DensityMethod,
DistributionBuilder,
} from './interfaces'
const { density: vegaDensity } = require('vega-transforms')
export class DensityBuilderImpl implements DensityBuilder {
private distributionValue: DistributionBuilder | undefined
private extentValue: [number, number] | undefined
private methodValue: DensityMethod | undefined
private stepsValue: number | undefined
private asValue: [string, string] | undefined
public distribution(value: DistributionBuilder): DensityBuilder {
this.distributionValue = value
return this
}
public extent(start: number, end: number): DensityBuilder {
this.extentValue = [start, end]
return this
}
public method(value: DensityMethod): DensityBuilder {
this.methodValue = value
return this
}
public steps(value: number): DensityBuilder {
this.stepsValue = value
return this
}
public as(valueString: string, probabilityField: string): DensityBuilder {
this.asValue = [valueString, probabilityField]
return this
}
public build(df: any, from: any, ds: DatasetManager) {
if (!this.distributionValue) {
throw new Error('density distribution() must be defined')
}
const spec: any = {
distribution: this.distributionValue.build(ds),
pulse: from,
}
if (this.extentValue) {
spec.extent = this.extentValue
}
if (this.methodValue) {
spec.method = this.methodValue
}
if (this.stepsValue) {
spec.steps = this.stepsValue
}
if (this.asValue) {
spec.as = this.asValue
}
return df.add(vegaDensity, spec)
}
}
export function density(): DensityBuilder {
return new DensityBuilderImpl()
}