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

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

import jetbrains.datalore.base.geometry.DoubleVector
import jetbrains.datalore.plot.base.CoordinateSystem
import jetbrains.datalore.plot.base.Scale
import jetbrains.datalore.plot.base.scale.ScaleBreaks
import jetbrains.datalore.plot.base.scale.ScaleUtil
import jetbrains.datalore.plot.builder.guide.AxisComponent

object AxisUtil {
    fun breaksData(
        scale: Scale,
        coord: CoordinateSystem,
        horizontal: Boolean
    ): AxisComponent.BreaksData {
        val scaleBreaks = scale.getScaleBreaks()
        val mappedBreaks = toAxisCoord(scaleBreaks, scale, coord, horizontal)
        return AxisComponent.BreaksData(
            majorBreaks = mappedBreaks,
            majorLabels = scaleBreaks.labels
        )
    }

    private fun toAxisCoord(
        scaleBreaks: ScaleBreaks,
        scale: Scale,
        coord: CoordinateSystem,
        horizontal: Boolean
    ): List {
        val breaksMapped = ScaleUtil.map(scaleBreaks.transformedValues, scale).map {
            // Don't expect NULLs.
            it as Double
        }
        val axisBreaks = ArrayList()
        for (br in breaksMapped) {
            val mappedBrPoint = when (horizontal) {
                true -> DoubleVector(br, 0.0)
                false -> DoubleVector(0.0, br)
            }

            val axisBrPoint = coord.toClient(mappedBrPoint)
            val axisBr = if (horizontal)
                axisBrPoint.x
            else
                axisBrPoint.y

            axisBreaks.add(axisBr)
            if (!axisBr.isFinite()) {
                throw IllegalStateException(
                    "Illegal axis '" + scale.name + "' break position " + axisBr +
                            " at index " + (axisBreaks.size - 1) +
                            "\nsource breaks    : " + scaleBreaks.domainValues +
                            "\ntranslated breaks: " + breaksMapped +
                            "\naxis breaks      : " + axisBreaks
                )
            }
        }
        return axisBreaks
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy