-
Notifications
You must be signed in to change notification settings - Fork 47
/
CoordProviderBase.kt
88 lines (77 loc) · 3.13 KB
/
CoordProviderBase.kt
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
/*
* Copyright (c) 2019. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package jetbrains.datalore.plot.builder.coord
import jetbrains.datalore.base.gcommon.collect.ClosedRange
import jetbrains.datalore.base.geometry.DoubleVector
import jetbrains.datalore.base.values.Pair
import jetbrains.datalore.plot.base.CoordinateSystem
import jetbrains.datalore.plot.base.Scale
import jetbrains.datalore.plot.base.coord.Coords
import jetbrains.datalore.plot.base.scale.MapperUtil
import jetbrains.datalore.plot.base.scale.Mappers
import jetbrains.datalore.plot.builder.layout.axis.GuideBreaks
internal abstract class CoordProviderBase(
private val xLim: ClosedRange<Double>?,
private val yLim: ClosedRange<Double>?
) : CoordProvider {
override fun buildAxisScaleX(scaleProto: Scale<Double>, domain: ClosedRange<Double>, axisLength: Double, breaks: GuideBreaks): Scale<Double> {
return buildAxisScaleDefault(
scaleProto,
domain,
axisLength,
breaks
)
}
override fun buildAxisScaleY(scaleProto: Scale<Double>, domain: ClosedRange<Double>, axisLength: Double, breaks: GuideBreaks): Scale<Double> {
return buildAxisScaleDefault(
scaleProto,
domain,
axisLength,
breaks
)
}
override fun createCoordinateSystem(xDomain: ClosedRange<Double>, xAxisLength: Double, yDomain: ClosedRange<Double>, yAxisLength: Double): CoordinateSystem {
return Coords.create(
MapperUtil.map(xDomain,
axisMapper(xDomain, xAxisLength)
),
MapperUtil.map(yDomain,
axisMapper(yDomain, yAxisLength)
))
}
final override fun adjustDomains(
xDomain: ClosedRange<Double>,
yDomain: ClosedRange<Double>,
displaySize: DoubleVector
): Pair<ClosedRange<Double>, ClosedRange<Double>> {
return adjustDomainsImpl(xDomain, yDomain, displaySize)
}
protected open fun adjustDomainsImpl(
xDomain: ClosedRange<Double>,
yDomain: ClosedRange<Double>,
displaySize: DoubleVector
): Pair<ClosedRange<Double>, ClosedRange<Double>> {
return Pair(xLim ?: xDomain, yLim ?: yDomain)
}
companion object {
fun axisMapper(domain: ClosedRange<Double>, axisLength: Double): (Double?) -> Double? {
return Mappers.mul(domain, axisLength)
}
private fun buildAxisScaleDefault(scaleProto: Scale<Double>, domain: ClosedRange<Double>, axisLength: Double, breaks: GuideBreaks): Scale<Double> {
return buildAxisScaleDefault(
scaleProto,
axisMapper(domain, axisLength),
breaks
)
}
fun buildAxisScaleDefault(scaleProto: Scale<Double>, axisMapper: (Double?) -> Double?, breaks: GuideBreaks): Scale<Double> {
return scaleProto.with()
.breaks(breaks.domainValues)
.labels(breaks.labels)
.mapper(axisMapper)
.build()
}
}
}