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

tornadofx.Charts.kt Maven / Gradle / Ivy

There is a newer version: 1.7.7
Show newest version
package tornadofx

import javafx.collections.ObservableList
import javafx.event.EventTarget
import javafx.scene.chart.*
import javafx.scene.layout.Pane

/**
 * Create a PieChart with optional title data and add to the parent pane. The optional op will be performed on the new instance.
 */
fun EventTarget.piechart(title: String? = null, data: ObservableList? = null, op: (PieChart.() -> Unit)? = null): PieChart {
    val chart = if (data != null) PieChart(data) else PieChart()
    chart.title = title
    return opcr(this, chart, op)
}

/**
 * Add and create a PieChart.Data entry. The optional op will be performed on the data instance,
 * a good place to add event handlers to the PieChart.Data.node for example.
 *
 * @return The new Data entry
 */
fun PieChart.data(name: String, value: Double, op: (PieChart.Data.() -> Unit)? = null) = PieChart.Data(name, value).apply {
    data.add(this)
    if (op != null) op(this)
}

/**
 * Add and create multiple PieChart.Data entries from the given map.
 */
fun PieChart.data(value: Map) = value.forEach { data(it.key, it.value) }


/**
 * Create a LineChart with optional title, axis and add to the parent pane. The optional op will be performed on the new instance.
 */
fun  EventTarget.linechart(title: String? = null, x: Axis, y: Axis, op: (LineChart.() -> Unit)? = null): LineChart {
    val chart = LineChart(x, y)
    chart.title = title
    return opcr(this, chart, op)
}

/**
 * Create an AreaChart with optional title, axis and add to the parent pane. The optional op will be performed on the new instance.
 */
fun  EventTarget.areachart(title: String? = null, x: Axis, y: Axis, op: (AreaChart.() -> Unit)? = null): AreaChart {
    val chart = AreaChart(x, y)
    chart.title = title
    return opcr(this, chart, op)
}

/**
 * Create a BubbleChart with optional title, axis and add to the parent pane. The optional op will be performed on the new instance.
 */
fun  EventTarget.bubblechart(title: String? = null, x: Axis, y: Axis, op: (BubbleChart.() -> Unit)? = null): BubbleChart {
    val chart = BubbleChart(x, y)
    chart.title = title
    return opcr(this, chart, op)
}

/**
 * Create a ScatterChart with optional title, axis and add to the parent pane. The optional op will be performed on the new instance.
 */
fun  EventTarget.scatterchart(title: String? = null, x: Axis, y: Axis, op: (ScatterChart.() -> Unit)? = null): ScatterChart {
    val chart = ScatterChart(x, y)
    chart.title = title
    return opcr(this, chart, op)
}

/**
 * Create a BarChart with optional title, axis and add to the parent pane. The optional op will be performed on the new instance.
 */
fun  EventTarget.barchart(title: String? = null, x: Axis, y: Axis, op: (BarChart.() -> Unit)? = null): BarChart {
    val chart = BarChart(x, y)
    chart.title = title
    return opcr(this, chart, op)
}

/**
 * Create a BarChart with optional title, axis and add to the parent pane. The optional op will be performed on the new instance.
 */
fun  EventTarget.stackedbarchart(title: String? = null, x: Axis, y: Axis, op: (StackedBarChart.() -> Unit)? = null): StackedBarChart {
    val chart = StackedBarChart(x, y)
    chart.title = title
    return opcr(this, chart, op)
}

/**
 * Add a new XYChart.Series with the given name to the given Chart. Optionally specify a list data for the new series or
 * add data with the optional op that will be performed on the created series object.
 */
fun > ChartType.series(name: String, elements: ObservableList>? = null, op: ((XYChart.Series).() -> Unit)? = null): XYChart.Series {
    val series = XYChart.Series()
    series.name = name
    if (elements != null) data.add(series)
    if (op != null) op(series)
    data.add(series)
    return series
}

/**
 * Add and create a XYChart.Data entry with x, y and optional extra value. The optional op will be performed on the data instance,
 * a good place to add event handlers to the Data.node for example.
 *
 * @return The new Data entry
 */
fun  XYChart.Series.data(x: X, y: Y, extra: Any? = null, op: ((XYChart.Data).() -> Unit)? = null) = XYChart.Data(x, y).apply {
    if (extra != null) extraValue = extra
    data.add(this)
    if (op != null) op(this)
}

/**
 * Helper class for the multiseries support
 */
class MultiSeries(val series: List>, val chart: XYChart) {
    fun data(x: X, vararg y: Y) {
        for ((index, value) in y.withIndex())
            series[index].data(x, value)
    }
}

/**
 * Add multiple series XYChart.Series with data in one go. Specify a list of names for the series
 * and then add values in the op. Example:
 * 
 * multiseries("Portfolio 1", "Portfolio 2") {
 *   data(1, 23, 10)
 *   data(2, 14, 5)
 *   data(3, 15, 8)
 *   ...
 * }
 * 
*/ fun > ChartType.multiseries(vararg names: String, op: ((MultiSeries).() -> Unit)? = null): MultiSeries { val series = names.map { XYChart.Series().apply { name = it } } val multiseries = MultiSeries(series, this) op?.invoke(multiseries) data.addAll(series) return multiseries }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy