Daily CO2 measurement at Mauna Loa Observatory, Hawaii
import io.data2viz.charts.*
import io.data2viz.charts.core.Padding
import io.data2viz.charts.core.*
import io.data2viz.charts.dimension.*
import io.data2viz.charts.chart.*
import io.data2viz.charts.chart.mark.*
import io.data2viz.charts.viz.*
import io.data2viz.charts.layout.*
import io.data2viz.charts.config.configs.*
import io.data2viz.math.*
import io.data2viz.color.*
import io.data2viz.geom.*
import io.data2viz.shape.Symbols
import io.data2viz.dsv.Dsv
import io.data2viz.timeFormat.*
import org.w3c.fetch.Response
import kotlinx.browser.window
import kotlin.js.Promise
import kotlinx.datetime.Instant
val width = 600.0
val height = 400.0
data class CO2Reading(
val date: Instant,
val reading: Double
)
private fun parseReading(row: List<String>) = CO2Reading(
Instant.parse(row[0]+"-"+row[1]+"-"+row[2]+"T00:00:00.000Z"),
row[3].toDouble()
)
fun main() {
// Creating and sizing the VizContainer
val vc = newVizContainer().apply {
size = Size(width, height)
}
val request: Promise<Response> =
window.fetch("https://docs.google.com/spreadsheets/d/e/2PACX-1vTdpJaf1wH8NfYMsYlS62KqRUvzZlB1Aan9D5JbZbUM4XAGX18UtchpK1gT4E5XMHy1XFHmQWWDIT3J/pub?gid=1402920869&single=true&output=csv")
request.then {
it.text().then {
val temperatures = Dsv()
.parseRows(it)
.drop(1)
.map { parseReading(it) }
.filter { it.reading > .0 }
vc.chart(temperatures, lightConfig) {
title = "Daily CO2 measurement at Mauna Loa Observatory, Hawaii"
config {
events {
zoomMode = ZoomMode.X
panMode = PanMode.X
triggerMode = TriggerMode.Column
}
zoom {
maxXZoomRatio = 1000.0
}
}
val dateDim = temporal( { domain.date } )
val co2Dim = quantitative( { domain.reading } ) {
name = "CO2 daily baseline average"
formatter = { "$this ppm" }
}
line(dateDim, co2Dim) {
x {
// start = Instant.parse("2000-01-01T00:00:00.000Z")
// end = Instant.parse("2020-01-01T00:00:00.000Z")
}
}
}
}
}
}
comments