Skip to content

Commit

Permalink
Improve bar helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Mar 1, 2023
1 parent a7537f5 commit 9db5ab7
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions core/shared/src/main/scala/viz/vega/plots/Examples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -523,32 +523,37 @@ case class Aninteractivescatterplotofglobalhealthstatisticsbycountryandyear_Lite
using PlotTarget
) extends FromUrl(SpecUrl.Aninteractivescatterplotofglobalhealthstatisticsbycountryandyear_Lite)

object BarChart
extends viz.companions.AxisUtils:
object BarChart extends viz.companions.AxisUtils:
import upickle.default.*
import ujson.Obj
import ujson.Value

trait BarData
trait BarData {}

trait BarPlottable(val category: String, val amount: Double) extends BarData
trait MarkColour(val colour: String) extends BarData

val takeColourFromData : (ujson.Value => Unit) = spec => spec("marks")(0)("encode")("update")("fill") = ujson.Obj("field" -> "colour")
val takeColourFromData: (ujson.Value => Unit) = spec =>
spec("marks")(0)("encode")("update")("fill") = Obj("field" -> "colour")

//def replaceBarData[T <: BarData](d: Seq[BarData]) = writeJs(d)

given enc[JsValue, T <: BarData](using upickleDefault: ReadWriter[T]): ReadWriter[T & BarData] =
readwriter[ujson.Value].bimap[T & BarData](
in =>
var toMerge: scala.collection.mutable.ArraySeq[ujson.Obj] = scala.collection.mutable.ArraySeq()
val vanilla = upickle.default.writeJs(in)(upickleDefault).obj
var toMerge: scala.collection.mutable.ArraySeq[Obj] = scala.collection.mutable.ArraySeq()
val vanilla = writeJs(in)(upickleDefault).obj

in match
case colorful: MarkColor =>
toMerge :+= ujson.Obj("colour" -> colorful.color)
case colorful: MarkColour =>
toMerge :+= Obj("colour" -> colorful.colour)
case _ => ()

in match
case xyData: XYPlottable =>
toMerge :+= ujson.Obj("category" -> xyData.x, "amount" -> xyData.y)
case barData: BarPlottable =>
toMerge :+= Obj("category" -> barData.category, "amount" -> barData.amount)
case _ => ()
toMerge.fold[ujson.Obj](vanilla) { case (start, next) => start.value ++ next.value },
toMerge.fold[Obj](vanilla) { case (start, next) => start.value ++ next.value },
in => ??? //upickle.default.read[T & BarData](in)
)

Expand Down

0 comments on commit 9db5ab7

Please sign in to comment.