MOA 12.03
Real Time Analytics for Data Streams
|
00001 /* 00002 * LineGraphViewPanel.java 00003 * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand 00004 * @author Richard Kirkby ([email protected]) 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 * 00019 */ 00020 package moa.gui; 00021 00022 import java.awt.BasicStroke; 00023 import java.awt.Color; 00024 import java.awt.Graphics; 00025 import java.awt.Graphics2D; 00026 import java.awt.Shape; 00027 import java.awt.geom.GeneralPath; 00028 import java.util.ArrayList; 00029 import java.util.List; 00030 00031 import javax.swing.JPanel; 00032 import javax.swing.table.AbstractTableModel; 00033 00034 import moa.evaluation.LearningCurve; 00035 00042 public class LineGraphViewPanel extends JPanel { 00043 00044 private static final long serialVersionUID = 1L; 00045 00046 static final BasicStroke lineStroke = new BasicStroke(3.0f); 00047 00048 protected class PlotLine { 00049 00050 public LearningCurve curve; 00051 00052 public Color colour; 00053 00054 public int xAxisIndex; 00055 00056 public int yAxisIndex; 00057 00058 public double xMin; 00059 00060 public double xMax; 00061 00062 public double yMin; 00063 00064 public double yMax; 00065 00066 public float convertX(double x) { 00067 return (float) ((x - this.xMin) / (this.xMax - this.xMin)); 00068 } 00069 00070 public float convertY(double y) { 00071 return (float) ((y - this.yMin) / (this.yMax - this.yMin)); 00072 } 00073 00074 public Shape getShapeToPlot() { 00075 GeneralPath path = new GeneralPath(); 00076 if (this.curve.numEntries() > 0) { 00077 path.moveTo(convertX(this.curve.getMeasurement(0, 00078 this.xAxisIndex)), convertY(this.curve.getMeasurement( 00079 0, this.yAxisIndex))); 00080 for (int i = 1; i < this.curve.numEntries(); i++) { 00081 path.lineTo(convertX(this.curve.getMeasurement(i, 00082 this.xAxisIndex)), convertY(this.curve.getMeasurement(i, this.yAxisIndex))); 00083 } 00084 } 00085 return path; 00086 } 00087 } 00088 00089 protected class PlotPanel extends JPanel { 00090 00091 private static final long serialVersionUID = 1L; 00092 00093 @Override 00094 public void paint(Graphics g) { 00095 g.setColor(getBackground()); 00096 g.fillRect(0, 0, getWidth(), getHeight()); 00097 Graphics2D g2 = (Graphics2D) g; 00098 g2.scale(getWidth(), getHeight()); 00099 g2.setStroke(lineStroke); 00100 for (PlotLine plotLine : LineGraphViewPanel.this.plotLines) { 00101 g2.setPaint(plotLine.colour); 00102 g2.draw(plotLine.getShapeToPlot()); 00103 } 00104 } 00105 } 00106 00107 protected class PlotTableModel extends AbstractTableModel { 00108 00109 private static final long serialVersionUID = 1L; 00110 00111 @Override 00112 public String getColumnName(int col) { 00113 switch (col) { 00114 case 0: 00115 return "source"; 00116 case 1: 00117 return "colour"; 00118 case 2: 00119 return "x-axis"; 00120 case 3: 00121 return "y-axis"; 00122 case 4: 00123 return "x-min"; 00124 case 5: 00125 return "x-max"; 00126 case 6: 00127 return "y-min"; 00128 case 7: 00129 return "y-max"; 00130 } 00131 return null; 00132 } 00133 00134 @Override 00135 public int getColumnCount() { 00136 return 8; 00137 } 00138 00139 @Override 00140 public int getRowCount() { 00141 return LineGraphViewPanel.this.plotLines.size(); 00142 } 00143 00144 @Override 00145 public Object getValueAt(int row, int col) { 00146 PlotLine plotLine = LineGraphViewPanel.this.plotLines.get(row); 00147 switch (col) { 00148 case 0: 00149 return plotLine.curve; 00150 case 1: 00151 return plotLine.colour; 00152 case 2: 00153 return plotLine.curve.getMeasurementName(plotLine.xAxisIndex); 00154 case 3: 00155 return plotLine.curve.getMeasurementName(plotLine.yAxisIndex); 00156 case 4: 00157 return plotLine.xMin; 00158 case 5: 00159 return plotLine.xMax; 00160 case 6: 00161 return plotLine.yMin; 00162 case 7: 00163 return plotLine.yMax; 00164 } 00165 return null; 00166 } 00167 00168 @Override 00169 public boolean isCellEditable(int row, int col) { 00170 return false; 00171 } 00172 } 00173 00174 protected List<PlotLine> plotLines = new ArrayList<PlotLine>(); 00175 }