Laughing-Man-Studios / Ski-Resort-Dashboard

A small dashboard to display some statistics about the local ski resorts I visit
MIT License
0 stars 0 forks source link

Use lit-graph library to render the data into graphs #106

Open Rogibb111 opened 1 year ago

Rogibb111 commented 1 year ago

I need to pull in the lit-graph library from the CDN and use it with the rendered data to create graphs of the data I want to show.

Rogibb111 commented 1 year ago

So, the way that I was rendering the JSON for the different data types was not entirely correct. Essentially, I need to pull out all of the implicit writes from the DataPlot class and move them above so that they are in the scope of the rest of the file. This will allow the to Json.toJson call on the different ArrayBuffers of DataPlots to access the writes and generate valid JSON. The previous way I was doing it was generating JSON arrays of JSON encoded strings, which were not getting decoded when being assigned as attributes to the lit graph. The following is an example of this working correctly:

import play.api.libs.json.JsValue
import scala.collection.mutable.ArrayBuffer;
import play.api.libs.json.Json
import play.api.libs.json.Writes
import play.api.libs.json.JsValue
import play.api.libs.json.JsString
import play.api.libs.json.JsNumber
implicit val valWrites = new Writes[Either[String, Int]] {
  def writes(o: Either[String,Int]): JsValue = o.fold(
    l => JsString(l),
    r => JsNumber(r)
  )
}
implicit val writes = Json.writes[DataPlot]

final case class DataPlot(timestamp: String, value: Either[String, Int]) {

  def toJson(): String = {
    return Json.toJson(this).toString()
  }

}

class GraphData () {
  val dailySnow = ArrayBuffer[DataPlot]()

  def addPlotsFromSnapshot(): Unit = {
      dailySnow += new DataPlot("hey", Right(3))
  }
}
val graphData = new GraphData()
graphData.addPlotsFromSnapshot()

print(Json.toJson(graphData.dailySnow))