Permalink
Browse files

Enable legend, controlled by checkbox

  • Loading branch information...
Joseph Shraibman
Joseph Shraibman committed May 2, 2012
1 parent c72398a commit 123df79b79e03801c3845cbf542bb3d3a0f1f95a
Showing with 89 additions and 14 deletions.
  1. +89 −14 src/main/scala/code/comet/JvmMemActor.scala
@@ -3,13 +3,13 @@ package code.comet
import code.lib.SimpFactory
import xml.{Text, NodeSeq}
-import net.liftweb.common.{Logger, Full}
import net.liftweb.http.{CometActor, S, DispatchSnippet, SHtml}
import net.liftweb.util.{Schedule, Helpers}
import _root_.net.liftweb.util.Helpers._
import net.liftweb.http.js.JsCmds.{Replace, SetHtml, Noop}
import net.liftweb.http.js.JsExp
+import net.liftweb.common.{Box, Logger, Full}
/**
* Created with IntelliJ IDEA.
@@ -32,7 +32,13 @@ class JvmMemActor extends CometActor with Logger{
def setHtml(text: String) = partialUpdate(SetHtml(uuid, <div>{ text }</div>))
}
- val memops = List("freemem" , "usedmem", "totalmem", "maxmem") //, "memdate")
+ class MyFlotSerie(val lbl: String, val _color: String) extends net.liftweb.widgets.flot.FlotSerie{
+ override def label: Box[String] = Full(lbl)
+ override def color: Box[Either[String, Int]] = Full(Left(_color) )
+ }
+
+ val memops = List("freemem" , "usedmem", "totalmem", "maxmem")
+ val lineOptions = List( "green", "yellow", "blue", "black")
class FreeTotalMax(){
val date = new java.util.Date
@@ -112,42 +118,68 @@ class JvmMemActor extends CometActor with Logger{
val getSpan = (".flotchart" #> ((chartDiv: scala.xml.NodeSeq) { chartDiv.asInstanceOf[scala.xml.Elem] % new scala.xml.UnprefixedAttribute("id", uuid, scala.xml.Null) })).apply(html3)
val memspan = new UpdateableSpan(uuid) {
- private def getDataPoint(op: String, ftm: FreeTotalMax): (Double, Double) =
+ private def getDataPoint(op: String, ftm: FreeTotalMax): (Double, Double) = {
(ftm.date.getTime,
op match{
case "freemem" => ftm.free
case "usedmem" => ftm.used
case "totalmem" => ftm.total
case "maxmem" => ftm.max
})
+ }
var data_lines: List[FlotSerie] = null //set in handleFirstDataPoint()
var pointsDone = 0
+ var show_toggle: Array[Boolean] = null //corresponds to each FlotSerie
+
private def handleFirstDataPoint(ftm: FreeTotalMax) = { //on first datapoint render the chart
- data_lines = memops.map( op => new FlotSerie(){
+ data_lines = memops.zip(lineOptions).map( {case (op, color) =>
+ new MyFlotSerie(op, color){
override val data = List( getDataPoint(op, ftm) )
}
- )
- val flot_widget_rendered = Flot.render(uuid, data_lines, new FlotOptions {
+ })
+ show_toggle = memops.map( _ => true).toArray
+
+ //get series to be rendered, based on if their boolean is set in show_toggle
+ def serieToRender = show_toggle.toList.zip(data_lines).filter{ case (b, _) => b }.map{ case (_, s) => s }
+
+ class MyFlotOptions extends FlotOptions {
override def xaxis = Full(
new FlotAxisOptions{
override def mode = Full("time")
})
- }, Flot.script(flot_span))
+ }
+
+ val flot_widget_rendered = Flot.render(uuid, data_lines, new MyFlotOptions, Flot.script(flot_span))
partialUpdate(SetHtml(uuid, flot_widget_rendered ))
//now create the checkboxes. Can't be created until data_lines is created
checkBoxSpanId match{
case None =>
case Some(cbx_id) =>
- partialUpdate(SetHtml(cbx_id, joinNodeSeqs( memops.zipWithIndex.map{case (name, idx) =>
+ partialUpdate(SetHtml(cbx_id, joinNodeSeqs(
+ List(<label> {SHtml.ajaxCheckbox (true, { (b: Boolean) =>
+ val newOptions = new MyFlotOptions{ override def legend = Full(new FlotLegendOptions{ override def show = Full(b)})}
+ //Doesn't work on its own. The options passed into renderFlotShow() are not actually used in its code
+// Flot.renderFlotShow ( uuid, serieToRender, newOptions, Noop)
+
+ net.liftweb.http.js.JsCmds.JsCrVar("options_"+uuid, newOptions.asJsObj) &
+ Flot.renderFlotShow ( uuid, null, null, Noop)
+ } ) }Legend </label> ) ++
+ memops.zipWithIndex.map{case (name, idx) =>
<label> {
SHtml.ajaxCheckbox (true, { (b: Boolean) =>
- JsFlotSetOps(uuid, data_lines,
- { val array = Array.fill[List[ (String, JsExp)]](memops.size)(Nil)
- array(idx) = List(("lines.lineWidth", if(b){ 2 }else{ 0 }))
- array.toList
- }) } ) }{ name} </label> }) ))
+// JsFlotSetOps(uuid, data_lines,
+// { val array = Array.fill[List[ (String, JsExp)]](memops.size)(Nil)
+// array(idx) = List(("lines.lineWidth", if(b){ 2 }else{ 0 }))
+// array.toList
+// })
+ show_toggle(idx) = b
+ //now rerender widget
+ net.liftweb.http.js.JsCmds.JsCrVar("datas_"+uuid, Flot.renderSeries(serieToRender, uuid)) &
+ Flot.renderFlotShow ( uuid, serieToRender,
+ new FlotOptions{}, Noop)
+ } ) }{ name} </label> }) ))
}//match
} //handleFirstDataPoint(
def doUpdate(ftm: FreeTotalMax) = {
@@ -226,4 +258,47 @@ import net.liftweb.http.js.JsCmds._
}
override def autoIncludeJsonCode = true
}
- // case object UpdateInfo
+ // case object UpdateInfo
+
+/*
+
+try { destroy_F81793020218TX5GBD(); } catch (e) {}
+
+jQuery('#'+"flotspan5").html("\u000a <script src=\"/classpath/flot/jquery.flot.js\" type=\"text/javascript\"></script><script src=\"/classpath/flot/jquery.flot.navigate.js\" type=\"text/javascript\"></script>\u000a <!--[if IE\u005d><script language=\"javascript\" type=\"text/javascript\" src=\"/classpath/flot/excanvas.js\"></script><![endif\u005d-->\u000a <link type=\"text/css\" rel=\"stylesheet\" href=\"/classpath/flot/jquery.flot.css\" />\u000a ");
+// <![CDATA[
+var data_flotspan5_1 = [[1.335922658888E12, 1.31983E8]
+]
+;
+var data_flotspan5_2 = [[1.335922658888E12, 2.1502312E7]
+]
+;
+var data_flotspan5_3 = [[1.335922658888E12, 1.53485312E8]
+]
+;
+var data_flotspan5_4 = [[1.335922658888E12, 1.854865408E9]
+]
+;
+var datas_flotspan5 = [{"data": data_flotspan5_1}, {"data": data_flotspan5_2}, {"data": data_flotspan5_3}, {"data": data_flotspan5_4}]
+;
+var options_flotspan5 = {"xaxis": {"mode": "time"}};
+function flot_plot_flotspan5() {
+ jQuery('#'+"flotspan5").addClass("flot_lww");
+jQuery('#'+"flotspan5").show();
+
+var plot_flotspan5 = jQuery.plot(jQuery("#flotspan5"), datas_flotspan5, options_flotspan5);
+
+
+ }
+
+jQuery(document).ready(function() {flot_plot_flotspan5();;});
+// ]]>
+;;
+jQuery('#'+"cbx6").html("<label> <input checked=\"checked\" type=\"checkbox\" onclick=\"liftAjax.lift_ajaxHandler(\u0027F81793020228Z0VYQ1=\u0027 + this.checked, null, null, null)\" />freemem </label><label> <input checked=\"checked\" type=\"checkbox\" onclick=\"liftAjax.lift_ajaxHandler(\u0027F81793020230V42XAL=\u0027 + this.checked, null, null, null)\" />usedmem </label><label> <input checked=\"checked\" type=\"checkbox\" onclick=\"liftAjax.lift_ajaxHandler(\u0027F81793020232LMFHW2=\u0027 + this.checked, null, null, null)\" />totalmem </label><label> <input checked=\"checked\" type=\"checkbox\" onclick=\"liftAjax.lift_ajaxHandler(\u0027F817930202340LATDB=\u0027 + this.checked, null, null, null)\" />maxmem </label>");
+jQuery('#'+"s4").html("<div>1,854,865,408</div>");
+jQuery('#'+"s3").html("<div>153,485,312</div>");
+jQuery('#'+"s2").html("<div>21,921,016</div>");
+jQuery('#'+"s1").html("<div>131,564,296</div>");
+jQuery('#'+"s0").html("<div>Tue\u00a0May\u00a001\u00a021:37:38</div>");
+try { destroy_F81793020218TX5GBD = function() {}; } catch (e) {}
+if (lift_toWatch['F81793020218TX5GBD'] !== undefined) lift_toWatch['F81793020218TX5GBD'] = '81793020241';
+*/

0 comments on commit 123df79

Please sign in to comment.