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

commonMain.jetbrains.datalore.plot.builder.interact.GeomInteractionBuilderUtil.kt Maven / Gradle / Ivy

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

import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.builder.tooltip.*

internal object GeomInteractionBuilderUtil {

    fun createTooltipLines(
        userTooltipSpec: TooltipSpecification?,
        tooltipAes: List>,
        tooltipAxisAes: List>,
        sideTooltipAes: List>,
        tooltipConstantAes: Map, Any>?,
    ): List {

        return when {
            userTooltipSpec == null -> {
                // No user tooltip specification => use default tooltips
                defaultValueSourceTooltipLines(
                    tooltipAes,
                    tooltipAxisAes,
                    sideTooltipAes,
                    userDefinedValueSources = null,
                    constantsMap = tooltipConstantAes
                )
            }
            userTooltipSpec.useDefaultTooltips() -> {
                // No user line patterns => use default tooltips with the given formatted valueSources
                defaultValueSourceTooltipLines(
                    tooltipAes,
                    tooltipAxisAes,
                    sideTooltipAes,
                    userTooltipSpec.valueSources,
                    tooltipConstantAes
                )
            }
            userTooltipSpec.hideTooltips() -> {
                // User list is empty => not show tooltips
                emptyList()
            }
            else -> {
                // Form value sources: user list + axis + outliers
                val geomOutliers = sideTooltipAes.toMutableList()

                // Remove outlier tooltip if the mappedAes is used in the general tooltip
                userTooltipSpec.tooltipLinePatterns!!.forEach { line ->
                    val userDataAesList = line.fields.filterIsInstance().map { it.aes }
                    geomOutliers.removeAll(userDataAesList)
                }
                val axisValueSources = tooltipAxisAes.map { aes ->
                    getMappingValueSource(aes, isOutlier = true, isAxis = true, userTooltipSpec.valueSources)
                }
                val geomOutlierValueSources = geomOutliers.map { aes ->
                    getMappingValueSource(aes, isOutlier = true, isAxis = false, userTooltipSpec.valueSources)
                }

                userTooltipSpec.tooltipLinePatterns +
                        (axisValueSources + geomOutlierValueSources)
                            .map(TooltipLine.Companion::defaultLineForValueSource)
            }
        }
    }


    private fun getMappingValueSource(
        aes: Aes<*>,
        isOutlier: Boolean,
        isAxis: Boolean,
        userDefinedValueSources: List?
    ): ValueSource {
        val userDefined = userDefinedValueSources?.filterIsInstance()?.find { it.aes == aes }
        return userDefined?.withFlags(isOutlier, isAxis) ?: MappingValue(
            aes,
            isOutlier = isOutlier,
            isAxis = isAxis
        )
    }

    internal fun defaultValueSourceTooltipLines(
        aesListForTooltip: List>,
        axisAes: List>,
        outliers: List>,
        userDefinedValueSources: List? = null,
        constantsMap: Map, Any>? = null
    ): List {
        val axisValueSources = axisAes.map { aes ->
            getMappingValueSource(aes, isOutlier = true, isAxis = true, userDefinedValueSources)
        }
        val outlierValueSources = outliers.map { aes ->
            getMappingValueSource(aes, isOutlier = true, isAxis = false, userDefinedValueSources)
        }
        val aesValueSources = aesListForTooltip.map { aes ->
            getMappingValueSource(aes, isOutlier = false, isAxis = false, userDefinedValueSources)
        }
        val constantValues = constantsMap?.map { (aes, value) -> ConstantValue(aes, value, format = null) } ?: emptyList()
        return (aesValueSources + axisValueSources + outlierValueSources + constantValues).map(TooltipLine.Companion::defaultLineForValueSource)
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy