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;
015import com.ganteater.ae.processor.annotation.CommandDescription;
016
017@CommandDescription("`XYLineChart` View Type example:\r\n"
018                + "```xml\r\n"
019                + "<View name=\"xyView\" reuse=\"true\" type=\"XYLineChart\" title=\"XYLineChart\" xAxisLabel=\"x\" yAxisLabel=\"y\" /> \r\n"
020                + "<Loop name=\"x\" numbers=\"10\">\r\n"
021                + "     <Rnd name=\"value\" symbols=\"3\" type=\"number\" /> \r\n"
022                + "     <Out view=\"xyView\" level=\"debug\" x=\"$var{x}\">$var{value}</Out> \r\n"
023                + "</Loop> ")
024public class XYLineChart extends View {
025        private static final long serialVersionUID = 1L;
026
027        private XYSeriesCollection dataset;
028        private JFreeChart chart = null;
029
030        public void init(Properties params, AEManager manager) {
031                super.init(params, manager);
032                String title = params.getProperty("title");
033                String xAxisLabel = params.getProperty("xAxisLabel");
034                String yAxisLabel = params.getProperty("yAxisLabel");
035
036                dataset = new XYSeriesCollection();
037                chart = ChartFactory.createXYLineChart(title, xAxisLabel, yAxisLabel, dataset, PlotOrientation.VERTICAL, true,
038                                true, false);
039
040                add(new ChartPanel(chart), BorderLayout.CENTER);
041        }
042
043        public void start() {
044        }
045
046        synchronized public void out(Object data, Properties properties) {
047                String seriesAttr = properties.getProperty("series");
048                XYSeries series;
049
050                if (seriesAttr == null) {
051                        if (dataset.getSeriesCount() == 0) {
052                                series = new XYSeries("data");
053                                dataset.addSeries(series);
054                        } else {
055                                series = dataset.getSeries(0);
056                        }
057                } else {
058                        try {
059                                series = dataset.getSeries(seriesAttr);
060                        } catch (UnknownKeyException e) {
061                                series = new XYSeries(seriesAttr);
062                                dataset.addSeries(series);
063                        }
064                }
065
066                double theX = 0;
067                String xAttr = properties.getProperty("x");
068                if (xAttr == null) {
069                        theX = series.getItemCount();
070                } else {
071                        theX = Double.parseDouble(xAttr);
072                }
073                try {
074                        if (data instanceof String) {
075                                double theY = Double.parseDouble((String) data);
076                                series.add(theX, theY);
077                        }
078                } catch (Exception e) {
079                        throw new IllegalArgumentException("Invalid output data: " + data, e);
080                }
081        }
082}