commonMain.jetbrains.datalore.plot.config.LayerConfigUtil.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plot-config-portable-js Show documentation
Show all versions of plot-config-portable-js Show documentation
The Let-Plot Kotlin API depends on this artifact.
/*
* 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.plot.base.Aes
import jetbrains.datalore.plot.base.DataFrame
import jetbrains.datalore.plot.base.DataFrame.Variable
import jetbrains.datalore.plot.builder.VarBinding
import jetbrains.datalore.plot.builder.sampling.Sampling
import jetbrains.datalore.plot.config.Option.Layer.POS
import jetbrains.datalore.plot.config.Option.Layer.SAMPLING
import jetbrains.datalore.plot.config.aes.AesOptionConversion
internal object LayerConfigUtil {
fun positionAdjustmentOptions(layerOptions: OptionsAccessor, geomProto: GeomProto): Map {
val preferredPosOptions: Map = geomProto.preferredPositionAdjustmentOptions(layerOptions)
val specifiedPosOptions: Map = when (val v = layerOptions[POS]) {
null -> preferredPosOptions
is Map<*, *> ->
@Suppress("UNCHECKED_CAST")
v as Map
else ->
mapOf(Option.Meta.NAME to v.toString())
}
return if (specifiedPosOptions[Option.Meta.NAME] == preferredPosOptions[Option.Meta.NAME]) {
// Merge
preferredPosOptions + specifiedPosOptions
} else {
specifiedPosOptions
}
}
fun initConstants(layerConfig: OptionsAccessor, consumedAesSet: Set>): Map, Any> {
val result = HashMap, Any>()
Option.Mapping.REAL_AES_OPTION_NAMES
.filter(layerConfig::has)
.associateWith(Option.Mapping::toAes)
.filterValues { aes -> aes in consumedAesSet }
.forEach { (option, aes) ->
val optionValue = layerConfig[option]!!
val constantValue = AesOptionConversion.apply(aes, optionValue)
?: throw IllegalArgumentException("Can't convert to '$option' value: $optionValue")
result[aes] = constantValue
}
return result
}
fun createBindings(
data: DataFrame,
mapping: Map, Variable>?,
consumedAesSet: Set>,
clientSide: Boolean
): List {
val result = ArrayList()
if (mapping != null) {
val aesSet = HashSet(consumedAesSet)
aesSet.retainAll(mapping.keys)
for (aes in aesSet) {
val variable = mapping.getValue(aes)
val binding: VarBinding = when {
data.has(variable) -> VarBinding(variable, aes)
variable.isStat && !clientSide -> VarBinding(variable, aes) // 'stat' is not yet built.
else -> throw IllegalArgumentException(
data.undefinedVariableErrorMessage(variable.name)
)
}
result.add(binding)
}
}
return result
}
fun initSampling(opts: OptionsAccessor, defaultSampling: Sampling): List {
return if (opts.has(SAMPLING)) {
SamplingConfig.create(opts[SAMPLING]!!)
} else listOf(defaultSampling)
}
}