TrendGraph.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00010 package com.generalrobotix.ui.view.graph;
00011 
00012 import java.util.*;
00013 import org.eclipse.swt.graphics.RGB;
00014 import com.generalrobotix.ui.grxui.Activator;
00015 
00016 
00022 public class TrendGraph {
00023 
00024     // -----------------------------------------------------------------
00025     private static final double MAX_DIV = 5;
00026     private static final double LOG10 = Math.log(10);
00027 
00028     public static final int SUCCEEDED = 0; 
00029     public static final int NOT_MATCHED = 1;
00030     public static final int NOT_SUPPORTED = 2;
00031     
00032     private static final String[] colorTable_ = {"green", "yellow", "cyan", "magenta", "red", "blue" };
00033       
00034     private static final int numColors_;
00035     static {
00036         numColors_ = colorTable_.length;
00037     }
00038     public RGB getGraphColor(int index) {
00039         int tmpcolorCounter_ = colorCounter_;
00040         tmpcolorCounter_ = tmpcolorCounter_ + index;
00041         if (tmpcolorCounter_ >= numColors_) {
00042             tmpcolorCounter_ = 0;
00043         }
00044         return Activator.getDefault().getColor(colorTable_[tmpcolorCounter_]).getRGB();
00045     }
00046 
00047     // -----------------------------------------------------------------
00048     private int colorCounter_;
00049 
00050     private DataKind dataKind_;
00051     private XYLineGraph graph_;
00052     private TrendGraphModel model_;
00053     private AxisInfo yAxisInfo_;
00054     private AxisInfo xAxisInfo_;
00055 
00056     private HashMap<String, DataItemInfo> dataItemInfoMap_;
00057 
00058     private String nodeName_;
00059     //private boolean projectRead_;
00060 
00061 //    private DataItemNode[] dataItemNodeArray_;
00062 //    private int dataItemNodeCount_;
00063 
00064     // -----------------------------------------------------------------
00071     public TrendGraph(TrendGraphModel model, String node) {
00072         model_ = model;
00073         nodeName_ = node;
00074 
00075         xAxisInfo_ = model_.getTimeAxisInfo();
00076         yAxisInfo_ = new AxisInfo(0, 1);
00077         yAxisInfo_.unitFont = Activator.getDefault().getFont( "dialog12" );
00078         yAxisInfo_.unitXOfs = 5;
00079         yAxisInfo_.unitYOfs = 15;
00080 
00081         dataKind_ = null;
00082 
00083         colorCounter_ = 0;
00084 
00085         dataItemInfoMap_ = new HashMap<String, DataItemInfo>();
00086    }
00087 
00088     // -----------------------------------------------------------------
00089 
00090     public void setGraph(XYLineGraph graph, LegendPanel legend) {
00091         graph_ = graph;
00092         graph_.setAxisInfo(
00093             XYLineGraph.AXIS_BOTTOM,
00094             xAxisInfo_
00095         );
00096 
00097         graph_.setLegend(legend);
00098         /*
00099         graph_.setAxisInfo( 
00100             XYLineGraph.AXIS_LEFT,
00101             yAxisInfo_
00102         );
00103         */
00104     }
00105 
00106     public XYLineGraph getGraph() {
00107         return graph_;
00108     }
00109 
00110     public void repaint() {
00111         graph_.redraw();
00112     }
00113 
00119     /*
00120     public void setDataKind(
00121         String dataKind,
00122         double base,
00123         double extent
00124     ) {
00125         dataKind_ = GraphProperties.getDataKindFromName(dataKind);
00126         yAxisInfo_ = new AxisInfo(base, extent);
00127         _updateDiv();
00128         yAxisInfo_.unitFont = new Font("dialog", Font.PLAIN, 12);
00129         yAxisInfo_.unitXOfs = 5;
00130         yAxisInfo_.unitYOfs = 7;
00131         yAxisInfo_.unitLabel = dataKind_.unitLabel;
00132         yAxisInfo_.factor = dataKind_.factor;
00133         graph_.setAxisInfo(
00134             XYLineGraph.AXIS_LEFT,
00135             yAxisInfo_
00136         );
00137     }
00138     */
00139 
00143     public int addDataItem(
00144                 DataItemInfo di
00145     ) {
00146         //projectRead_ = false;
00147 
00148         DataKind dataKind = GraphProperties.getDataKindFromAttr(di.dataItem.getFullAttributeName());
00149         if (dataKind == null) { 
00150             return NOT_SUPPORTED;
00151         }
00152         if (dataKind_ != null) {
00153             if (!dataKind.equals(dataKind_)) {
00154                 return NOT_MATCHED;
00155             }
00156         } else {
00157             dataKind_ = dataKind;
00158             //yAxisInfo_ = new AxisInfo(dataKind_.base, dataKind_.extent);
00159             //yAxisInfo_.unitFont = new Font("dialog", Font.PLAIN, 12);
00160             //yAxisInfo_.unitXOfs = 5;
00161             //yAxisInfo_.unitYOfs = 7;
00162             yAxisInfo_.base = dataKind_.base;
00163             yAxisInfo_.extent = dataKind_.extent;
00164             yAxisInfo_.unitLabel = dataKind_.unitLabel;
00165             yAxisInfo_.factor = dataKind_.factor;
00166             _updateDiv();
00167             graph_.setAxisInfo(
00168                 XYLineGraph.AXIS_LEFT,
00169                 yAxisInfo_
00170             );
00171 //            SEString dk = new SEString(dataKind_.name);
00172             //System.out.println(nodeName_ + "." + GraphNode.DATA_KIND + "=" + dk.toString());
00173 //            world_.updateAttribute(nodeName_ + "." + GraphNode.DATA_KIND + "=" + dk.toString());
00174 //            SEDoubleArray vr = new SEDoubleArray(new double[]{dataKind_.base, dataKind_.base + dataKind_.extent});
00175 //            world_.updateAttribute(nodeName_ + "." + GraphNode.V_RANGE + "=" + vr.toString());
00176         }
00177         if(di.color==null)
00178                 di.color = Activator.getDefault().getColor(colorTable_[colorCounter_]).getRGB();
00179         if(di.legend==null)
00180             di.legend = di.dataItem.toString();
00181         if(_addDataItem(di))
00182                 if (++colorCounter_ >= numColors_)
00183             colorCounter_ = 0;
00184  
00185         return SUCCEEDED;
00186     }
00187 
00192     public void removeDataItem(
00193         DataItemInfo dataItemInfo
00194     ) {
00195         model_.removeDataItem(dataItemInfo.dataItem);
00196         String key = dataItemInfo.dataItem.toString();
00197 
00198         graph_.removeDataSeries(dataItemInfo.dataSeries);
00199 
00200         dataItemInfoMap_.remove(key);
00201         if (dataItemInfoMap_.isEmpty()) {
00202             dataKind_ = null;
00203             //yAxisInfo_ = null; 
00204             graph_.setAxisInfo(
00205                 XYLineGraph.AXIS_LEFT,
00206                 null
00207             );
00208 //            SEString dk = new SEString("");
00209 //            world_.updateAttribute(nodeName_ + "." + GraphNode.DATA_KIND + "=" + dk.toString());
00210 //            SEDoubleArray vr = new SEDoubleArray(new double[]{0, 1});
00211 //            world_.updateAttribute(nodeName_ + "." + GraphNode.V_RANGE + "=" + vr.toString());
00212         }
00213 //        for (int i = 0; i < dataItemListenerList_.size(); i++) {
00214 //            DataItemListener listener = dataItemListenerList_.getListener(i);
00215 //            listener.dataItemRemoved(dii);
00216 //        }
00217     }
00218 
00223     public int getNumDataItems() {
00224         return dataItemInfoMap_.size();
00225     }
00226 
00231     public DataItemInfo[] getDataItemInfoList() {
00232         return dataItemInfoMap_.values().toArray(new DataItemInfo[0]);
00233     }
00234 
00239    public void setDataItemInfo(
00240         DataItemInfo dii
00241     ) {
00242 //        DataSeries ds = (DataSeries)dataSeriesMap_.get(dii.dataItem.toString());
00243 //        Color pc = graph_.getStyle(ds);
00244 //        String pl = graph_.getLegendLabel(ds);
00245 //        String diNodeName = _getDataItemNodeName(dii.dataItem);
00246         _setDataItemInfo(dii);
00247  /*       if (!pc.equals(dii.color)) { 
00248            world_.updateAttribute(
00249                 diNodeName + "." + DataItemNode.COLOR + "=" + (String)revColorMap_.get(dii.color)
00250             );
00251         }
00252         if (!pl.equals(dii.legend)) {
00253             world_.updateAttribute(
00254                 diNodeName + "." + DataItemNode.LEGEND + "=" + dii.legend
00255             );
00256         }*/
00257     }
00258 
00259     private void _setDataItemInfo(
00260         DataItemInfo dii
00261     ) {
00262         graph_.setStyle(dii.dataSeries, dii.color);
00263         graph_.setLegendLabel(dii.dataSeries, dii.legend);
00264     }
00265 
00271     public void setRange(
00272         double base,
00273         double extent
00274     ) {
00275         _setRange(base, extent);
00276 //        SEDoubleArray vr = new SEDoubleArray(new double[]{base, base + extent});
00277 //        world_.updateAttribute(nodeName_ + "." + GraphNode.V_RANGE + "=" + vr.toString());
00278     }
00279 
00284     public String getUnitLabel() {
00285         return yAxisInfo_.unitLabel;
00286     }
00287 
00292     public double getBase() {
00293         return yAxisInfo_.base;
00294     }
00295 
00300     public double getExtent() {
00301         return yAxisInfo_.extent;
00302     }
00303 
00309     public void setTimeRangeAndPos(
00310         double timeRange,
00311         double markerPos
00312     ) {
00313         _setTimeRangeAndPos(timeRange, markerPos);
00314 //        SEDoubleArray tr = new SEDoubleArray(new double[]{timeRange, markerPos});
00315         //SEDouble hr = new SEDouble(timeRange);
00316         //SEDouble mp = new SEDouble(markerPos);
00317 //        world_.updateAttribute(nodeName_ + "." + GraphNode.TIME_RANGE + "=" + tr.toString());
00318     }
00319 
00324 //    public void addDataItemListener(DataItemListener listener) {
00325 //        dataItemListenerList_.addListener(listener);
00326 //    }
00327 
00332   /*  public void removeDataItemListener(DataItemListener listener) {
00333         dataItemListenerList_.removeListener(listener);
00334     }*/
00335 
00336     // -----------------------------------------------------------------
00337     // WorldReplaceListener
00342 /*    public void replaceWorld(SimulationWorld world) {
00343         world_ = world;
00344         while (dataItemList_.size() > 0) {
00345             DataItem di = (DataItem)dataItemList_.get(0);
00346             model_.removeDataItem(di, false);
00347             String key = di.toString();
00348             dataItemInfoMap_.remove(key);
00349             DataSeries ds = (DataSeries)dataSeriesMap_.get(key);
00350             graph_.removeDataSeries(ds);
00351             dataSeriesMap_.remove(key);
00352             dataItemList_.remove(0);
00353         }
00354 
00355         //projectRead_ = true;
00356 
00357         dataKind_ = null;
00358 
00359         world_.addAttributeListener(this);
00360         world_.addNodeListener(this);
00361     }
00362 */
00363     // -----------------------------------------------------------------
00364     // AttributeListener
00371  /*   public void attributeChanged(
00372         SimulationNode node,
00373         String attribute,
00374         StringExchangeable value
00375     ) {
00376         String nodeType = node.getNodeName();
00377         String name = node.getName();
00378         if (nodeType.equals(GraphNode.NODE_TYPE)
00379             && name.equals(nodeName_)
00380         ) {
00381             if (attribute.equals(GraphNode.TIME_RANGE)) {
00382                 // timeRange
00383                 //System.out.println("set Time Range: " + value);
00384                 SEDoubleArray tr = (SEDoubleArray)value;
00385                 _setTimeRangeAndPos(tr.doubleValue(0), tr.doubleValue(1));
00386             } else if (attribute.equals(GraphNode.DATA_KIND)) {
00387                 // dataKind
00388                 _setDataKind(value.toString());
00389             } else if (attribute.equals(GraphNode.H_RANGE)) {
00390                 // hRange
00391                 _setTimeRange(((SEDouble)value).doubleValue());
00392                 //System.out.println("set HRange: " + value);
00393             } else if (attribute.equals(GraphNode.MARKER_POS)) {
00394                 // markerPos
00395                 _setMarkerPos(((SEDouble)value).doubleValue());
00396             } else if (attribute.equals(GraphNode.V_RANGE)) {
00397                 // vRange
00398                 SEDoubleArray vr = (SEDoubleArray)value;
00399                 _setRange(
00400                     vr.doubleValue(0),
00401                     vr.doubleValue(1) - vr.doubleValue(0)
00402                 );
00403             }
00404         } else if (nodeType.equals(DataItemNode.NODE_TYPE)
00405             && name.startsWith(nodeName_)
00406         ) {
00407             //System.out.println("### data item attribute changed = " + attribute);
00408             DataItemNode din = (DataItemNode)node;
00409             if (attribute.equals("color")) {
00410                 String diname = din.getDataItemName();
00411                 if (diname == null) {
00412                     //System.out.println("color $$$$ Data item name is not decided.");
00413                     return;
00414                 }
00415                 DataItemInfo dii = (DataItemInfo)dataItemInfoMap_.get(din.getDataItemName());
00416                 if (dii == null) {
00417                     //System.out.println("color $$$$ Data item info is not found. (" + diname + ")");
00418                     return;
00419                 }
00420                 dii.color = (Color)colorMap_.get(value.getValue());
00421                 _setDataItemInfo(dii);
00422                                 //
00423                 graph_.repaint();
00424                 graph_.getLegendPanel().repaint();
00425             }
00426             if (attribute.equals("legend")) {
00427                 String diname = din.getDataItemName();
00428                 if (diname == null) {
00429                     //System.out.println("legend $$$$ Data item name is not decided.");
00430                     return;
00431                 }
00432                 DataItemInfo dii = (DataItemInfo)dataItemInfoMap_.get(din.getDataItemName());
00433                 if (dii == null) {
00434                     //System.out.println("legend $$$$ Data item info is not found. (" + diname + ")");
00435                     return;
00436                 }
00437                 dii.legend = value.toString();
00438                 _setDataItemInfo(dii);
00439                 graph_.repaint();
00440                 graph_.getLegendPanel().repaint();
00441             }
00442         }
00443     }*/
00444 
00445     // -----------------------------------------------------------------
00451     private boolean _addDataItem(
00452         DataItemInfo dataItemInfo
00453     ) {
00454         DataItem dataItem = dataItemInfo.dataItem;
00455         String key = dataItem.toString();
00456 
00457         DataSeries ds = model_.addDataItem(dataItem);
00458         if (ds == null) {
00459             return false;
00460         }
00461         dataItemInfo.dataSeries = ds;
00462 
00463         if (dataItemInfoMap_.containsKey(key)) {
00464             return false;
00465         }
00466 
00467         dataItemInfoMap_.put(key, dataItemInfo);
00468 
00469  
00470         graph_.addDataSeries(
00471             ds, xAxisInfo_, yAxisInfo_,
00472             dataItemInfo.color,
00473             dataItemInfo.legend
00474         );
00475 
00476         return true;
00477     }
00478 
00483 /*  
00484      private void _setDataKind( String dataKind ) {
00485         if (dataKind_ != null) {
00486             if (dataKind.equals(dataKind_.name)) {
00487                 return;
00488             }
00489         }
00490         dataKind_ = GraphProperties.getDataKindFromName(dataKind);
00491         if (dataKind_ != null) {
00492             //yAxisInfo_.base = dataKind_.base;
00493             //yAxisInfo_.extent = dataKind_.extent;
00494             yAxisInfo_.unitLabel = dataKind_.unitLabel;
00495             yAxisInfo_.factor = dataKind_.factor;
00496             _updateDiv();
00497             graph_.setAxisInfo(
00498                 XYLineGraph.AXIS_LEFT,
00499                 yAxisInfo_
00500             );
00501         }
00502     }
00503 */
00509     private void _setRange(
00510         double base,
00511         double extent
00512     ) {
00513         yAxisInfo_.base = base;
00514         yAxisInfo_.extent = extent;
00515         _updateDiv();
00516     }
00517 
00522     String _getDataItemNodeName(DataItem di) {
00523         StringBuffer sb = new StringBuffer(nodeName_);
00524         sb.append('.');
00525         sb.append(nodeName_);
00526         if (di.object != null) {
00527             sb.append('_');
00528             sb.append(di.object);
00529         }
00530         sb.append('_');
00531         sb.append(di.node);
00532         sb.append('_');
00533         sb.append(di.attribute);
00534         if (di.index >= 0) {
00535             sb.append('_');
00536             sb.append(di.index);
00537         }
00538 
00539         return sb.toString();
00540     }
00541     
00542     String getNodeName() {
00543         return nodeName_;
00544     }
00545 
00551     private void _setTimeRangeAndPos(
00552         double timeRange,
00553         double markerPos
00554     ) {
00555         model_.setRangeAndPos(timeRange, markerPos);
00556     }
00557 
00561     private void _updateDiv() {
00562         double sMin = yAxisInfo_.extent / MAX_DIV;
00563         int eMin = (int)Math.floor(Math.log(sMin) / LOG10);
00564         double step = 0;
00565         String format = "0";
00566         int e = eMin;
00567         boolean found = false;
00568         while (!found) {
00569             int m = 1;
00570             for (int i = 1; i <= 3; i++) {
00571                 step = m * Math.pow(10.0, e);
00572                 if (sMin <= step) { // && step <= sMax
00573                     if (e < 0) {
00574                         char[] c = new char[-e + 2];
00575                         c[0] = '0';
00576                         c[1] = '.';
00577                         for (int j = 0; j < -e; j++) {
00578                             c[j + 2] = '0';
00579                         }
00580                         format = new String(c);
00581                     }
00582                     found = true;
00583                     break;
00584                 }
00585                 m += (2 * i - 1);
00586             }
00587             e++;
00588         }
00589         yAxisInfo_.tickEvery = step;
00590         yAxisInfo_.labelEvery = step;
00591         yAxisInfo_.gridEvery = step;
00592         yAxisInfo_.labelFormat = format;
00593     }
00594 
00595         public void clearDataItem() {
00596                 DataItemInfo[] info = getDataItemInfoList();
00597                 for (int j = 0; j < info.length; j++)
00598                         removeDataItem(info[j]);
00599         }
00600 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:57