1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.swing;
14 import java.awt.Graphics2D;
15 import java.awt.geom.AffineTransform;
16 import java.util.ArrayList;
17 import java.util.List;
19 import javax.swing.JPanel;
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;
33 public class MultiVariableTrendNode extends ComponentNode<JPanel> {
38 private static final long serialVersionUID = 8508750881358776559L;
40 protected transient JFreeChart chart = null;
41 protected transient List<List<TrendPoint>> points = new ArrayList<List<TrendPoint>>();
42 protected transient XYSeriesCollection dataset = null;
46 XYSeries serie = new XYSeries("Trend");
47 dataset = new XYSeriesCollection(serie);
50 chart = ChartFactory.createXYLineChart(
55 PlotOrientation.VERTICAL,
60 final XYPlot plot = chart.getXYPlot();
61 ValueAxis axis = plot.getDomainAxis();
62 axis.setAutoRange(true);
63 // axis.setFixedAutoRange(60000.0); // 60 seconds
65 component = new ChartPanel(chart, false);
66 ((ChartPanel)component).setRefreshBuffer(false);
67 component.setIgnoreRepaint(true);
68 component.setDoubleBuffered(false);
70 component.setBounds(0, 0, 0, 0);
73 for(int i = 0; i < points.size(); i++) {
74 if(dataset.getSeriesCount() < i) {
75 XYSeries s = new XYSeries("Serie "+(i+1));
79 for(TrendPoint p : points.get(i)) {
81 dataset.getSeries(i).add(p.getX(), p.getY());
82 } catch(org.jfree.data.general.SeriesException e) {
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();
98 AffineTransform at = new AffineTransform();
99 at.setToTranslation(bounds.getMinX(), bounds.getMinY());
100 at.scale(1/scaleX, 1/scaleY);
102 int width = (int)(bounds.getWidth() * scaleX);
103 int height = (int)(bounds.getHeight() * scaleY);
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);
110 component.paint(g2d);
112 g2d.setTransform(ot);
116 @PropertySetter("Title")
118 public void setTitle(String title) {
119 if(component != null) {
120 synchronized(component) {
121 ((ChartPanel)component).getChart().setTitle(title);
126 @PropertySetter("X-Axis Label")
128 public void setXTitle(String xTitle) {
129 if(component != null) {
130 synchronized(component) {
131 ((ChartPanel)component).getChart().getXYPlot().getDomainAxis().setLabel(xTitle);
136 @PropertySetter("Y-Axis Label")
138 public void setYTitle(String yTitle) {
139 if(component != null) {
140 synchronized(component) {
141 ((ChartPanel)component).getChart().getXYPlot().getRangeAxis().setLabel(yTitle);
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)));
152 this.points.add(serie, points);
153 synchronized(component) {
154 dataset.getSeries(serie.intValue()).clear();
155 for(TrendPoint p : points) {
157 dataset.getSeries(serie.intValue()).add(p.getX(), p.getY());
158 } catch(org.jfree.data.general.SeriesException e) {
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)));
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
178 if(location.equals(Location.REMOTE)) {
179 this.points.get(serie).addAll(points);
182 for(TrendPoint p : points) {
184 dataset.getSeries(serie).add(p.getX(), p.getY());
185 } catch(org.jfree.data.general.SeriesException e) {