/
Viz.scala
58 lines (42 loc) · 1.73 KB
/
Viz.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.dallaway.snippet
import scala.xml.NodeSeq
import net.liftweb.util.Helpers._
import net.liftweb.http.js.JsCmds._
import net.liftweb.http.js._
import net.liftweb.widgets.flot._
import net.liftweb.common._
import JE._
class Viz {
val data = List(50,80) // range 0-100
val bar_labels = List("Fred", "Bert")
// -- GOOGLE CHARTS EXAMPLE: ---------------------------------------------
val width = 300
val height = 225
def googleUrl = "http://chart.apis.google.com/chart?" + List(
"chxt=x,y",
"chxl=0:|" + bar_labels.mkString("|"),
"chs=%dx%d".format(width,height),
"cht=bvg",
"chco=A2C180",
"chd=t:"+data.mkString(",") ).mkString("&")
def google(xhtml: NodeSeq) = <img src={googleUrl} width={width.toString} height={height.toString} alt="graph"/>
// -- FLOT EXAMPLE: -----------------------------------------------------
def flot(xhtml: NodeSeq) = {
// One FlotSerie for each bar
val data_to_plot = for ( (y,x) <- data zipWithIndex ) yield new FlotSerie() {
override val data : List[(Double,Double)] = (x.toDouble, y.toDouble) :: Nil
override val label = Full( bar_labels(x) )
}
val options:FlotOptions = new FlotOptions () {
override val series = Full( Map( "bars" -> JsObj("show"->true, "barWidth"->0.5) ) )
override val xaxis = Full( new FlotAxisOptions() {
override def min = Some(0d)
override def max = Some(data.length * 1d)
})
override def grid = Full( new FlotGridOptions() {
override def hoverable = Full(true)
})
}
Flot.render("graph_area", data_to_plot, options, Flot.script(xhtml))
}
}