import io.data2viz.color.Colors
import io.data2viz.geom.Point
import io.data2viz.geom.Size
import io.data2viz.hexbin.Bin
import io.data2viz.hexbin.hexbinGenerator
import io.data2viz.random.RandomDistribution
import io.data2viz.scale.ScalesChromatic
import io.data2viz.viz.PathNode
import io.data2viz.viz.*
const val POINT_COUNT = 2000
const val defaultSize = 720.0
val generator = RandomDistribution.normal(defaultSize/2, 80.0)
val allX = (1..POINT_COUNT).map { generator() }
val allY = (1..POINT_COUNT).map { generator() }
val points = allX.zip(allY).map { Point(it.first, it.second) }
val hexbin = hexbinGenerator {
width = defaultSize
height = defaultSize
radius = 15.0
}
val bins by lazy { hexbin(points) }
val scale = ScalesChromatic.Continuous.linearLAB {
domain = listOf(0.0, bins.map { it.points.count().toDouble() }.max()!!)
range = listOf(Colors.Web.white, Colors.rgb(0x0141F79))
}
fun buildViz(): Viz {
val bins: MutableList<Bin> = hexbin(points)
val paths = bins.map { bin ->
val path = PathNode()
hexbin.hexagon(path, Point(bin.x, bin.y))
path.fill = scale(bin.points.size)
path.stroke = Colors.Web.white
path.strokeWidth = 1.0
path
}
val viz = viz {
size = Size(defaultSize, defaultSize)
}
paths.forEach {
viz.add(it)
}
return viz
}
fun main() {
buildViz().bindRendererOnNewCanvas()
}
comments