From a804380fe076def0038b35e8eef023393cd27a4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Dr=C3=A4ger?= Date: Mon, 12 Apr 2010 08:11:46 +0000 Subject: [PATCH] Improved the plot function: it is now easier to change the plot from linear to logarithmic scale and the other way arround. --- src/eva2/gui/FunctionArea.java | 32 +++- src/eva2/gui/Plot.java | 290 +++++++++++++++++++-------------- 2 files changed, 191 insertions(+), 131 deletions(-) diff --git a/src/eva2/gui/FunctionArea.java b/src/eva2/gui/FunctionArea.java index 5e2dccee..ed5d47c0 100644 --- a/src/eva2/gui/FunctionArea.java +++ b/src/eva2/gui/FunctionArea.java @@ -296,7 +296,11 @@ public class FunctionArea extends DArea implements Serializable { updateLegend(); } - protected boolean checkLoggable() { + /** + * + * @return + */ + public boolean checkLoggable() { double minY = Double.MAX_VALUE; for (int i = 0; i < m_PointSetContainer.size(); i++) { DPointSet pSet = (m_PointSetContainer.get(i).getConnectedPointSet()); @@ -522,7 +526,7 @@ public class FunctionArea extends DArea implements Serializable { } } if (maxSize > 0) { // if there is any data, init string array and set x - // value column + // value column s = new String[maxSize + 1]; s[0] = "calls"; for (int j = 1; j <= maxSize; j++) @@ -536,11 +540,11 @@ public class FunctionArea extends DArea implements Serializable { GraphPointSet set = (GraphPointSet) m_PointSetContainer.get(i); DPointSet pset = set.getConnectedPointSet(); s[0] = s[0] + " " + cleanBlanks(set.getInfoString(), '_'); // add - // column - // name + // column + // name for (int j = 1; j < s.length; j++) { // add column data of place - // holder if no value in - // this set + // holder if no value in + // this set if ((j - 1) < pset.getSize()) s[j] = s[j] + " " + pset.getDPoint(j - 1).y; else @@ -748,7 +752,7 @@ public class FunctionArea extends DArea implements Serializable { if (legendBox != null && (m_legend)) legendBox.paintIn(g, m_Border.getInnerRect(this)); } - + /** * */ @@ -968,13 +972,25 @@ public class FunctionArea extends DArea implements Serializable { ScaledBorder buffer = m_Border; m_Border = new ScaledBorder(); m_Border.x_label = buffer.x_label; // "App. " + Name + - // " func. calls"; + // " func. calls"; m_Border.y_label = buffer.y_label; // "fitness"; setBorder(m_Border); } repaint(); } + /** + * Allows setting whether or not to paint the y-axis in logarithmic scale. + * + * @param log + * if true logarithmic scale is used, linear scale in case of + * false. + */ + public void toggleLog(boolean log) { + if (log != m_log) + toggleLog(); + } + /** * Recreate the legend object with the current point sets. * diff --git a/src/eva2/gui/Plot.java b/src/eva2/gui/Plot.java index d0b05087..7a701181 100644 --- a/src/eva2/gui/Plot.java +++ b/src/eva2/gui/Plot.java @@ -1,4 +1,5 @@ package eva2.gui; + /* * Title: EvA2 * Description: @@ -45,6 +46,7 @@ import eva2.server.go.individuals.AbstractEAIndividual; import eva2.server.go.populations.Population; import eva2.tools.BasicResourceLoader; import eva2.tools.chart2d.DPointSet; + /*==========================================================================* * CLASS DECLARATION *==========================================================================*/ @@ -53,6 +55,10 @@ import eva2.tools.chart2d.DPointSet; */ public class Plot implements PlotInterface, Serializable { + /** + * Generated serial version identifier. + */ + private static final long serialVersionUID = -9027101244918249825L; public static boolean TRACE = false; private JFileChooser m_FileChooser; private JPanel m_ButtonPanel; @@ -63,36 +69,43 @@ public class Plot implements PlotInterface, Serializable { protected JFrame m_Frame; /** - * You might want to try to assign the x-range as x and y-range as y array parameters. + * You might want to try to assign the x-range as x and y-range as y array + * parameters. */ - public Plot(String PlotName,String xname,String yname,double[] x,double[] y) { - if (TRACE) System.out.println("Constructor Plot "+PlotName); + public Plot(String PlotName, String xname, String yname, double[] x, + double[] y) { + if (TRACE) + System.out.println("Constructor Plot " + PlotName); m_xname = xname; m_yname = yname; m_PlotName = PlotName; init(); DPointSet points = new DPointSet(); - for (int i=0;i ClassType = Class.forName("FunctionArea"); if (!ClassType.isAssignableFrom(obj.getClass())) - throw new Exception("Object not of type: " + ClassType.getName()); + throw new Exception("Object not of type: " + + ClassType.getName()); return obj; } catch (Exception ex) { - JOptionPane.showMessageDialog(m_Frame, - "Couldn't read object: " - + selected.getName() - + "\n" + ex.getMessage(), - "Open object file", - JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(m_Frame, "Couldn't read object: " + + selected.getName() + "\n" + ex.getMessage(), + "Open object file", JOptionPane.ERROR_MESSAGE); } } return null; @@ -356,15 +401,15 @@ public class Plot implements PlotInterface, Serializable { if (returnVal == JFileChooser.APPROVE_OPTION) { File sFile = m_FileChooser.getSelectedFile(); if (sFile.exists()) { - returnVal = JOptionPane.showConfirmDialog(m_Frame, "The file "+sFile.getName()+" already exists. Overwrite?"); - if (returnVal != JOptionPane.YES_OPTION) return; + returnVal = JOptionPane.showConfirmDialog(m_Frame, "The file " + + sFile.getName() + " already exists. Overwrite?"); + if (returnVal != JOptionPane.YES_OPTION) + return; } if (!(m_PlotArea.exportToAscii(sFile))) { JOptionPane.showMessageDialog(m_Frame, - "Couldn't write to file: " - + sFile.getName(), - "Export error", - JOptionPane.ERROR_MESSAGE); + "Couldn't write to file: " + sFile.getName(), + "Export error", JOptionPane.ERROR_MESSAGE); } } } @@ -379,15 +424,14 @@ public class Plot implements PlotInterface, Serializable { if (returnVal == JFileChooser.APPROVE_OPTION) { File sFile = m_FileChooser.getSelectedFile(); try { - ObjectOutputStream oo = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(sFile))); + ObjectOutputStream oo = new ObjectOutputStream( + new BufferedOutputStream(new FileOutputStream(sFile))); oo.writeObject(object); oo.close(); } catch (IOException ex) { JOptionPane.showMessageDialog(m_Frame, - "Couldn't write to file: " - + sFile.getName() - + "\n" + ex.getMessage(), - "Save object", + "Couldn't write to file: " + sFile.getName() + "\n" + + ex.getMessage(), "Save object", JOptionPane.ERROR_MESSAGE); } } @@ -414,6 +458,7 @@ public class Plot implements PlotInterface, Serializable { public FunctionArea getFunctionArea() { return m_PlotArea; } + /** * */ @@ -424,7 +469,7 @@ public class Plot implements PlotInterface, Serializable { /** * Add the corners of the given range as unconnected points. - * + * * @param range * @param graphLabel */ @@ -433,23 +478,22 @@ public class Plot implements PlotInterface, Serializable { setUnconnectedPoint(range[0][1], range[1][1], graphLabel); } - // /** - // * Just for testing the Plot class. - // */ - // public static void main( String[] args ){ - // Plot plot = new Plot("Plot-Test","x-value","y-value"); - // plot.init(); - // double x; - // for (x= 0; x <6000; x++) { - // //double y = SpecialFunction.getnormcdf(x); - // // double yy = 0.5*SpecialFunction.getnormpdf(x); - // double n = Math.sin(((double)x/1000*Math.PI)); - // //plot.setConnectedPoint(x,Math.sin(x),0); - // //plot.setConnectedPoint(x,Math.cos(x),1); - // //plot.setConnectedPoint(x,y,0); - // plot.setConnectedPoint(x,n,1); - // } - // //plot.addGraph(1,2); - // } + // /** + // * Just for testing the Plot class. + // */ + // public static void main( String[] args ){ + // Plot plot = new Plot("Plot-Test","x-value","y-value"); + // plot.init(); + // double x; + // for (x= 0; x <6000; x++) { + // //double y = SpecialFunction.getnormcdf(x); + // // double yy = 0.5*SpecialFunction.getnormpdf(x); + // double n = Math.sin(((double)x/1000*Math.PI)); + // //plot.setConnectedPoint(x,Math.sin(x),0); + // //plot.setConnectedPoint(x,Math.cos(x),1); + // //plot.setConnectedPoint(x,y,0); + // plot.setConnectedPoint(x,n,1); + // } + // //plot.addGraph(1,2); + // } } -