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

commonMain.jetbrains.datalore.plot.builder.assemble.LegendAssemblerUtil.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.assemble

import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.base.Aesthetics
import jetbrains.datalore.plot.base.aes.AestheticsBuilder
import jetbrains.datalore.plot.base.aes.AestheticsDefaults
import jetbrains.datalore.plot.builder.guide.LegendDirection
import jetbrains.datalore.plot.builder.guide.LegendPosition
import jetbrains.datalore.plot.builder.theme.LegendTheme

internal object LegendAssemblerUtil {
    fun  mapToAesthetics(
        valuesByAes: Map, List>, constantByAes: Map, T>, aestheticsDefaults: AestheticsDefaults
    ): Aesthetics {
        val builder = AestheticsBuilder(0)
        for (aes in Aes.values()) {
            @Suppress("UNCHECKED_CAST")
            builder.constantAes(aes as Aes, aestheticsDefaults.defaultValue(aes))
        }
        for (aes in valuesByAes.keys) {
            val values = valuesByAes[aes]!!
            builder.aes(aes, AestheticsBuilder.collection(values))
            builder.dataPointCount(values.size)
        }
        for (aes in constantByAes.keys) {
            builder.constantAes(aes, constantByAes[aes]!!)
        }
        return builder.build()
    }


    fun mapToAesthetics(
        valueByAesIterable: Collection, Any>>,
        constantByAes: Map, Any>,
        aestheticsDefaults: AestheticsDefaults
    ): Aesthetics {
        val dataPoints = ArrayList, Any>>()
        for (valueByAes in valueByAesIterable) {
            val dataPoint = HashMap, Any>()
            for (aes in Aes.values()) {
                dataPoint[aes] = aestheticsDefaults.defaultValueInLegend(aes)!!
            }

            // Derive some aesthetics from constants
            for (constantAes in constantByAes.keys) {
                when (constantAes) {
                    Aes.SHAPE,
                    Aes.COLOR,
                    Aes.FILL -> dataPoint[constantAes] = constantByAes[constantAes]!!
                }
            }

            for (aes in valueByAes.keys) {
                dataPoint[aes] = valueByAes[aes]!!
            }

            dataPoints.add(dataPoint)
        }

        val builder = AestheticsBuilder(dataPoints.size)
        for (aes in Aes.values()) {
            @Suppress("UNCHECKED_CAST")
            builder.aes(aes as Aes) { index -> dataPoints[index][aes]!! }
        }
        return builder.build()
    }

    fun legendDirection(theme: LegendTheme): LegendDirection {
        var legendDirection = theme.direction()
        if (legendDirection === LegendDirection.AUTO) {
            val legendPosition = theme.position()
            legendDirection =
                if (legendPosition === LegendPosition.TOP || legendPosition === LegendPosition.BOTTOM)
                    LegendDirection.HORIZONTAL
                else
                    LegendDirection.VERTICAL
        }
        return legendDirection
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy