commonMain.jetbrains.datalore.plot.config.PlotConfigScaleProviders.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) 2022. 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.builder.scale.ScaleProvider
import jetbrains.datalore.plot.builder.scale.ScaleProviderHelper
internal object PlotConfigScaleProviders {
internal fun createScaleProviders(
layerConfigs: List,
scaleConfigs: List>,
excludeStatVariables: Boolean
): Map, ScaleProvider<*>> {
val scaleProviderByAes = HashMap, ScaleProvider<*>>()
// Create 'configured' scale providers.
for (scaleConfig in scaleConfigs) {
val scaleProvider = scaleConfig.createScaleProvider()
scaleProviderByAes[scaleConfig.aes] = scaleProvider
}
val setup = PlotConfigUtil.createPlotAesBindingSetup(layerConfigs, excludeStatVariables)
val dataByVarBinding = setup.dataByVarBinding
val variablesByMappedAes = setup.variablesByMappedAes
// Append date-time scale provider
val dateTimeDataByVarBinding = dataByVarBinding
.filter { (varBinding, df) ->
df.isDateTime(varBinding.variable)
}
dateTimeDataByVarBinding
.map { (varBinding, _) -> varBinding.aes }
.filter { aes -> aes in setOf(Aes.X, Aes.Y) }
.filter { aes -> aes !in scaleProviderByAes }
.forEach { aes ->
val name = PlotConfigUtil.defaultScaleName(aes, variablesByMappedAes)
scaleProviderByAes[aes] = ScaleProviderHelper.createDateTimeScaleProvider(aes, name)
}
// All aes used in bindings and x/y aes.
// Exclude "stat positional" because we don't know which of axis they will use (i.e. orientation="y").
val aesSet = setup.mappedAesWithoutStatPositional() + setOf(Aes.X, Aes.Y)
// Append all the rest scale providers.
return aesSet.associateWith { aes ->
val scaleAes = when {
Aes.isPositionalX(aes) -> Aes.X
Aes.isPositionalY(aes) -> Aes.Y
else -> aes
}
scaleProviderByAes.getOrElse(scaleAes) {
ScaleProviderHelper.createDefault(scaleAes)
}
}
}
}