io.docops.asciidoc.charts.StackedBar.kt Maven / Gradle / Ivy
package io.docops.asciidoc.charts
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
@ChartDsl
class StackedBar {
var width: Int = 800
var height: Int = 400
private val divId = "div_${System.currentTimeMillis()}"
var showLabel = true
var title = ""
var subTitle = ""
var yAxisLabel = ""
var xAxisLabel = ""
private val legend = mutableListOf()
private val xAxisData = mutableListOf()
private val seriesData = mutableListOf()
private var barChartData: MutableList = mutableListOf()
var series = mutableListOf()
var dimension = mutableListOf()
fun s(series: Series.() -> Unit) {
val ser = Series().apply(series)
this.series.add(ser)
}
fun validate(): StackedBar {
return this
}
fun dimensionJson() : String {
return Json.encodeToString(dimension)
}
private fun seriesJson(): String {
val sb = StringBuilder("[")
series.forEachIndexed { idx, value ->
sb.append("${value.tpl(idx)},")
}
sb.append("]")
return sb.toString()
}
fun toEChart() : String {
//language=html
val tpl = """
""".trimIndent()
return tpl
}
}
@ChartDsl
class Series
{
var name = ""
var isStack = true
var stackGroupName:String = ""
var data = mutableListOf()
fun dataJson() : String {
return Json.encodeToString(data)
}
fun tpl(idx: Int): String {
//language=json
return """
{
"name": "$name",
"type": "bar",
"stack": "$stackGroupName",
"emphasis": {
"itemStyle": {
"shadowBlur": 10,
"shadowColor": "rgba(112,128,144, 0.3)"
}
},
"data": ${dataJson()}
}
""".trimIndent()
}
}
fun stackBar(stackedBar: StackedBar.()-> Unit): StackedBar {
return StackedBar().apply(stackedBar).validate()
}