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

io.docops.asciidoc.stackbar.StackedBarMaker.kt Maven / Gradle / Ivy

There is a newer version: 2023.20
Show newest version
/*
 * Copyright 2020 The DocOps Consortium
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.docops.asciidoc.stackbar

import io.docops.asciidoc.stackbar.model.StackModel
import io.docops.asciidoc.utils.addLinebreaks
import kotlin.math.roundToInt

class StackedBarMaker(val pdf: Boolean = false) {
    private var colorMap = listOf(
        "#093145", "#107896", "#829356", "#0C374D", "#1287A8",
        "#93A661", "#C2571A", "#9A2617", "#D3B53D", "#DA621E", "#AD2A1A",
        "#EBC944", "#F26D21", "#C02F1D", "#0D3D56", "#1496BB", "#A3B86C", "#3C6478",
        "#43ABC9", "#B5C689", "#BCA136", "#EFD469", "#F58B4C", "#CD594A"
    )

    var colors = mutableListOf()
    var colorRange = mapOf(
        1 to listOf("#deaddf","#e8e08e","#e7d2ae","#97dfd2","#9efdc1","#f3e76f","#a7cbda","#ef8ce3","#6ed7ed","#d9b8a2","#e9ce86","#dac588","#fae587","#56f1e2","#8fbeef","#d8b5e3","#cba2f8","#cf7bf3","#ead471","#d3e58f"),
        2 to listOf("#b9deae","#c8bbe5","#8baeea","#65cef4","#d7f640","#ddfc65","#ece2a7","#f1a9b2","#abc2f0","#ed9ecb","#f588d0","#f1d692","#ec8af0","#fc64f3","#7ecbe6","#78e9c9","#61f3fc","#a197e7","#a9dcbd","#a7ef7d"),
        3 to listOf("#ecc4b9","#e2c5c3","#e69cf0","#eab2bc","#b7b1e0","#cff24e","#cfe8c1","#8cd6f4","#e9aecb","#a7d3be","#ed8bc9","#69d5eb","#f3a0c4","#f4c99f","#96b6fd","#f356ec","#aec0ec","#aac7e5","#9291fa","#aff8b5"),
        4 to listOf("#efd17b","#7fd4d7","#dd9ad6","#fbdb80","#e2d1ad","#e0e878","#e7b2c5","#e579cb","#e9b392","#e1b0ce","#c5e885","#48d6f6","#e0b4e3","#bcfc7a","#db78dc","#b6f3c3","#d397c7","#f8a9aa","#72a7fb","#81cdd8"),
    )


    fun makeStackedBar(stackModels: List, title: String, darkMode: Boolean = false): String {
        val i = (1..4).random()
        colors = colorRange[i]!!.toMutableList()
        val barWidth = 40
        val sb = StringBuilder()
        val width = 800
        sb.append(head(height = 460, width = width))
        sb.append(defs())
        sb.append(style(darkMode))
        if (!pdf) {
            sb.append(script())
        }
        val norm = normalize(stackModels)
        var totalHeight = 20.0
        val x = width * 0.10
        var mode = "#eeeeee"
        if(darkMode) {
            mode = "#444444"
        }
        var fill = "#000000"
        if(darkMode) {
            fill = "#ffffff"
        }
        // language=svg
        sb.append(
            """
               
               $title
            """.trimIndent()
        )
        stackModels.forEachIndexed { index, stackModel ->
            val height = 400 * norm[index]

            val nextHalf = height / 2 + totalHeight
            // language=svg
            sb.append("""""")
            // language=svg
            sb.append("""${(norm[index] * 100).roundToInt()}%""")
            // language=svg
            sb.append("""""")
            val lines = addLinebreaks(stackModel.description, 50)
            // language=svg
            sb.append(
                """
                """
            )
            var start = 3
            if (lines.size > 1) {
                start = -2
            }
            lines.forEachIndexed { i, line ->
                if (i > 0) {
                    start = 12
                }
                // language=svg
                sb.append("""${line}""")
            }
            sb.append("")

            if (!pdf) {
                val desc = addLinebreaks(stackModel.fullDescription, 50)
                // language=svg
                sb.append("""""")

                desc.forEachIndexed { i, d ->
                    start = if (i == 0) {
                        0
                    } else {
                        12
                    }
                    // language=svg
                    sb.append("""${d}""")
                }
                sb.append("""""")
            }
            totalHeight += height
        }
        sb.append(end())
        return sb.toString()
    }

    private fun normalize(stackModels: List): MutableList {
        val values = mutableListOf()
        val sum = stackModels.sumOf { it.value }
        stackModels.forEach {
            values.add(it.value / sum)
        }
        return values
    }

    fun toLine(filename: String, list: List): MutableList {
        val norm = normalize(list)
        val lines = mutableListOf()

        lines.add(".$filename")
        // language=Asciidoc
        lines.add("""[cols="15%,15%,20%, 50%", options=header]""")
        // language=Asciidoc
        lines.add("|===")
        // language=Asciidoc
        lines.add("|Percent |Value |Label |Description")
        list.forEachIndexed { index, stackModel ->
            lines.add("|${(norm[index] * 100).roundToInt()} % |${stackModel.value} |${stackModel.description} |${stackModel.fullDescription}")
        }
        lines.add("|===")
        return lines
    }
    private fun head(height: Int = 460, width: Int = 970): String {
        // language=svg
        return """
            
            
        """.trimIndent()
    }

    private fun end(): String {
        return ""
    }

    private fun style(darkMode: Boolean): String {
        var fill = "#000000"
        if(darkMode) {
            fill = "#ffffff"
        }
        // language=html
        return """
    
        """.trimIndent()
    }

    private fun defs(): String {
        // language=svg
        return """
    
        
            
        
        
            
        
    
        """.trimIndent()
    }

    private fun script(): String {
        // language=html
        return """
    
        """.trimIndent()
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy