⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plot2d.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 *    Plot2D.java
 *    Copyright (C) 2000 Mark Hall
 *
 */


package weka.gui.visualize;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import weka.core.FastVector;
import weka.core.Instances;
import weka.core.Utils;

/**
 * This class plots datasets in two dimensions. It can also plot
 * classifier errors and clusterer predictions.
 * 
 * @author Mark Hall (mhall@cs.waikato.ac.nz)
 * @version $Revision$
 */
public class Plot2D extends JPanel {

  /* constants for shape types */
  public static final int MAX_SHAPES = 5;
  public static final int ERROR_SHAPE = 1000;
  public static final int MISSING_SHAPE = 2000;
  public static final int CONST_AUTOMATIC_SHAPE = -1;
  public static final int X_SHAPE = 0;
  public static final int PLUS_SHAPE = 1;
  public static final int DIAMOND_SHAPE = 2;
  public static final int TRIANGLEUP_SHAPE = 3;
  public static final int TRIANGLEDOWN_SHAPE = 4;
  public static final int DEFAULT_SHAPE_SIZE = 2;

  /** Default colour for the axis */
  protected Color m_axisColour = Color.green;

  /** Default colour for the plot background */
  protected Color m_backgroundColour = Color.black;

  /** The plots to display */
  protected FastVector m_plots = new FastVector();

  /** The master plot */
  protected PlotData2D m_masterPlot = null;

  /** The name of the master plot */
  protected String m_masterName = "master plot";

  /** The instances to be plotted */
  protected Instances m_plotInstances=null;

  /** An optional "compainion" of the panel. If specified, this
      class will get to do its thing with our graphics context
      before we do any drawing. Eg. the visualize panel may need
      to draw polygons etc. before we draw plot axis and data points */
  protected Plot2DCompanion m_plotCompanion=null;

  /** For popping up text info on data points */
  protected JFrame m_InstanceInfo = null;
  protected JTextArea m_InstanceInfoText = new JTextArea();

  /** The list of the colors used */
  protected FastVector m_colorList;

  /** default colours for colouring discrete class */
  protected Color [] m_DefaultColors = {Color.blue,
					Color.red,
					Color.green,
					Color.cyan,
					Color.pink,
					new Color(255, 0, 255),
					Color.orange,
					new Color(255, 0, 0),
					new Color(0, 255, 0),
					Color.white};

  /** Indexes of the attributes to go on the x and y axis and the attribute
      to use for colouring and the current shape for drawing */
  protected int m_xIndex=0;
  protected int m_yIndex=0;
  protected int m_cIndex=0;
  protected int m_sIndex=0;

  /** Holds the min and max values of the x, y and colouring attributes 
   over all plots */
  protected double m_maxX;
  protected double m_minX;
  protected double m_maxY;
  protected double m_minY;
  protected double m_maxC;
  protected double m_minC;
    
  /** Axis padding */
  protected final int m_axisPad = 5;

  /** Tick size */
  protected final int m_tickSize = 5;

  /**the offsets of the axes once label metrics are calculated */
  protected int m_XaxisStart=0;
  protected int m_YaxisStart=0;
  protected int m_XaxisEnd=0;
  protected int m_YaxisEnd=0;

  /** if the user resizes the window, or the attributes selected for
      the attributes change, then the lookup table for points needs
      to be recalculated */
  protected boolean m_plotResize = true;
  
  /** if the user changes attribute assigned to an axis */
  protected boolean m_axisChanged = false;

  /** An array used to show if a point is hidden or not.
   * This is used for speeding up the drawing of the plot panel
   * although I am not sure how much performance this grants over
   * not having it.
   */
  protected int[][] m_drawnPoints;

  /** Font for labels */
  protected Font m_labelFont;
  protected FontMetrics m_labelMetrics=null; 

  /** the level of jitter */
  protected int m_JitterVal=0;

  /** random values for perterbing the data points */
  protected Random m_JRand = new Random(0);

  /** lookup table for plotted points */
  protected double [][] m_pointLookup=null;

  /** Constructor */
  public Plot2D() {
    setProperties();
    this.setBackground(m_backgroundColour);
    m_InstanceInfoText.setFont(new Font("Monospaced", Font.PLAIN,12));
    m_InstanceInfoText.setEditable(false);

    m_drawnPoints = new int[this.getWidth()][this.getHeight()];

    /** Set up some default colours */
    m_colorList = new FastVector(10);
    for (int noa = m_colorList.size(); noa < 10; noa++) {
      Color pc = m_DefaultColors[noa % 10];
      int ija =  noa / 10;
      ija *= 2; 
      for (int j=0;j<ija;j++) {
	pc = pc.darker();
      }
      
      m_colorList.addElement(pc);
    }
  }

