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.gcommon.collect.ClosedRange
import jetbrains.datalore.base.values.Color
import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.base.Transform
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

object DefaultMapperProviderUtil {

    internal fun createColorMapperProvider(): MapperProvider {
        val discrete = ColorBrewerMapperProvider(null, null, null, Color.GRAY)
        val continuous = ColorGradientMapperProvider.DEFAULT
        return object : MapperProvider {
            override fun createDiscreteMapper(domainValues: Collection<*>): GuideMapper {
                return discrete.createDiscreteMapper(domainValues)
            }

            override fun createContinuousMapper(
                domain: ClosedRange,
                lowerLimit: Double?,
                upperLimit: Double?,
                trans: Transform?
            ): GuideMapper {
                return continuous.createContinuousMapper(domain, lowerLimit, upperLimit, trans)
            }
        }
    }

    fun  createWithDiscreteOutput(outputValues: List, naValue: T): MapperProvider {
        return object : MapperProvider {
            override fun createDiscreteMapper(domainValues: Collection<*>): GuideMapper {
                return GuideMappers.discreteToDiscrete(domainValues, outputValues, naValue)
            }

            override fun createContinuousMapper(
                domain: ClosedRange,
                lowerLimit: Double?,
                upperLimit: Double?,
                trans: Transform?
            ): GuideMapper {
                return GuideMappers.continuousToDiscrete(
                    MapperUtil.rangeWithLimitsAfterTransform(domain, lowerLimit, upperLimit, trans),
                    outputValues, naValue
                )
            }
        }
    }

    @Suppress("UNUSED_PARAMETER")
    internal fun createObjectIdentity(aes: Aes): MapperProvider {
        return object : IdentityDiscreteMapperProvider({ it }, null) {
            override fun createContinuousMapper(
                domain: ClosedRange,
                lowerLimit: Double?,
                upperLimit: Double?,
                trans: Transform?
            ): GuideMapper {
                return GuideMappers.adaptContinuous { it }
            }
        }
    }

    internal fun createObjectIdentityDiscrete(aes: Aes): MapperProvider {
        val converter: (Any?) -> Any? = { it }
        return createIdentityMapperProvider(aes, converter, null)
    }

    internal fun createStringIdentity(aes: Aes): MapperProvider {
        val converter = { it: Any? -> it?.toString() }
        val continuousMapper = { it: Double? -> it?.toString() }
        return createIdentityMapperProvider(
            aes,
            converter,
            continuousMapper
        )
    }

    private fun  createIdentityMapperProvider(
        aes: Aes,
        converter: (Any?) -> T?,
        continuousMapper: ((Double?) -> T?)?
    ): MapperProvider {
        return object : IdentityDiscreteMapperProvider(converter, DefaultNaValue[aes]) {
            override fun createContinuousMapper(
                domain: ClosedRange,
                lowerLimit: Double?,
                upperLimit: Double?,
                trans: Transform?
            ): GuideMapper {
                if (continuousMapper != null) {
                    return GuideMappers.adaptContinuous(continuousMapper)
                }
                throw IllegalStateException("Can't create $aes mapper for continuous domain $domain")
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy