-
Notifications
You must be signed in to change notification settings - Fork 49
/
ScaleConfig.kt
269 lines (243 loc) · 10.8 KB
/
ScaleConfig.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* 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.config
import jetbrains.datalore.base.gcommon.base.Preconditions.checkArgument
import jetbrains.datalore.base.stringFormat.StringFormat
import jetbrains.datalore.base.stringFormat.StringFormat.FormatType.DATETIME_FORMAT
import jetbrains.datalore.base.values.Color
import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.base.scale.Mappers.nullable
import jetbrains.datalore.plot.base.scale.transform.DateTimeBreaksGen
import jetbrains.datalore.plot.base.scale.transform.Transforms
import jetbrains.datalore.plot.builder.scale.*
import jetbrains.datalore.plot.builder.scale.mapper.ShapeMapper
import jetbrains.datalore.plot.builder.scale.provider.*
import jetbrains.datalore.plot.common.text.Formatter
import jetbrains.datalore.plot.config.Option.Scale.AES
import jetbrains.datalore.plot.config.Option.Scale.BREAKS
import jetbrains.datalore.plot.config.Option.Scale.CHROMA
import jetbrains.datalore.plot.config.Option.Scale.DIRECTION
import jetbrains.datalore.plot.config.Option.Scale.END
import jetbrains.datalore.plot.config.Option.Scale.EXPAND
import jetbrains.datalore.plot.config.Option.Scale.FORMAT
import jetbrains.datalore.plot.config.Option.Scale.GUIDE
import jetbrains.datalore.plot.config.Option.Scale.HIGH
import jetbrains.datalore.plot.config.Option.Scale.HUE_RANGE
import jetbrains.datalore.plot.config.Option.Scale.LABELS
import jetbrains.datalore.plot.config.Option.Scale.LIMITS
import jetbrains.datalore.plot.config.Option.Scale.LOW
import jetbrains.datalore.plot.config.Option.Scale.LUMINANCE
import jetbrains.datalore.plot.config.Option.Scale.MAX_SIZE
import jetbrains.datalore.plot.config.Option.Scale.MID
import jetbrains.datalore.plot.config.Option.Scale.MIDPOINT
import jetbrains.datalore.plot.config.Option.Scale.NAME
import jetbrains.datalore.plot.config.Option.Scale.NA_VALUE
import jetbrains.datalore.plot.config.Option.Scale.OUTPUT_VALUES
import jetbrains.datalore.plot.config.Option.Scale.PALETTE
import jetbrains.datalore.plot.config.Option.Scale.PALETTE_TYPE
import jetbrains.datalore.plot.config.Option.Scale.RANGE
import jetbrains.datalore.plot.config.Option.Scale.SCALE_MAPPER_KIND
import jetbrains.datalore.plot.config.Option.Scale.SHAPE_SOLID
import jetbrains.datalore.plot.config.Option.Scale.START
import jetbrains.datalore.plot.config.Option.Scale.START_HUE
import jetbrains.datalore.plot.config.Option.TransformName
import jetbrains.datalore.plot.config.aes.AesOptionConversion
import jetbrains.datalore.plot.config.aes.TypedContinuousIdentityMappers
/**
* @param <T> - target aesthetic type of the configured scale
*/
class ScaleConfig<T>(options: Map<String, Any>) : OptionsAccessor(options) {
@Suppress("UNCHECKED_CAST")
val aes: Aes<T> = aesOrFail(options) as Aes<T>
fun createScaleProvider(): ScaleProvider<T> {
return createScaleProviderBuilder().build()
}
private fun createScaleProviderBuilder(): ScaleProviderBuilder<T> {
var mapperProvider: MapperProvider<*>? = null
val naValue: T = when {
has(NA_VALUE) -> getValue(aes, NA_VALUE)!!
else -> DefaultNaValue[aes]
}
// all 'manual' scales
if (has(OUTPUT_VALUES)) {
val outputValues = getList(OUTPUT_VALUES)
val mapperOutputValues = AesOptionConversion.applyToList(aes, outputValues)
mapperProvider = DefaultMapperProviderUtil.createWithDiscreteOutput(mapperOutputValues, naValue)
}
if (aes == Aes.SHAPE) {
val solid = get(SHAPE_SOLID)
// False - show only hollow shapes, otherwise - all (default)
if (solid is Boolean && solid == false) {
mapperProvider = DefaultMapperProviderUtil.createWithDiscreteOutput(
ShapeMapper.hollowShapes(), ShapeMapper.NA_VALUE
)
}
} else if (aes == Aes.ALPHA && has(RANGE)) {
mapperProvider =
AlphaMapperProvider(getRange(RANGE), (naValue as Double))
} else if (aes == Aes.SIZE && has(RANGE)) {
mapperProvider =
SizeMapperProvider(getRange(RANGE), (naValue as Double))
}
// used in scale_x_discrete, scale_y_discrete
val discreteDomain = getBoolean(Option.Scale.DISCRETE_DOMAIN)
val reverse = getBoolean(Option.Scale.DISCRETE_DOMAIN_REVERSE)
val scaleMapperKind =
getString(SCALE_MAPPER_KIND) ?: if (!has(OUTPUT_VALUES) && discreteDomain && aes in setOf<Aes<*>>(
Aes.FILL,
Aes.COLOR
)
)
// Default palette type for discrete colors
COLOR_BREWER
else
null
when (scaleMapperKind) {
null -> {
}
IDENTITY ->
mapperProvider = createIdentityMapperProvider(aes, naValue)
COLOR_GRADIENT ->
mapperProvider = ColorGradientMapperProvider(
getColor(LOW),
getColor(HIGH),
(naValue as Color)
)
COLOR_GRADIENT2 ->
mapperProvider = ColorGradient2MapperProvider(
getColor(LOW),
getColor(MID),
getColor(HIGH),
getDouble(MIDPOINT), naValue as Color
)
COLOR_HUE ->
mapperProvider = ColorHueMapperProvider(
getDoubleList(HUE_RANGE),
getDouble(CHROMA),
getDouble(LUMINANCE),
getDouble(START_HUE),
getDouble(DIRECTION), naValue as Color
)
COLOR_GREY ->
mapperProvider = GreyscaleLightnessMapperProvider(
getDouble(START),
getDouble(END),
naValue as Color
)
COLOR_BREWER ->
mapperProvider = ColorBrewerMapperProvider(
getString(PALETTE_TYPE),
get(PALETTE),
getDouble(DIRECTION),
naValue as Color
)
SIZE_AREA ->
mapperProvider = SizeAreaMapperProvider(
getDouble(MAX_SIZE),
naValue as Double
)
else ->
throw IllegalArgumentException("Aes '" + aes.name + "' - unexpected scale mapper kind: '" + scaleMapperKind + "'")
}
val b = ScaleProviderBuilder(aes)
if (mapperProvider != null) {
@Suppress("UNCHECKED_CAST")
b.mapperProvider(mapperProvider as MapperProvider<T>)
}
b.discreteDomain(discreteDomain)
b.discreteDomainReverse(reverse)
if (getBoolean(Option.Scale.DATE_TIME)) {
val dateTimeFormatter = getString(FORMAT)?.let { pattern ->
val stringFormat = StringFormat.forOneArg(pattern, type = DATETIME_FORMAT)
return@let { value: Any -> stringFormat.format(value) }
}
b.breaksGenerator(DateTimeBreaksGen(dateTimeFormatter))
} else if (!discreteDomain && has(Option.Scale.CONTINUOUS_TRANSFORM)) {
val transformName = getStringSafe(Option.Scale.CONTINUOUS_TRANSFORM)
val transform = when (transformName.lowercase()) {
TransformName.IDENTITY -> Transforms.IDENTITY
TransformName.LOG10 -> Transforms.LOG10
TransformName.REVERSE -> Transforms.REVERSE
TransformName.SQRT -> Transforms.SQRT
else -> throw IllegalArgumentException(
"Unknown transform name: '$transformName'. Supported: ${
listOf(
TransformName.IDENTITY,
TransformName.LOG10,
TransformName.REVERSE,
TransformName.SQRT
).joinToString(transform = { "'$it'" })
}."
)
}
b.continuousTransform(transform)
}
return applyCommons(b)
}
private fun applyCommons(b: ScaleProviderBuilder<T>): ScaleProviderBuilder<T> {
if (has(NAME)) {
b.name(getString(NAME)!!)
}
if (has(BREAKS)) {
b.breaks(getList(BREAKS).mapNotNull { it })
}
if (has(LABELS)) {
b.labels(getStringList(LABELS))
} else {
// Skip format is labels are defined
b.labelFormat(getString(FORMAT))
}
if (has(EXPAND)) {
val list = getList(EXPAND)
if (list.isNotEmpty()) {
val multiplicativeExpand = list[0] as Number
b.multiplicativeExpand(multiplicativeExpand.toDouble())
if (list.size > 1) {
val additiveExpand = list[1] as Number
b.additiveExpand(additiveExpand.toDouble())
}
}
}
if (has(LIMITS)) {
b.limits(this.getList(LIMITS))
}
return b
}
fun hasGuideOptions(): Boolean {
return has(GUIDE)
}
fun getGuideOptions(): GuideConfig {
return GuideConfig.create(get(GUIDE)!!)
}
companion object {
private const val IDENTITY = "identity"
private const val COLOR_GRADIENT = "color_gradient"
private const val COLOR_GRADIENT2 = "color_gradient2"
private const val COLOR_HUE = "color_hue"
private const val COLOR_GREY = "color_grey"
private const val COLOR_BREWER = "color_brewer"
private const val SIZE_AREA = "size_area"
fun aesOrFail(options: Map<String, Any>): Aes<*> {
val accessor = OptionsAccessor(options)
require(accessor.has(AES)) { "Required parameter '$AES' is missing" }
return Option.Mapping.toAes(accessor.getStringSafe(AES))
}
fun <T> createIdentityMapperProvider(aes: Aes<T>, naValue: T): MapperProvider<T> {
// There is an option value converter for every AES (which can be used as discrete identity mapper)
val cvt = AesOptionConversion.getConverter(aes)
val discreteMapperProvider =
IdentityDiscreteMapperProvider(cvt, naValue)
// For some AES there is also a continuous identity mapper
if (TypedContinuousIdentityMappers.contain(aes)) {
val continuousMapper = TypedContinuousIdentityMappers[aes]
return IdentityMapperProvider(
discreteMapperProvider,
nullable(continuousMapper, naValue)
)
}
return discreteMapperProvider
}
}
}