  /**
   * Set the properties for Plot2D
   */
  private void setProperties() {
    if (VisualizeUtils.VISUALIZE_PROPERTIES != null) {
      String thisClass = this.getClass().getName();
      String axisKey = thisClass+".axisColour";
      String backgroundKey = thisClass+".backgroundColour";

      String axisColour = VisualizeUtils.VISUALIZE_PROPERTIES.
	getProperty(axisKey);
      if (axisColour == null) {
	/*
	System.err.println("Warning: no configuration property found in "
			   +VisualizeUtils.PROPERTY_FILE
			   +" for "+axisKey);*/
      } else {
	//System.err.println("Setting axis colour to: "+axisColour);
	m_axisColour = VisualizeUtils.processColour(axisColour, m_axisColour);
      }

      String backgroundColour = 
	VisualizeUtils.VISUALIZE_PROPERTIES.getProperty(backgroundKey);
      if (backgroundColour == null) {
	/*
	System.err.println("Warning: no configuration property found in "
			   +VisualizeUtils.PROPERTY_FILE
			   +" for "+backgroundKey);*/
      } else {
	//System.err.println("Setting background colour to: "+backgroundColour);
	m_backgroundColour = VisualizeUtils.processColour(backgroundColour, 
							  m_backgroundColour);
      }
    }
  }

  /** 
   * This will check the values of the screen points passed and make sure 
   * that they land on the screen
   * @param x1 The x coord.
   * @param y1 The y coord.
   */
  private boolean checkPoints(double x1, double y1) {
    if (x1 < 0 || x1 > this.getSize().width || y1 < 0 
	|| y1 > this.getSize().height) {
      return false;
    }
    return true;
  }

  /**
   * Set a companion class. This is a class that might want
   * to render something on the plot before we do our thing. Eg,
   * Malcolm's shape drawing stuff needs to happen before we plot
   * axis and points
   * @param p a companion class
   */
  public void setPlotCompanion(Plot2DCompanion p) {
    m_plotCompanion = p;
  }

  /**
   * Set level of jitter and repaint the plot using the new jitter value
   * @param j the level of jitter
   */
  public void setJitter(int j) {
    if (m_plotInstances.numAttributes() > 0 
	&& m_plotInstances.numInstances() > 0) {
      if (j >= 0) {
	m_JitterVal = j;
	m_JRand = new Random(m_JitterVal);
	//      if (m_pointLookup != null) {
	m_drawnPoints = new int[m_XaxisEnd - m_XaxisStart + 1]
	  [m_YaxisEnd - m_YaxisStart + 1];
	updatePturb();
	//      }
	this.repaint();
      }
    }
  }

  /**
   * Set a list of colours to use when colouring points according
   * to class values or cluster numbers
   * @param cols the list of colours to use
   */
  public void setColours (FastVector cols) {
    m_colorList = cols;
  }

  /**
   * Set the index of the attribute to go on the x axis
   * @param x the index of the attribute to use on the x axis
   */
  public void setXindex(int x) {
    m_xIndex = x;
    for (int i=0;i<m_plots.size();i++) {
      ((PlotData2D)m_plots.elementAt(i)).setXindex(m_xIndex);
    }
    determineBounds();
    if (m_JitterVal != 0) {
      updatePturb();
    }
    m_axisChanged = true;
    this.repaint();
  }
    
  /**
   * Set the index of the attribute to go on the y axis
   * @param y the index of the attribute to use on the y axis
   */
  public void setYindex(int y) {
    m_yIndex = y;
    for (int i=0;i<m_plots.size();i++) {
      ((PlotData2D)m_plots.elementAt(i)).setYindex(m_yIndex);
    }
    determineBounds();
    if (m_JitterVal != 0) {
      updatePturb();
    }
    m_axisChanged = true;
    this.repaint();
  }

  /**
   * Set the index of the attribute to use for colouring
   * @param c the index of the attribute to use for colouring
   */
  public void setCindex(int c) {
    m_cIndex = c;
    for (int i=0;i<m_plots.size();i++) {
      ((PlotData2D)m_plots.elementAt(i)).setCindex(m_cIndex);
    }
    determineBounds();
    m_axisChanged = true;
    this.repaint();
  }

  /**
   * Return the list of plots
   * @return the list of plots
   */
  public FastVector getPlots() {
    return m_plots;
  }

  /**
   * Get the master plot
   * @return the master plot
   */
  public PlotData2D getMasterPlot() {
    return m_masterPlot;
  }

  /** 
   * Return the current max value of the attribute plotted on the x axis
   * @return the max x value
   */
  public double getMaxX() {
    return m_maxX;
  }

  /** 
   * Return the current max value of the attribute plotted on the y axis
   * @return the max y value
   */
  public double getMaxY() {
    return m_maxY;
  }

  /** 
   * Return the current min value of the attribute plotted on the x axis
   * @return the min x value
   */
  public double getMinX() {
    return m_minX;
  }
  
  /** 
   * Return the current min value of the attribute plotted on the y axis
   * @return the min y value
   */
  public double getMinY() {
    return m_minY;
  }

  /** 
   * Return the current max value of the colouring attribute
   * @return the max colour value
   */
  public double getMaxC() {
    return m_maxC;
  }
  
  /** 
   * Return the current min value of the colouring attribute
   * @return the min colour value
   */
  public double getMinC() {
    return m_minC;
  }
    
  /**
   * Sets the master plot from a set of instances
   * @param inst the instances
   * @exception exception Exception if instances could not be set
   */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -