Skip to content

Commit

Permalink
first todo with piechart completed
Browse files Browse the repository at this point in the history
  • Loading branch information
halotukozak committed Jan 17, 2024
1 parent 33913c4 commit dce4541
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/backend/Simulation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Simulation(
fun slower() = _dayDuration.updateAndGet { it + 100 }


override fun close() {
override fun close() { ///todo make it working
launchMainImmediate { simulationJob.cancelAndJoin() }
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/backend/statistics/StatisticsService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import backend.map.Vector
import backend.model.Animal
import backend.model.Gen
import backend.model.Genome
import javafx.scene.chart.PieChart
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import metrics.Day
import metrics.collector.Collector
import metrics.collector.MutableCollector
Expand Down Expand Up @@ -90,6 +92,14 @@ class StatisticsService(simulationConfig: Config) {
private val _genCollector by lazy { MutableCollector<Gen>(range) }
val genCollector: Collector<Gen> by lazy { _genCollector }

val presentGens by lazy {
genCollector.map {
it.toList().lastOrNull()?.second?.sortedBy { it.first }?.map { (gen, count) ->
PieChart.Data(gen.name, count.toDouble())
}
}
}

val isGenomeCollectorEnabled = simulationConfig.genomes
private val _genomeCollector by lazy { MutableCollector<Genome>(range) }
val genomeCollector: Collector<Genome> by lazy { _genomeCollector }
Expand Down
7 changes: 2 additions & 5 deletions src/main/kotlin/frontend/statistics/StatisticsView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import frontend.components.View
import frontend.components.card
import frontend.components.fontIcon
import javafx.scene.chart.NumberAxis
import javafx.scene.chart.PieChart
import javafx.scene.chart.XYChart
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
Expand Down Expand Up @@ -116,10 +115,8 @@ class StatisticsView(
vbox {
piechart("Present") {
animated = false
genCollector.onUpdate {
data.setAll(it.toList().lastOrNull()?.second?.sortedBy { it.first }?.map { (gen, count) ->
PieChart.Data(gen.name, count.toDouble()) //todo to view model
})
presentGens.onUpdate {
data.setAll(it)
}
}
linechart("Gens", NumberAxis(), NumberAxis()) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/shared/FlowUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ inline fun <T, U, V> Flow<Pair<T, U>>.mapValues(crossinline f: suspend (T, U) ->
inline fun <T, U, V> Flow<Pair<T, U>>.mapValues(crossinline f: suspend (U) -> V): Flow<Pair<T, V>> =
map { (t, u) -> t to f(u) }

fun <K, V> Flow<Pair<K, V>>.group(): Flow<Pair<K, List<V>>> = flow {//todo idk if it works
fun <K, V> Flow<Pair<K, V>>.group(): Flow<Pair<K, List<V>>> = flow {
val storage = mutableMapOf<K, MutableList<V>>()
collect { t -> storage.getOrPut(t.first) { mutableListOf() } += t.second }
storage.forEach { (k, ts) -> emit(k to ts) }
}

val Flow<Boolean>.not
get() = map { !it }
get() = map { !it }

0 comments on commit dce4541

Please sign in to comment.