[Ovirt-devel] [PATCH server] Add tooltips, y-axis labels, and first animation transition to flexchart.

Steve Linabery slinabery at redhat.com
Wed Oct 29 20:39:23 UTC 2008


Also improve OO design with new classes and better organization thereof.
---
 src/app/controllers/graph_controller.rb            |    3 +-
 src/flexchart/README.txt                           |    2 +-
 src/flexchart/flexchart.mxml                       |   69 +++++++--
 src/flexchart/org/ovirt/ChartLoader.as             |   64 --------
 src/flexchart/org/ovirt/Constants.as               |   28 ++++
 src/flexchart/org/ovirt/DataSeries.as              |   42 -----
 src/flexchart/org/ovirt/DataSource.as              |   14 +-
 src/flexchart/org/ovirt/charts/BarChart.as         |  158 ++++++++++++++++++++
 src/flexchart/org/ovirt/charts/Chart.as            |   46 ++++++
 src/flexchart/org/ovirt/data/DataPoint.as          |   47 ++++++
 src/flexchart/org/ovirt/data/DataSeries.as         |   54 +++++++
 src/flexchart/org/ovirt/elements/SingleBar.as      |   67 ++++++++
 src/flexchart/org/ovirt/elements/TextLiberation.as |   46 ++++++
 src/flexchart/org/ovirt/elements/YAxisLabel.as     |   41 +++++
 14 files changed, 556 insertions(+), 125 deletions(-)
 delete mode 100644 src/flexchart/org/ovirt/ChartLoader.as
 create mode 100644 src/flexchart/org/ovirt/Constants.as
 delete mode 100644 src/flexchart/org/ovirt/DataSeries.as
 create mode 100644 src/flexchart/org/ovirt/charts/BarChart.as
 create mode 100644 src/flexchart/org/ovirt/charts/Chart.as
 create mode 100644 src/flexchart/org/ovirt/data/DataPoint.as
 create mode 100644 src/flexchart/org/ovirt/data/DataSeries.as
 create mode 100644 src/flexchart/org/ovirt/elements/SingleBar.as
 create mode 100644 src/flexchart/org/ovirt/elements/TextLiberation.as
 create mode 100644 src/flexchart/org/ovirt/elements/YAxisLabel.as

diff --git a/src/app/controllers/graph_controller.rb b/src/app/controllers/graph_controller.rb
index c1d27b5..0105ea6 100644
--- a/src/app/controllers/graph_controller.rb
+++ b/src/app/controllers/graph_controller.rb
@@ -15,7 +15,8 @@ class GraphController < ApplicationController
                    :values => graph_obj[:dataset][2][:values].last(40) }
     my_data = graph_data[:labels].zip(graph_data[:values])
     graph = { :vectors => my_data,
-              :max_value => graph_obj[:total_peak]
+              :max_value => graph_obj[:total_peak],
+              :description => params[:target]
             }
     render :json => graph
   end
diff --git a/src/flexchart/README.txt b/src/flexchart/README.txt
index 66eb183..3de7430 100644
--- a/src/flexchart/README.txt
+++ b/src/flexchart/README.txt
@@ -5,4 +5,4 @@ Once you have mxmlc on your system, run:
 
 mxmlc flexchart.mxml
 
-in this directory, and copy the resulting file flexchart.swf to /usr/share/ovirt-server/public on your appliance.
+in this directory, and copy the resulting file flexchart.swf to /usr/share/ovirt-server/public/swfs on your appliance.
diff --git a/src/flexchart/flexchart.mxml b/src/flexchart/flexchart.mxml
index 796329d..35fa9a6 100644
--- a/src/flexchart/flexchart.mxml
+++ b/src/flexchart/flexchart.mxml
@@ -1,20 +1,67 @@
 <?xml version="1.0"?>
