All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.jetbrains.datalore.plot.builder.scale.DefaultMapperProviderUtil.kt Maven / Gradle / Ivy

There is a newer version: 4.5.3-alpha1
Show newest version
/*
 * 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.scale

import jetbrains.datalore.base.interval.DoubleSpan
import jetbrains.datalore.base.values.Color
import jetbrains.datalore.plot.base.ContinuousTransform
import jetbrains.datalore.plot.base.DiscreteTransform
import jetbrains.datalore.plot.base.ScaleMapper
import jetbrains.datalore.plot.base.scale.MapperUtil
import jetbrains.datalore.plot.builder.scale.mapper.GuideMappers
import jetbrains.datalore.plot.builder.scale.provider.ColorBrewerMapperProvider
import jetbrains.datalore.plot.builder.scale.provider.ColorGradientMapperProvider
import jetbrains.datalore.plot.builder.scale.provider.IdentityDiscreteMapperProvider
import jetbrains.datalore.plot.builder.scale.provider.IdentityMapperProvider

object DefaultMapperProviderUtil {

    internal fun createColorMapperProvider(): MapperProvider {
        val discrete = ColorBrewerMapperProvider(null, null, null, Color.GRAY)
        val continuous = ColorGradientMapperProvider.DEFAULT
        return object : MapperProvider {
            override fun createDiscreteMapper(discreteTransform: DiscreteTransform): ScaleMapper {
                return discrete.createDiscreteMapper(discreteTransform)
            }

            override fun createContinuousMapper(
                domain: DoubleSpan,
                trans: ContinuousTransform
            ): GuideMapper {
                return continuous.createContinuousMapper(domain, trans)
            }
        }
    }

    fun  createWithDiscreteOutput(outputValues: List, naValue: T): MapperProvider {
        return object : MapperProvider {
            override fun createDiscreteMapper(discreteTransform: DiscreteTransform): ScaleMapper {
                return GuideMappers.discreteToDiscrete(discreteTransform, outputValues, naValue)
            }

            override fun createContinuousMapper(
                domain: DoubleSpan,
                trans: ContinuousTransform
            ): GuideMapper {
                return GuideMappers.continuousToDiscrete(
                    MapperUtil.rangeWithLimitsAfterTransform(domain, trans),
                    outputValues, naValue
                )
            }
        }
    }

    internal fun createObjectIdentity(): MapperProvider {
        val converter: (Any?) -> Any? = { it }
        val discreteMapperProvider = IdentityDiscreteMapperProvider(converter)
        val continuousMapper = object : ScaleMapper {
            override fun invoke(v: Double?): Any? = v
        }
        return IdentityMapperProvider(discreteMapperProvider, continuousMapper)
    }

    internal fun createStringIdentity(): MapperProvider {
        val converter = { it: Any? -> it?.toString() }
        return IdentityDiscreteMapperProvider(converter)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy