]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/MultiVariableTrendNode.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph.swing / src / org / simantics / scenegraph / swing / MultiVariableTrendNode.java
diff --git a/bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/MultiVariableTrendNode.java b/bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/MultiVariableTrendNode.java
new file mode 100644 (file)
index 0000000..1d4a6c4
--- /dev/null
@@ -0,0 +1,190 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.scenegraph.swing;
+
+import java.awt.Graphics2D;\r
+import java.awt.geom.AffineTransform;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import javax.swing.JPanel;\r
+\r
+import org.jfree.chart.ChartFactory;\r
+import org.jfree.chart.ChartPanel;\r
+import org.jfree.chart.JFreeChart;\r
+import org.jfree.chart.axis.ValueAxis;\r
+import org.jfree.chart.plot.PlotOrientation;\r
+import org.jfree.chart.plot.XYPlot;\r
+import org.jfree.data.xy.XYSeries;\r
+import org.jfree.data.xy.XYSeriesCollection;\r
+import org.simantics.scenegraph.ExportableWidget.RasterOutputWidget;\r
+import org.simantics.scenegraph.g2d.nodes.Trend2DNode.TrendPoint;\r
+\r
+@RasterOutputWidget
+public class MultiVariableTrendNode extends ComponentNode<JPanel> {
+
+    /**
+     * 
+     */
+    private static final long serialVersionUID = 8508750881358776559L;
+    \r
+    protected transient JFreeChart chart = null;
+    protected transient List<List<TrendPoint>> points = new ArrayList<List<TrendPoint>>();
+    protected transient XYSeriesCollection dataset = null;\r
+    
+    @Override\r
+    public void init() {\r
+       XYSeries serie = new XYSeries("Trend");\r
+        dataset = new XYSeriesCollection(serie);\r
+       scale = false;\r
+\r
+        chart = ChartFactory.createXYLineChart(\r
+                       "Trend", // Title\r
+                       "Value", // X-title\r
+                       "Time",  // Y-title\r
+                dataset,\r
+                PlotOrientation.VERTICAL,\r
+                false,\r
+                true,\r
+                false\r
+        );\r
+        final XYPlot plot = chart.getXYPlot();\r
+        ValueAxis axis = plot.getDomainAxis();\r
+        axis.setAutoRange(true);\r
+//        axis.setFixedAutoRange(60000.0);  // 60 seconds\r
+\r
+        component = new ChartPanel(chart, false);\r
+        ((ChartPanel)component).setRefreshBuffer(false);\r
+        component.setIgnoreRepaint(true); \r
+        component.setDoubleBuffered(false);\r
+        if(bounds != null) {\r
+            component.setBounds(0, 0, 0, 0);\r
+        }\r
+\r
+       for(int i = 0; i < points.size(); i++) {\r
+               if(dataset.getSeriesCount() < i) {\r
+                       XYSeries s = new XYSeries("Serie "+(i+1));\r
+                       dataset.addSeries(s);\r
+                       dataset.addSeries(s);\r
+               }\r
+            for(TrendPoint p : points.get(i)) {\r
+                try {\r
+                       dataset.getSeries(i).add(p.getX(), p.getY());\r
+                } catch(org.jfree.data.general.SeriesException e) {\r
+\r
+                }\r
+            }\r
+       }\r
+        super.init();\r
+    }\r
+
+    @Override
+    public void render(Graphics2D g2d) {
+        if (component != null) {\r
+               AffineTransform ot = g2d.getTransform();\r
+            g2d.transform(transform);\r
+            double scaleX = g2d.getTransform().getScaleX();
+            double scaleY = g2d.getTransform().getScaleY();
+\r
+            AffineTransform at = new AffineTransform();
+            at.setToTranslation(bounds.getMinX(), bounds.getMinY());
+            at.scale(1/scaleX, 1/scaleY);
+            g2d.transform(at);
+            int width = (int)(bounds.getWidth() * scaleX);
+            int height = (int)(bounds.getHeight() * scaleY);
+
+               synchronized(component) {\r
+                   component.setLocation((int)g2d.getTransform().getTranslateX(), (int)g2d.getTransform().getTranslateY());\r
+                   if(component.getSize().getWidth() != width || component.getSize().getHeight() != height) {
+                       component.setSize(width, height);\r
+                   }
+                   component.paint(g2d);\r
+               }\r
+               g2d.setTransform(ot);
+        }
+    }\r
+    \r
+    @PropertySetter("Title")
+    @ClientSide
+    public void setTitle(String title) {
+        if(component != null) {
+               synchronized(component) {\r
+                       ((ChartPanel)component).getChart().setTitle(title);\r
+               }\r
+        }
+    }
+\r
+    @PropertySetter("X-Axis Label")
+    @ClientSide\r
+    public void setXTitle(String xTitle) {
+        if(component != null) {\r
+               synchronized(component) {\r
+                       ((ChartPanel)component).getChart().getXYPlot().getDomainAxis().setLabel(xTitle);\r
+               }\r
+        }\r
+    }
+\r
+    @PropertySetter("Y-Axis Label")
+    @ClientSide\r
+    public void setYTitle(String yTitle) {
+        if(component != null) {\r
+               synchronized(component) {\r
+                       ((ChartPanel)component).getChart().getXYPlot().getRangeAxis().setLabel(yTitle);\r
+               }\r
+        }\r
+    }\r
+    
+    @ClientSide\r
+    public void setPoints(List<TrendPoint> points, Integer serie) {\r
+       while(dataset.getSeriesCount() <= serie) {\r
+               this.points.add(new ArrayList<TrendPoint>());\r
+               dataset.addSeries(new XYSeries("Trend "+(dataset.getSeriesCount()+1)));\r
+       }
+        this.points.add(serie, points);
+       synchronized(component) {\r
+               dataset.getSeries(serie.intValue()).clear();
+               for(TrendPoint p : points) {
+                   try {
+                       dataset.getSeries(serie.intValue()).add(p.getX(), p.getY());
+                   } catch(org.jfree.data.general.SeriesException e) {
+       
+                   }
+               }\r
+       }
+    }
+
+    @ClientSide
+    protected void appendPoints(List<TrendPoint> points, Integer serie) {
+       while(dataset.getSeriesCount() <= serie) {\r
+               this.points.add(new ArrayList<TrendPoint>());\r
+               dataset.addSeries(new XYSeries("Trend "+(dataset.getSeriesCount()+1)));\r
+       }        \r
+       /**
+         * We need to have the same set of points on the both sides, so locally the points are just updated,
+         * but on remote side we send only the new points.
+         * In case we are running on local workbench, the point list is updated and in addition these points
+         * would be added to the list without this check.
+         * FIXME: find out some way to implement this without this check
+         */
+        if(location.equals(Location.REMOTE)) {
+            this.points.get(serie).addAll(points);
+        }
+
+        for(TrendPoint p : points) {
+            try {
+               dataset.getSeries(serie).add(p.getX(), p.getY());
+            } catch(org.jfree.data.general.SeriesException e) {
+
+            }
+        }
+    }
+}