-<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" applicationComplete="populate(flexChart)">
+<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" applicationComplete="myInit(); populate(mainChart);" styleName="plain" horizontalScrollPolicy="off" verticalScrollPolicy="off">
   <mx:Script>
     <![CDATA[
 
-      import mx.containers.Box;
-      import org.ovirt.*;
+    import mx.containers.VBox;
+    import mx.effects.Resize;
+    import org.ovirt.Constants;
+    import org.ovirt.charts.Chart;
+    import org.ovirt.charts.BarChart;
 
-      private function populate(chart:Box):void {
-        var chartLoader:ChartLoader = new ChartLoader(chart, parameters['flexchart_data']);
-        chartLoader.load();
-      }
+    private function populate(container:VBox):void {
+        var chart:Chart = new BarChart(container, parameters['flexchart_data']);
+        chart.load();
+    }
+
+    private var expandHistory:Resize = new Resize();
+    private var contractHistory:Resize = new Resize();
+
+    private var expandResources:Resize = new Resize();
+    private var contractResources:Resize = new Resize();
+
+    private function myInit():void {
+      mainChart.height = Constants.height;
+      mainChart.width = Constants.width;
+      hostsChart.width = Constants.width;
+
+      expandHistory.widthTo = Constants.width;
+      expandHistory.heightTo = Constants.height;
+      expandHistory.target = mainChart;
+
+      contractHistory.widthTo = Constants.width;
+      contractHistory.heightTo = Constants.height * .3333;
+      contractHistory.target = mainChart;
+
+      expandResources.widthTo = Constants.width;
+      expandResources.heightTo = Constants.height * .6666;
+      expandResources.target = hostsChart;
+      expandResources.duration = 500;
+
+      contractResources.widthTo = Constants.width;
+      contractResources.heightTo = 0;
+      contractResources.target = hostsChart;
+      contractResources.duration = 500;
+    }
+
+    private function zoomOutSeries(e:Event):void {
+      expandHistory.end(); expandHistory.play();
+      contractResources.end(); contractResources.play();
+      hostsChart.visible = false;
+    }
+
+    private function zoomIntoSeries(e:Event):void {
+      hostsChart.visible=true;
+      expandResources.end(); expandResources.play();
+      contractHistory.end(); contractHistory.play();
+    }
 
     ]]>
+
+
   </mx:Script>
-  <mx:Panel height="100%" width="100%" visible="true">
-    <mx:HBox id="flexChart"  height="100%" width="100%" visible="true" verticalAlign="bottom" opaqueBackground="0xFFFFFF" borderThickness="0">
-    </mx:HBox>
-  </mx:Panel>
+
+  <mx:VBox id="mainChart"  click="zoomIntoSeries(event)" />
+  <mx:VBox id="hostsChart" visible="false" opaqueBackground="0x00ff00" click="zoomOutSeries(event)"/>
+
 </mx:Application>
