Created
February 13, 2018 18:33
-
-
Save hudsonb/0e9f7f7aa336225c0c30b2b0eae5c4ee to your computer and use it in GitHub Desktop.
Let's Make a Bar Chart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kubed.demo | |
import javafx.application.Application | |
import javafx.geometry.Insets | |
import javafx.scene.Group | |
import javafx.scene.Scene | |
import javafx.scene.paint.Color | |
import javafx.stage.Stage | |
import kubed.scale.scaleLinear | |
import kubed.selection.selectAll | |
import kubed.shape.rect | |
class BasicBarChartDemo : Application() { | |
override fun start(primaryStage: Stage?) { | |
val data = listOf(4.0, 8.0, 15.0, 16.0, 23.0, 42.0) | |
val margin = Insets(20.0, 10.0, 20.0, 10.0) | |
val outerWidth = 420.0 | |
val outerHeight = 500.0 | |
val innerWidth = outerWidth - margin.left - margin.right | |
val barHeight = 20.0 | |
val xScale = scaleLinear<Double> { | |
domain(0.0, data.max()!!.toDouble()) | |
range(0.0, innerWidth) | |
} | |
val rect = rect<Double> { | |
width { d, _ -> xScale(d) } | |
translateY { _, i -> (barHeight + 1) * i } | |
height(barHeight) | |
fill(Color.STEELBLUE) | |
} | |
val chart = Group() | |
chart.prefWidth(outerWidth) | |
chart.prefHeight(outerHeight) | |
chart.translateX = margin.left | |
chart.translateY = margin.top | |
chart.selectAll<Double>() | |
.data(data) | |
.enter() | |
.append { d, i, _ -> rect(d, i) } | |
val scene = Scene(chart) | |
primaryStage?.scene = scene | |
primaryStage?.show() | |
} | |
companion object { | |
@JvmStatic | |
fun main(vararg args: String) { | |
launch(BasicBarChartDemo::class.java, *args) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment