001package com.ganteater.ae.desktop.view;
002
003import java.awt.BorderLayout;
004import java.util.Properties;
005
006import org.jfree.chart.ChartFactory;
007import org.jfree.chart.ChartPanel;
008import org.jfree.chart.JFreeChart;
009import org.jfree.chart.plot.PlotOrientation;
010import org.jfree.data.UnknownKeyException;
011import org.jfree.data.xy.XYSeries;
012import org.jfree.data.xy.XYSeriesCollection;
013
014import com.ganteater.ae.AEManager;
015
016public class XYLineChart extends PresentationPanel {
017        private static final long serialVersionUID = 1L;
018
019        private XYSeriesCollection dataset;
020        private JFreeChart chart = null;
021
022        public XYLineChart(Properties params, AEManager manager) {
023                super(params);
024                String title = params.getProperty("title");
025                String xAxisLabel = params.getProperty("xAxisLabel");
026                String yAxisLabel = params.getProperty("yAxisLabel");
027
028                dataset = new XYSeriesCollection();
029                chart = ChartFactory.createXYLineChart(title, xAxisLabel, yAxisLabel, dataset, PlotOrientation.VERTICAL, true,
030                                true, false);
031
032                add(new ChartPanel(chart), BorderLayout.CENTER);
033        }
034
035        public void start() {
036        }
037
038        synchronized public void out(Object data, Properties properties) {
039                String seriesAttr = properties.getProperty("series");
040                XYSeries series;
041
042                if (seriesAttr == null) {
043                        if (dataset.getSeriesCount() == 0) {
044                                series = new XYSeries("data");
045                                dataset.addSeries(series);
046                        } else {
047                                series = dataset.getSeries(0);
048                        }
049                } else {
050                        try {
051                                series = dataset.getSeries(seriesAttr);
052                        } catch (UnknownKeyException e) {
053                                series = new XYSeries(seriesAttr);
054                                dataset.addSeries(series);
055                        }
056                }
057
058                double theX = 0;
059                String xAttr = properties.getProperty("x");
060                if (xAttr == null) {
061                        theX = series.getItemCount();
062                } else {
063                        theX = Double.parseDouble(xAttr);
064                }
065                try {
066                        if (data instanceof String) {
067                                double theY = Double.parseDouble((String) data);
068                                series.add(theX, theY);
069                        }
070                } catch (Exception e) {
071                        throw new IllegalArgumentException("Invalide output data: " + data, e);
072                }
073        }
074}