]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph.swing/src/org/simantics/scenegraph/swing/MultiVariableTrendNode.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scenegraph.swing / src / org / simantics / scenegraph / swing / MultiVariableTrendNode.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.scenegraph.swing;
13
14 import java.awt.Graphics2D;
15 import java.awt.geom.AffineTransform;
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import javax.swing.JPanel;
20
21 import org.jfree.chart.ChartFactory;
22 import org.jfree.chart.ChartPanel;
23 import org.jfree.chart.JFreeChart;
24 import org.jfree.chart.axis.ValueAxis;
25 import org.jfree.chart.plot.PlotOrientation;
26 import org.jfree.chart.plot.XYPlot;
27 import org.jfree.data.xy.XYSeries;
28 import org.jfree.data.xy.XYSeriesCollection;
29 import org.simantics.scenegraph.ExportableWidget.RasterOutputWidget;
30 import org.simantics.scenegraph.g2d.nodes.Trend2DNode.TrendPoint;
31
32 @RasterOutputWidget
33 public class MultiVariableTrendNode extends ComponentNode<JPanel> {
34
35     /**
36      * 
37      */
38     private static final long serialVersionUID = 8508750881358776559L;
39     
40     protected transient JFreeChart chart = null;
41     protected transient List<List<TrendPoint>> points = new ArrayList<List<TrendPoint>>();
42     protected transient XYSeriesCollection dataset = null;
43     
44     @Override
45     public void init() {
46         XYSeries serie = new XYSeries("Trend");
47         dataset = new XYSeriesCollection(serie);
48         scale = false;
49
50         chart = ChartFactory.createXYLineChart(
51                         "Trend", // Title
52                         "Value", // X-title
53                         "Time",  // Y-title
54                 dataset,
55                 PlotOrientation.VERTICAL,
56                 false,
57                 true,
58                 false
59         );
60         final XYPlot plot = chart.getXYPlot();
61         ValueAxis axis = plot.getDomainAxis();
62         axis.setAutoRange(true);
63 //        axis.setFixedAutoRange(60000.0);  // 60 seconds
64
65         component = new ChartPanel(chart, false);
66         ((ChartPanel)component).setRefreshBuffer(false);
67         component.setIgnoreRepaint(true); 
68         component.setDoubleBuffered(false);
69         if(bounds != null) {
70             component.setBounds(0, 0, 0, 0);
71         }
72
73         for(int i = 0; i < points.size(); i++) {
74                 if(dataset.getSeriesCount() < i) {
75                         XYSeries s = new XYSeries("Serie "+(i+1));
76                         dataset.addSeries(s);
77                         dataset.addSeries(s);
78                 }
79             for(TrendPoint p : points.get(i)) {
80                 try {
81                         dataset.getSeries(i).add(p.getX(), p.getY());
82                 } catch(org.jfree.data.general.SeriesException e) {
83
84                 }
85             }
86         }
87         super.init();
88     }
89
90     @Override
91     public void render(Graphics2D g2d) {
92         if (component != null) {
93                 AffineTransform ot = g2d.getTransform();
94             g2d.transform(transform);
95             double scaleX = g2d.getTransform().getScaleX();
96             double scaleY = g2d.getTransform().getScaleY();
97
98             AffineTransform at = new AffineTransform();
99             at.setToTranslation(bounds.getMinX(), bounds.getMinY());
100             at.scale(1/scaleX, 1/scaleY);
101             g2d.transform(at);
102             int width = (int)(bounds.getWidth() * scaleX);
103             int height = (int)(bounds.getHeight() * scaleY);
104
105                 synchronized(component) {
106                     component.setLocation((int)g2d.getTransform().getTranslateX(), (int)g2d.getTransform().getTranslateY());
107                     if(component.getSize().getWidth() != width || component.getSize().getHeight() != height) {
108                         component.setSize(width, height);
109                     }
110                     component.paint(g2d);
111                 }
112                 g2d.setTransform(ot);
113         }
114     }
115     
116     @PropertySetter("Title")
117     @ClientSide
118     public void setTitle(String title) {
119         if(component != null) {
120                 synchronized(component) {
121                         ((ChartPanel)component).getChart().setTitle(title);
122                 }
123         }
124     }
125
126     @PropertySetter("X-Axis Label")
127     @ClientSide
128     public void setXTitle(String xTitle) {
129         if(component != null) {
130                 synchronized(component) {
131                         ((ChartPanel)component).getChart().getXYPlot().getDomainAxis().setLabel(xTitle);
132                 }
133         }
134     }
135
136     @PropertySetter("Y-Axis Label")
137     @ClientSide
138     public void setYTitle(String yTitle) {
139         if(component != null) {
140                 synchronized(component) {
141                         ((ChartPanel)component).getChart().getXYPlot().getRangeAxis().setLabel(yTitle);
142                 }
143         }
144     }
145     
146     @ClientSide
147     public void setPoints(List<TrendPoint> points, Integer serie) {
148         while(dataset.getSeriesCount() <= serie) {
149                 this.points.add(new ArrayList<TrendPoint>());
150                 dataset.addSeries(new XYSeries("Trend "+(dataset.getSeriesCount()+1)));
151         }
152         this.points.add(serie, points);
153         synchronized(component) {
154                 dataset.getSeries(serie.intValue()).clear();
155                 for(TrendPoint p : points) {
156                     try {
157                         dataset.getSeries(serie.intValue()).add(p.getX(), p.getY());
158                     } catch(org.jfree.data.general.SeriesException e) {
159         
160                     }
161                 }
162         }
163     }
164
165     @ClientSide
166     protected void appendPoints(List<TrendPoint> points, Integer serie) {
167         while(dataset.getSeriesCount() <= serie) {
168                 this.points.add(new ArrayList<TrendPoint>());
169                 dataset.addSeries(new XYSeries("Trend "+(dataset.getSeriesCount()+1)));
170         }        
171         /**
172          * We need to have the same set of points on the both sides, so locally the points are just updated,
173          * but on remote side we send only the new points.
174          * In case we are running on local workbench, the point list is updated and in addition these points
175          * would be added to the list without this check.
176          * FIXME: find out some way to implement this without this check
177          */
178         if(location.equals(Location.REMOTE)) {
179             this.points.get(serie).addAll(points);
180         }
181
182         for(TrendPoint p : points) {
183             try {
184                 dataset.getSeries(serie).add(p.getX(), p.getY());
185             } catch(org.jfree.data.general.SeriesException e) {
186
187             }
188         }
189     }
190 }