TrendGraph.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
10 package com.generalrobotix.ui.view.graph;
11 
12 import java.util.*;
13 import org.eclipse.swt.graphics.RGB;
15 
16 
22 public class TrendGraph {
23 
24  // -----------------------------------------------------------------
25  private static final double MAX_DIV = 5;
26  private static final double LOG10 = Math.log(10);
27 
28  public static final int SUCCEEDED = 0;
29  public static final int NOT_MATCHED = 1;
30  public static final int NOT_SUPPORTED = 2;
31 
32  private static final String[] colorTable_ = {"green", "yellow", "cyan", "magenta", "red", "blue" };
33 
34  private static final int numColors_;
35  static {
36  numColors_ = colorTable_.length;
37  }
38  public RGB getGraphColor(int index) {
39  int tmpcolorCounter_ = colorCounter_;
40  tmpcolorCounter_ = tmpcolorCounter_ + index;
41  if (tmpcolorCounter_ >= numColors_) {
42  tmpcolorCounter_ = 0;
43  }
44  return Activator.getDefault().getColor(colorTable_[tmpcolorCounter_]).getRGB();
45  }
46 
47  // -----------------------------------------------------------------
48  private int colorCounter_;
49 
55 
56  private HashMap<String, DataItemInfo> dataItemInfoMap_;
57 
58  private String nodeName_;
59  //private boolean projectRead_;
60 
61 // private DataItemNode[] dataItemNodeArray_;
62 // private int dataItemNodeCount_;
63 
64  // -----------------------------------------------------------------
71  public TrendGraph(TrendGraphModel model, String node) {
72  model_ = model;
73  nodeName_ = node;
74 
75  xAxisInfo_ = model_.getTimeAxisInfo();
76  yAxisInfo_ = new AxisInfo(0, 1);
77  yAxisInfo_.unitFont = Activator.getDefault().getFont( "dialog12" );
78  yAxisInfo_.unitXOfs = 5;
79  yAxisInfo_.unitYOfs = 15;
80 
81  dataKind_ = null;
82 
83  colorCounter_ = 0;
84 
85  dataItemInfoMap_ = new HashMap<String, DataItemInfo>();
86  }
87 
88  // -----------------------------------------------------------------
89 
90  public void setGraph(XYLineGraph graph, LegendPanel legend) {
91  graph_ = graph;
92  graph_.setAxisInfo(
94  xAxisInfo_
95  );
96 
97  graph_.setLegend(legend);
98  /*
99  graph_.setAxisInfo(
100  XYLineGraph.AXIS_LEFT,
101  yAxisInfo_
102  );
103  */
104  }
105 
107  return graph_;
108  }
109 
110  public void repaint() {
111  graph_.redraw();
112  }
113 
119  /*
120  public void setDataKind(
121  String dataKind,
122  double base,
123  double extent
124  ) {
125  dataKind_ = GraphProperties.getDataKindFromName(dataKind);
126  yAxisInfo_ = new AxisInfo(base, extent);
127  _updateDiv();
128  yAxisInfo_.unitFont = new Font("dialog", Font.PLAIN, 12);
129  yAxisInfo_.unitXOfs = 5;
130  yAxisInfo_.unitYOfs = 7;
131  yAxisInfo_.unitLabel = dataKind_.unitLabel;
132  yAxisInfo_.factor = dataKind_.factor;
133  graph_.setAxisInfo(
134  XYLineGraph.AXIS_LEFT,
135  yAxisInfo_
136  );
137  }
138  */
139 
143  public int addDataItem(
144  DataItemInfo di
145  ) {
146  //projectRead_ = false;
147 
149  if (dataKind == null) {
150  return NOT_SUPPORTED;
151  }
152  if (dataKind_ != null) {
153  if (!dataKind.equals(dataKind_)) {
154  return NOT_MATCHED;
155  }
156  } else {
157  dataKind_ = dataKind;
158  //yAxisInfo_ = new AxisInfo(dataKind_.base, dataKind_.extent);
159  //yAxisInfo_.unitFont = new Font("dialog", Font.PLAIN, 12);
160  //yAxisInfo_.unitXOfs = 5;
161  //yAxisInfo_.unitYOfs = 7;
162  yAxisInfo_.base = dataKind_.base;
163  yAxisInfo_.extent = dataKind_.extent;
164  yAxisInfo_.unitLabel = dataKind_.unitLabel;
165  yAxisInfo_.factor = dataKind_.factor;
166  _updateDiv();
167  graph_.setAxisInfo(
169  yAxisInfo_
170  );
171 // SEString dk = new SEString(dataKind_.name);
172  //System.out.println(nodeName_ + "." + GraphNode.DATA_KIND + "=" + dk.toString());
173 // world_.updateAttribute(nodeName_ + "." + GraphNode.DATA_KIND + "=" + dk.toString());
174 // SEDoubleArray vr = new SEDoubleArray(new double[]{dataKind_.base, dataKind_.base + dataKind_.extent});
175 // world_.updateAttribute(nodeName_ + "." + GraphNode.V_RANGE + "=" + vr.toString());
176  }
177  if(di.color==null)
178  di.color = Activator.getDefault().getColor(colorTable_[colorCounter_]).getRGB();
179  if(di.legend==null)
180  di.legend = di.dataItem.toString();
181  if(_addDataItem(di))
182  if (++colorCounter_ >= numColors_)
183  colorCounter_ = 0;
184 
185  return SUCCEEDED;
186  }
187 
192  public void removeDataItem(
193  DataItemInfo dataItemInfo
194  ) {
195  model_.removeDataItem(dataItemInfo.dataItem);
196  String key = dataItemInfo.dataItem.toString();
197 
198  graph_.removeDataSeries(dataItemInfo.dataSeries);
199 
200  dataItemInfoMap_.remove(key);
201  if (dataItemInfoMap_.isEmpty()) {
202  dataKind_ = null;
203  //yAxisInfo_ = null;
204  graph_.setAxisInfo(
206  null
207  );
208 // SEString dk = new SEString("");
209 // world_.updateAttribute(nodeName_ + "." + GraphNode.DATA_KIND + "=" + dk.toString());
210 // SEDoubleArray vr = new SEDoubleArray(new double[]{0, 1});
211 // world_.updateAttribute(nodeName_ + "." + GraphNode.V_RANGE + "=" + vr.toString());
212  }
213 // for (int i = 0; i < dataItemListenerList_.size(); i++) {
214 // DataItemListener listener = dataItemListenerList_.getListener(i);
215 // listener.dataItemRemoved(dii);
216 // }
217  }
218 
223  public int getNumDataItems() {
224  return dataItemInfoMap_.size();
225  }
226 
232  return dataItemInfoMap_.values().toArray(new DataItemInfo[0]);
233  }
234 
239  public void setDataItemInfo(
240  DataItemInfo dii
241  ) {
242 // DataSeries ds = (DataSeries)dataSeriesMap_.get(dii.dataItem.toString());
243 // Color pc = graph_.getStyle(ds);
244 // String pl = graph_.getLegendLabel(ds);
245 // String diNodeName = _getDataItemNodeName(dii.dataItem);
246  _setDataItemInfo(dii);
247  /* if (!pc.equals(dii.color)) {
248  world_.updateAttribute(
249  diNodeName + "." + DataItemNode.COLOR + "=" + (String)revColorMap_.get(dii.color)
250  );
251  }
252  if (!pl.equals(dii.legend)) {
253  world_.updateAttribute(
254  diNodeName + "." + DataItemNode.LEGEND + "=" + dii.legend
255  );
256  }*/
257  }
258 
259  private void _setDataItemInfo(
260  DataItemInfo dii
261  ) {
262  graph_.setStyle(dii.dataSeries, dii.color);
263  graph_.setLegendLabel(dii.dataSeries, dii.legend);
264  }
265 
271  public void setRange(
272  double base,
273  double extent
274  ) {
275  _setRange(base, extent);
276 // SEDoubleArray vr = new SEDoubleArray(new double[]{base, base + extent});
277 // world_.updateAttribute(nodeName_ + "." + GraphNode.V_RANGE + "=" + vr.toString());
278  }
279 
284  public String getUnitLabel() {
285  return yAxisInfo_.unitLabel;
286  }
287 
292  public double getBase() {
293  return yAxisInfo_.base;
294  }
295 
300  public double getExtent() {
301  return yAxisInfo_.extent;
302  }
303 
309  public void setTimeRangeAndPos(
310  double timeRange,
311  double markerPos
312  ) {
313  _setTimeRangeAndPos(timeRange, markerPos);
314 // SEDoubleArray tr = new SEDoubleArray(new double[]{timeRange, markerPos});
315  //SEDouble hr = new SEDouble(timeRange);
316  //SEDouble mp = new SEDouble(markerPos);
317 // world_.updateAttribute(nodeName_ + "." + GraphNode.TIME_RANGE + "=" + tr.toString());
318  }
319 
324 // public void addDataItemListener(DataItemListener listener) {
325 // dataItemListenerList_.addListener(listener);
326 // }
327 
332  /* public void removeDataItemListener(DataItemListener listener) {
333  dataItemListenerList_.removeListener(listener);
334  }*/
335 
336  // -----------------------------------------------------------------
337  // WorldReplaceListener
342 /* public void replaceWorld(SimulationWorld world) {
343  world_ = world;
344  while (dataItemList_.size() > 0) {
345  DataItem di = (DataItem)dataItemList_.get(0);
346  model_.removeDataItem(di, false);
347  String key = di.toString();
348  dataItemInfoMap_.remove(key);
349  DataSeries ds = (DataSeries)dataSeriesMap_.get(key);
350  graph_.removeDataSeries(ds);
351  dataSeriesMap_.remove(key);
352  dataItemList_.remove(0);
353  }
354 
355  //projectRead_ = true;
356 
357  dataKind_ = null;
358 
359  world_.addAttributeListener(this);
360  world_.addNodeListener(this);
361  }
362 */
363  // -----------------------------------------------------------------
364  // AttributeListener
371  /* public void attributeChanged(
372  SimulationNode node,
373  String attribute,
374  StringExchangeable value
375  ) {
376  String nodeType = node.getNodeName();
377  String name = node.getName();
378  if (nodeType.equals(GraphNode.NODE_TYPE)
379  && name.equals(nodeName_)
380  ) {
381  if (attribute.equals(GraphNode.TIME_RANGE)) {
382  // timeRange
383  //System.out.println("set Time Range: " + value);
384  SEDoubleArray tr = (SEDoubleArray)value;
385  _setTimeRangeAndPos(tr.doubleValue(0), tr.doubleValue(1));
386  } else if (attribute.equals(GraphNode.DATA_KIND)) {
387  // dataKind
388  _setDataKind(value.toString());
389  } else if (attribute.equals(GraphNode.H_RANGE)) {
390  // hRange
391  _setTimeRange(((SEDouble)value).doubleValue());
392  //System.out.println("set HRange: " + value);
393  } else if (attribute.equals(GraphNode.MARKER_POS)) {
394  // markerPos
395  _setMarkerPos(((SEDouble)value).doubleValue());
396  } else if (attribute.equals(GraphNode.V_RANGE)) {
397  // vRange
398  SEDoubleArray vr = (SEDoubleArray)value;
399  _setRange(
400  vr.doubleValue(0),
401  vr.doubleValue(1) - vr.doubleValue(0)
402  );
403  }
404  } else if (nodeType.equals(DataItemNode.NODE_TYPE)
405  && name.startsWith(nodeName_)
406  ) {
407  //System.out.println("### data item attribute changed = " + attribute);
408  DataItemNode din = (DataItemNode)node;
409  if (attribute.equals("color")) {
410  String diname = din.getDataItemName();
411  if (diname == null) {
412  //System.out.println("color $$$$ Data item name is not decided.");
413  return;
414  }
415  DataItemInfo dii = (DataItemInfo)dataItemInfoMap_.get(din.getDataItemName());
416  if (dii == null) {
417  //System.out.println("color $$$$ Data item info is not found. (" + diname + ")");
418  return;
419  }
420  dii.color = (Color)colorMap_.get(value.getValue());
421  _setDataItemInfo(dii);
422  //
423  graph_.repaint();
424  graph_.getLegendPanel().repaint();
425  }
426  if (attribute.equals("legend")) {
427  String diname = din.getDataItemName();
428  if (diname == null) {
429  //System.out.println("legend $$$$ Data item name is not decided.");
430  return;
431  }
432  DataItemInfo dii = (DataItemInfo)dataItemInfoMap_.get(din.getDataItemName());
433  if (dii == null) {
434  //System.out.println("legend $$$$ Data item info is not found. (" + diname + ")");
435  return;
436  }
437  dii.legend = value.toString();
438  _setDataItemInfo(dii);
439  graph_.repaint();
440  graph_.getLegendPanel().repaint();
441  }
442  }
443  }*/
444 
445  // -----------------------------------------------------------------
451  private boolean _addDataItem(
452  DataItemInfo dataItemInfo
453  ) {
454  DataItem dataItem = dataItemInfo.dataItem;
455  String key = dataItem.toString();
456 
457  DataSeries ds = model_.addDataItem(dataItem);
458  if (ds == null) {
459  return false;
460  }
461  dataItemInfo.dataSeries = ds;
462 
463  if (dataItemInfoMap_.containsKey(key)) {
464  return false;
465  }
466 
467  dataItemInfoMap_.put(key, dataItemInfo);
468 
469 
470  graph_.addDataSeries(
471  ds, xAxisInfo_, yAxisInfo_,
472  dataItemInfo.color,
473  dataItemInfo.legend
474  );
475 
476  return true;
477  }
478 
483 /*
484  private void _setDataKind( String dataKind ) {
485  if (dataKind_ != null) {
486  if (dataKind.equals(dataKind_.name)) {
487  return;
488  }
489  }
490  dataKind_ = GraphProperties.getDataKindFromName(dataKind);
491  if (dataKind_ != null) {
492  //yAxisInfo_.base = dataKind_.base;
493  //yAxisInfo_.extent = dataKind_.extent;
494  yAxisInfo_.unitLabel = dataKind_.unitLabel;
495  yAxisInfo_.factor = dataKind_.factor;
496  _updateDiv();
497  graph_.setAxisInfo(
498  XYLineGraph.AXIS_LEFT,
499  yAxisInfo_
500  );
501  }
502  }
503 */
509  private void _setRange(
510  double base,
511  double extent
512  ) {
513  yAxisInfo_.base = base;
514  yAxisInfo_.extent = extent;
515  _updateDiv();
516  }
517 
522  String _getDataItemNodeName(DataItem di) {
523  StringBuffer sb = new StringBuffer(nodeName_);
524  sb.append('.');
525  sb.append(nodeName_);
526  if (di.object != null) {
527  sb.append('_');
528  sb.append(di.object);
529  }
530  sb.append('_');
531  sb.append(di.node);
532  sb.append('_');
533  sb.append(di.attribute);
534  if (di.index >= 0) {
535  sb.append('_');
536  sb.append(di.index);
537  }
538 
539  return sb.toString();
540  }
541 
542  String getNodeName() {
543  return nodeName_;
544  }
545 
551  private void _setTimeRangeAndPos(
552  double timeRange,
553  double markerPos
554  ) {
555  model_.setRangeAndPos(timeRange, markerPos);
556  }
557 
561  private void _updateDiv() {
562  double sMin = yAxisInfo_.extent / MAX_DIV;
563  int eMin = (int)Math.floor(Math.log(sMin) / LOG10);
564  double step = 0;
565  String format = "0";
566  int e = eMin;
567  boolean found = false;
568  while (!found) {
569  int m = 1;
570  for (int i = 1; i <= 3; i++) {
571  step = m * Math.pow(10.0, e);
572  if (sMin <= step) { // && step <= sMax
573  if (e < 0) {
574  char[] c = new char[-e + 2];
575  c[0] = '0';
576  c[1] = '.';
577  for (int j = 0; j < -e; j++) {
578  c[j + 2] = '0';
579  }
580  format = new String(c);
581  }
582  found = true;
583  break;
584  }
585  m += (2 * i - 1);
586  }
587  e++;
588  }
589  yAxisInfo_.tickEvery = step;
590  yAxisInfo_.labelEvery = step;
591  yAxisInfo_.gridEvery = step;
592  yAxisInfo_.labelFormat = format;
593  }
594 
595  public void clearDataItem() {
597  for (int j = 0; j < info.length; j++)
598  removeDataItem(info[j]);
599  }
600 }
int c
Definition: autoplay.py:16
void setAxisInfo(int axis, AxisInfo ai)
#define null
our own NULL pointer
Definition: IceTypes.h:57
void _setRange(double base, double extent)
png_uint_32 i
Definition: png.h:2735
HashMap< String, DataItemInfo > dataItemInfoMap_
Definition: TrendGraph.java:56
void removeDataItem(DataItemInfo dataItemInfo)
void setGraph(XYLineGraph graph, LegendPanel legend)
Definition: TrendGraph.java:90
void addDataSeries(DataSeries ds, AxisInfo xai, AxisInfo yai, RGB rgb, String legend)
void setRangeAndPos(double timeRange, double markerPos)
typedef int
Definition: png.h:1113
backing_store_ptr info
Definition: jmemsys.h:181
void _setTimeRangeAndPos(double timeRange, double markerPos)
void setStyle(DataSeries ds, RGB rgb)
void setTimeRangeAndPos(double timeRange, double markerPos)
org
boolean _addDataItem(DataItemInfo dataItemInfo)
void setRange(double base, double extent)
static DataKind getDataKindFromAttr(String attribute)
TrendGraph(TrendGraphModel model, String node)
Definition: TrendGraph.java:71
void setLegendLabel(DataSeries ds, String legend)


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Sep 8 2022 02:24:05