liftmodules / widgets

A collection of useful widgets for use with Lift
12 stars 18 forks source link

Change FlotSerie to enable additional fields such as yaxis #9

Open fmpwizard opened 11 years ago

fmpwizard commented 11 years ago

FlotSerie does not provide for putting data on second y axis. Proposed FlotSerie:

trait FlotSerie extends BaseFlotOptions
{
  def data: List[(Double, Double)] = Nil
  def label: Box[String] = Empty
  def lines: Box[FlotLinesOptions] = Empty
  def points: Box[FlotPointsOptions] = Empty
  def bars: Box[FlotBarsOptions] = Empty
  def color: Box[Either[String, Int]] = Empty
  def shadowSize: Box[Int] = Empty

  def buildOptions = {
    List(label.map(v => ("label", Str(v))),
         lines.map(v => ("lines", v.asJsObj)),
         points.map(v => ("points", v.asJsObj)),
         bars.map(v => ("bars", v.asJsObj)),
         color.map {
           case Left(c) => ("color", Str(c))
           case Right(c) => ("color", Num(c))
         },
         shadowSize.map(s => ("shadowSize", Num(s)))
      )
  }
}

Then using it in Flot:

  def renderOneSerie(data: FlotSerie, idPlaceholder: String, idSerie: Int): JsObj = {
    val info: List[Box[(String, JsExp)]] =
      Full(("data", JsVar("data_"+idPlaceholder + "_" + idSerie))) ::
      data.buildOptions
    JsObj(info.flatten(_.toList) :_*)
  }

Enables user code such as:

      series = new FlotSerie () {
        override val label = Full(theDs.split("@")(0))
        override val data = theDataMap(theDs)
        val yaxis = Full (2)
        override def buildOptions = {
          yaxis.map( v => ("yaxis", Num(v))) ::
          super.buildOptions
        }
      } :: series

Migrated from https://github.com/lift/framework/issues/559