diff --git a/src/flexchart/org/ovirt/ChartLoader.as b/src/flexchart/org/ovirt/ChartLoader.as
deleted file mode 100644
index 4e493a4..0000000
--- a/src/flexchart/org/ovirt/ChartLoader.as
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- Copyright (C) 2008 Red Hat, Inc.
- Written by Steve Linabery <slinabery at redhat.com>
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; version 2 of the License.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- MA  02110-1301, USA.  A copy of the GNU General Public License is
- also available at http://www.gnu.org/copyleft/gpl.html.
-*/
-
-package org.ovirt {
-
-  import mx.containers.Box;
-  import mx.containers.HBox;
-  import mx.controls.Text;
-
-  public class ChartLoader {
-
-    private var element:Box;
-    private var datasourceUrl:String;
-
-    public function ChartLoader(element:Box, datasourceUrl:String) {
-      this.element = element;
-      this.datasourceUrl = datasourceUrl;
-    }
-
-    public function addData(dataSeries:DataSeries):void {
-      var points:Array = dataSeries.getPoints();
-      var maxValue:Number = dataSeries.getMaxValue();
-      var scale:Number = maxValue;
-      if (scale == 0) { scale = 1; }
-      var size:int = points.length;
-      element.removeAllChildren();
-      element.setStyle("horizontalGap","2");
-      for (var i:int = 0; i < size; i++) {
-        var value:Number = (points[i] as Array)[1];
-        var bar:HBox = new HBox();
-        bar.percentHeight = ((value / scale) * 90);
-        bar.percentWidth = (100 / size);
-        bar.setStyle("backgroundColor","0x0000FF");
-        bar.setStyle("left","1");
-        bar.setStyle("right","1");
-        bar.visible = true;
-        bar.setVisible(true);
-        element.addChild(bar);
-      }
-    }
-
-    public function load():void {
-      var dataSource:DataSource = new DataSource(this);
-      dataSource.retrieveData(datasourceUrl);
-    }
-  }
-}
\ No newline at end of file
diff --git a/src/flexchart/org/ovirt/Constants.as b/src/flexchart/org/ovirt/Constants.as
new file mode 100644
index 0000000..996a31e
--- /dev/null
+++ b/src/flexchart/org/ovirt/Constants.as
@@ -0,0 +1,28 @@
+/*
+ Copyright (C) 2008 Red Hat, Inc.
+ Written by Steve Linabery <slinabery at redhat.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ MA  02110-1301, USA.  A copy of the GNU General Public License is
+ also available at http://www.gnu.org/copyleft/gpl.html.
+*/
+
+package org.ovirt {
+  public class Constants {
+    public static var width:int = 722;
+    public static var height:int = 297;
+    public static var barSpacing:int = 2;
+    public static var labelHeight:int = 40;
+  }
+}
diff --git a/src/flexchart/org/ovirt/DataSeries.as b/src/flexchart/org/ovirt/DataSeries.as
deleted file mode 100644
index d63162a..0000000
--- a/src/flexchart/org/ovirt/DataSeries.as
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- Copyright (C) 2008 Red Hat, Inc.
- Written by Steve Linabery <slinabery at redhat.com>
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; version 2 of the License.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- MA  02110-1301, USA.  A copy of the GNU General Public License is
- also available at http://www.gnu.org/copyleft/gpl.html.
-*/
-
-//class to encapsulate the json object representation of a data
-//series returned from stats package
-
-package org.ovirt {
-
-  public class DataSeries {
-
-    private var object:Object;
-
-    public function DataSeries (object:Object) {
-      this.object = object;
-    }
-
-    public function getPoints():Array {
-      return object["vectors"] as Array;
-    }
-
-    public function getMaxValue():Number {
-      return object["max_value"] as Number;
-    }
-  }
-}
\ No newline at end of file
diff --git a/src/flexchart/org/ovirt/DataSource.as b/src/flexchart/org/ovirt/DataSource.as
index 1a64f03..44e482d 100644
--- a/src/flexchart/org/ovirt/DataSource.as
+++ b/src/flexchart/org/ovirt/DataSource.as
@@ -25,19 +25,21 @@ package org.ovirt {
   import com.adobe.serialization.json.JSON;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
+  import org.ovirt.data.DataSeries;
+  import org.ovirt.charts.Chart;
 
   public class DataSource {
 
-    private var chartLoader:ChartLoader;
+    private var chart:Chart;
 
-    public function DataSource(chartLoader:ChartLoader) {
-      this.chartLoader = chartLoader;
+    public function DataSource(chart:Chart) {
+      this.chart = chart;
     }
 
     public function retrieveData(url:String):void {
       var loader:URLLoader = new URLLoader();
-      loader.addEventListener( IOErrorEvent.IO_ERROR, this.ioError );
-      loader.addEventListener( Event.COMPLETE, dataLoaded );
+      loader.addEventListener(IOErrorEvent.IO_ERROR, this.ioError);
+      loader.addEventListener(Event.COMPLETE, dataLoaded);
       var request:URLRequest = new URLRequest(url);
       loader.load(request);
     }
@@ -46,7 +48,7 @@ package org.ovirt {
       var loader:URLLoader = URLLoader(event.target);
       var object:Object = JSON.decode(loader.data);
       var series:DataSeries = new DataSeries(object);
-      chartLoader.addData(series);
+      chart.addData(series);
     }
 
     private function ioError( e:IOErrorEvent ):void {
diff --git a/src/flexchart/org/ovirt/charts/BarChart.as b/src/flexchart/org/ovirt/charts/BarChart.as
new file mode 100644
index 0000000..83cf0bb
--- /dev/null
+++ b/src/flexchart/org/ovirt/charts/BarChart.as
@@ -0,0 +1,158 @@
+/*
+ Copyright (C) 2008 Red Hat, Inc.
+ Written by Steve Linabery <slinabery at redhat.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ MA  02110-1301, USA.  A copy of the GNU General Public License is
+ also available at http://www.gnu.org/copyleft/gpl.html.
+*/
+
+package org.ovirt.charts {
+
+  import mx.containers.Box;
+  import mx.containers.HBox;
+  import mx.containers.VBox;
+  import mx.controls.Text;
+  import mx.containers.Canvas;
+  import org.ovirt.data.*;
+  import org.ovirt.elements.*;
+  import org.ovirt.Constants;
+
+  public class BarChart extends Chart {
+
+    private var chartArea:HBox;
+    private var labelArea:Canvas;
+
+    public function BarChart(container:Box,
+                             datasourceUrl:String) {
+      super(container,datasourceUrl);
+      chartArea = new HBox();
+      chartArea.setStyle("horizontalGap",Constants.barSpacing);
+      chartArea.setStyle("verticalAlign","bottom");
+      chartArea.percentHeight = 80;
+      chartArea.percentWidth = 100;
+      this.container.addChild(chartArea);
+
+      labelArea = new Canvas();
+      labelArea.height = Constants.labelHeight;
+      labelArea.minHeight = Constants.labelHeight;
+      labelArea.percentWidth = 100;
+      this.container.addChild(labelArea);
+    }
+
+    override public function addData(dataSeries:DataSeries):void {
+      try {
+        var dataPoints:Array = dataSeries.getDataPoints();
+        var maxValue:Number = dataSeries.getMaxValue();
+        var scale:Number = maxValue;
+        //avoid divide by zero
+        if (scale == 0) {
+          scale = 1;
+        }
+        var size:int = dataPoints.length;
+        if (size == 0) {
+          throw new Error("No data points in range");
+        }
+
+        //have to iterate through datapoint.timestamp strings,
+        //create a TextLiberation object with them, and add them to
+        //a parent container before we can tell how wide they are in pixels.
+        var labelWidth:Number = 0;
+        for (var i:int = 0; i < size; i++) {
+          var dataPoint:DataPoint = dataPoints[i] as DataPoint;
+          var textTemp:TextLiberation =
+            new TextLiberation(dataPoint.getTimestamp());
+          textTemp.setVisible(false);
+          chartArea.addChild(textTemp);
+          var tempLabelWidth:Number = textTemp.getTextWidth();
+          if (! isNaN(tempLabelWidth)) {
+            labelWidth = Math.max(labelWidth, tempLabelWidth);
+          }
+        }
+        //now we have to remove all the children we just added, since we don't
+        //really want them to be part of the chart.
+        chartArea.removeAllChildren();
+
+        //we always want an odd number of y-axis labels, and we'll
+        //determine this by using the labelWidth we just determined
+        var labelCount:int = Math.floor(Constants.width / labelWidth);
+        if (labelCount > 3 && labelCount % 2 == 1) {
+          labelCount--;
+        }
+
+        //the distance between left edges of adjacent bars
+        var gridWidth:Number = Constants.width / size;
+
+        //the width of each SingleBar (does not including padding between bars)
+        var barWidth:Number = gridWidth - Constants.barSpacing;
+
+        //use this to center y-axis labels on the bars
+        var labelOffset:Number = barWidth / 2;
+
+        //distance between first and last label
+        var labelSpace:Number = Constants.width - gridWidth;
+        var labelSpacing:Number = labelSpace / labelCount;
+
+        //add the bars & labels to the chart
+        var labelCounter:int = 0;
+        for (i = 0; i < size; i++) {
+          dataPoint = dataPoints[i] as DataPoint;
+          var value:Number = dataPoint.getValue();
+          var bar:SingleBar = new SingleBar(dataPoint);
+          bar.percentHeight = ((value / scale) * 80);
+          bar.width = barWidth;
+          bar.setVisible(true);
+          chartArea.addChild(bar);
+          var currentLabelPosition:int = labelCounter * labelSpacing +
+                                           labelOffset;
+
+          if (currentLabelPosition >= i * gridWidth &&
+                currentLabelPosition < (i + 1) * gridWidth) {
+            var label:YAxisLabel = new YAxisLabel(dataPoint.getTimestamp());
+            label.setVisible(false);
+            label.y = ((labelCounter + 1) % 2) * 13 + 4;
+            labelArea.addChild(label);
+            //make sure the label is fully within the chart width
+            label.x = Math.max(0,
+                               Math.min((i) * gridWidth -
+                                 (label.labelText.getTextWidth() / 2) +
+                                 labelOffset,
+                               Constants.width -
+                                 label.labelText.getTextWidth() - 6)
+                              );
+            label.setVisible(true);
+            labelCounter++;
+
+            //add a 'tick' in the center of the bar to which this label
+            //corresponds
+            var ind:Box = new Box();
+            ind.opaqueBackground = 0x000000;
+            ind.width=1;
+            ind.height=3;
+            ind.x =  (i) * gridWidth + labelOffset;
+            ind.y = 0;
+            ind.setVisible(true);
+            ind.setStyle("backgroundColor","0x000000");
+            labelArea.addChild(ind);
+          }
+        }
+      } catch (e:Error) {
+        var err:Text = new Text();
+        err.text = e.message;
+        err.setVisible(true);
+        chartArea.addChild(err);
+      }
+    }
+  }
+}
diff --git a/src/flexchart/org/ovirt/charts/Chart.as b/src/flexchart/org/ovirt/charts/Chart.as
new file mode 100644
index 0000000..26c8d02
--- /dev/null
+++ b/src/flexchart/org/ovirt/charts/Chart.as
@@ -0,0 +1,46 @@
+/*
+ Copyright (C) 2008 Red Hat, Inc.
+ Written by Steve Linabery <slinabery at redhat.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ MA  02110-1301, USA.  A copy of the GNU General Public License is
+ also available at http://www.gnu.org/copyleft/gpl.html.
+*/
+
+package org.ovirt.charts {
+
+  public class Chart {
+
+    import org.ovirt.DataSource;
+    import mx.containers.Box;
+    import org.ovirt.data.DataSeries;
+
+    protected var container:Box;
+    protected var datasourceUrl:String;
+
+    public function Chart(container:Box, datasourceUrl:String) {
+      this.container = container;
+      this.datasourceUrl = datasourceUrl;
+    }
+
+    public function addData(dataSeries:DataSeries):void {
+      //override me!
+    }
+
+    public function load():void {
+      var dataSource:DataSource = new DataSource(this);
+      dataSource.retrieveData(datasourceUrl);
+    }
+  }
+}
diff --git a/src/flexchart/org/ovirt/data/DataPoint.as b/src/flexchart/org/ovirt/data/DataPoint.as
new file mode 100644
index 0000000..00cd0a4
--- /dev/null
+++ b/src/flexchart/org/ovirt/data/DataPoint.as
@@ -0,0 +1,47 @@
+/*
+ Copyright (C) 2008 Red Hat, Inc.
+ Written by Steve Linabery <slinabery at redhat.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ MA  02110-1301, USA.  A copy of the GNU General Public License is
+ also available at http://www.gnu.org/copyleft/gpl.html.
+*/
+
+package org.ovirt.data {
+
+  public class DataPoint {
+
+    private var timestamp:String;
+    private var value:Number;
+    private var description:String;
+
+    public function DataPoint (timestamp:String, value:Number, description:String) {
+      this.timestamp = timestamp;
+      this.value = value;
+      this.description = description;
+    }
+
+    public function getTimestamp():String {
+      return timestamp;
+    }
+
+    public function getValue():Number {
+      return value;
+    }
+
+    public function getDescription():String {
+      return description;
+    }
+  }
+}
diff --git a/src/flexchart/org/ovirt/data/DataSeries.as b/src/flexchart/org/ovirt/data/DataSeries.as
new file mode 100644
index 0000000..764fd34
--- /dev/null
+++ b/src/flexchart/org/ovirt/data/DataSeries.as
@@ -0,0 +1,54 @@
+/*
+ Copyright (C) 2008 Red Hat, Inc.
+ Written by Steve Linabery <slinabery at redhat.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ MA  02110-1301, USA.  A copy of the GNU General Public License is
+ also available at http://www.gnu.org/copyleft/gpl.html.
+*/
+
+//class to encapsulate the json object representation of a data
+//series returned from stats package
+
+package org.ovirt.data {
+
+  public class DataSeries {
+
+    private var object:Object;
+    private var dataPoints:Array;
+    private var description:String;
+    private var maxValue:Number;
+
+    public function DataSeries (object:Object) {
+      this.object = object;
+      this.description = object["description"] as String;
+      dataPoints = new Array();
+      var inDataPoints:Array = object["vectors"] as Array;
+      for (var i:int = 0; i < inDataPoints.length; i++) {
+        dataPoints.push(new DataPoint((inDataPoints[i] as Array)[0] as String,
+                                  (inDataPoints[i] as Array)[1] as Number,
+                                  description));
+      }
+      maxValue = object["max_value"] as Number;
+    }
+
+    public function getDataPoints():Array {
+      return dataPoints;
+    }
+
+    public function getMaxValue():Number {
+      return maxValue;
+    }
+  }
+}
diff --git a/src/flexchart/org/ovirt/elements/SingleBar.as b/src/flexchart/org/ovirt/elements/SingleBar.as
new file mode 100644
index 0000000..6e09bff
--- /dev/null
+++ b/src/flexchart/org/ovirt/elements/SingleBar.as
@@ -0,0 +1,67 @@
+/*
+ Copyright (C) 2008 Red Hat, Inc.
+ Written by Steve Linabery <slinabery at redhat.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ MA  02110-1301, USA.  A copy of the GNU General Public License is
+ also available at http://www.gnu.org/copyleft/gpl.html.
+*/
+
+package org.ovirt.elements {
+
+  import mx.containers.Box;
+  import mx.controls.ToolTip;
+  import mx.managers.ToolTipManager;
+  import flash.events.Event;
+  import flash.events.MouseEvent;
+  import flash.geom.Rectangle;
+  import flash.display.DisplayObject;
+  import org.ovirt.data.DataPoint;
+
+  public class SingleBar extends Box {
+
+    private var tip:ToolTip;
+    private var dataPoint:DataPoint;
+
+    public function SingleBar(dataPoint:DataPoint) {
+      super();
+      this.dataPoint = dataPoint;
+      addEventListener(MouseEvent.MOUSE_OVER,showTip);
+      addEventListener(MouseEvent.MOUSE_OUT,destroyTip);
+      this.setStyle("backgroundColor","0x0000FF");
+      this.setStyle("left","1");
+      this.setStyle("right","1");
+    }
+
+    private function showTip(event:Event):void {
+      var w:Number = this.stage.width;
+      var target:DisplayObject = event.currentTarget as DisplayObject;
+      var pt:Rectangle = this.stage.getBounds(target);
+      var yPos:Number = pt.y * -1;
+      var xPos:Number = pt.x * -1;
+      tip = ToolTipManager.createToolTip(dataPoint.getDescription() + "\n" +
+                                           dataPoint.getTimestamp() + "\n" +
+                                           dataPoint.getValue(),
+                                         xPos,yPos) as ToolTip;
+      tip.x = Math.min(tip.x,
+                       w - tip.width);
+      tip.y = Math.max(0,
+                       tip.y - tip.height);
+    }
+
+    private function destroyTip(event:Event):void {
+      ToolTipManager.destroyToolTip(tip);
+    }
+  }
+}
diff --git a/src/flexchart/org/ovirt/elements/TextLiberation.as b/src/flexchart/org/ovirt/elements/TextLiberation.as
new file mode 100644
index 0000000..f0314a2
--- /dev/null
+++ b/src/flexchart/org/ovirt/elements/TextLiberation.as
@@ -0,0 +1,46 @@
+/*
+ Copyright (C) 2008 Red Hat, Inc.
+ Written by Steve Linabery <slinabery at redhat.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ MA  02110-1301, USA.  A copy of the GNU General Public License is
+ also available at http://www.gnu.org/copyleft/gpl.html.
+*/
+
+package org.ovirt.elements {
+
+  import mx.controls.Label;
+
+  public class TextLiberation extends Label {
+
+    //FIXME: this should point to a local asset somehow.
+    [Embed(source='/usr/share/fonts/liberation/LiberationSans-Regular.ttf',
+           fontName='liberation',
+           mimeType='application/x-font'
+          )]
+    //this variable exists so that the compiler will link in the font.
+    private var font1:Class;
+
+    public function TextLiberation(text:String) {
+      super();
+      this.text = text;
+      this.setStyle("fontFamily","liberation");
+    }
+
+    public function getTextWidth():Number {
+      this.validateNow();
+      return this.textWidth;
+    }
+  }
+}
diff --git a/src/flexchart/org/ovirt/elements/YAxisLabel.as b/src/flexchart/org/ovirt/elements/YAxisLabel.as
new file mode 100644
index 0000000..0e93b97
--- /dev/null
+++ b/src/flexchart/org/ovirt/elements/YAxisLabel.as
@@ -0,0 +1,41 @@
+/*
+ Copyright (C) 2008 Red Hat, Inc.
+ Written by Steve Linabery <slinabery at redhat.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ MA  02110-1301, USA.  A copy of the GNU General Public License is
+ also available at http://www.gnu.org/copyleft/gpl.html.
+*/
+
+package org.ovirt.elements {
+
+  import mx.containers.Box;
+  import mx.core.ScrollPolicy;
+
+  public class YAxisLabel extends Box {
+
+    public var labelText:TextLiberation;
+
+    public function YAxisLabel(text:String) {
+      super();
+      labelText = new TextLiberation(text);
+      labelText.setVisible(true);
+      this.addChild(labelText);
+      this.horizontalScrollPolicy = ScrollPolicy.OFF;
+      this.verticalScrollPolicy = ScrollPolicy.OFF;
+      this.setStyle("paddingLeft","0");
+      this.setStyle("paddingRight","0");
+    }
+  }
+}
-- 
1.5.6.5




More information about the ovirt-devel mailing list