📄 attributepanel.java
字号:
/*
* 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.
*/
/*
* AttributePanel.java
* Copyright (C) 1999 Malcolm Ware, Mark Hall
*
*/
package weka.gui.visualize;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instances;
/**
* This panel displays one dimensional views of the attributes in a
* dataset. Colouring is done on the basis of a column in the dataset or
* an auxiliary array (useful for colouring cluster predictions).
*
* @author Malcolm Ware (mfw4@cs.waikato.ac.nz)
* @author Mark Hall (mhall@cs.waikato.ac.nz)
* @version $Revision$
*/
public class AttributePanel extends JScrollPane {
/** The instances to be plotted */
protected Instances m_plotInstances=null;
/** Holds the min and max values of the colouring attributes */
protected double m_maxC;
protected double m_minC;
protected int m_cIndex;
protected int m_xIndex;
protected int m_yIndex;
/** The colour map to use for colouring points */
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};
/** The list of things listening to this panel */
protected FastVector m_Listeners = new FastVector();
/** Holds the random height for each instance. */
protected int[] m_heights;
//protected Color[] colors_array;
/** The container window for the attribute bars, and also where the
* X,Y or B get printed.
*/
protected JPanel m_span=null;
/** The default colour to use for the background of the bars if
a colour is not defined in Visualize.props */
protected Color m_barColour=Color.black;
/** inner inner class used for plotting the points
* into a bar for a particular attribute.
*/
protected class AttributeSpacing extends JPanel {
/** The min and max values for this attribute. */
protected double m_maxVal;
protected double m_minVal;
/** The attribute itself. */
protected Attribute m_attrib;
/** The index for this attribute. */
protected int m_attribIndex;
/** The x position of each point. */
protected int[] m_cached;
//note for m_cached, if you wanted to speed up the drawing algorithm
// and save memory, the system could be setup to drop out any
// of the instances not being drawn (you would need to find a new way
//of matching the height however).
/** A temporary array used to strike any instances that would be
* drawn redundantly.
*/
protected boolean[][] m_pointDrawn;
/** Used to determine if the positions need to be recalculated. */
protected int m_oldWidth=-9000;
/** The container window for the attribute bars, and also where the
* X,Y or B get printed.
*/
/**
* This constructs the bar with the specified attribute and
* sets its index to be used for selecting by the mouse.
* @param a The attribute this bar represents.
* @param aind The index of this bar.
*/
public AttributeSpacing(Attribute a, int aind) {
m_attrib = a;
m_attribIndex = aind;
this.setBackground(m_barColour);
this.setPreferredSize(new Dimension(0, 20));
this.setMinimumSize(new Dimension(0, 20));
m_cached = new int[m_plotInstances.numInstances()];
//this will only get allocated if m_plotInstances != null
//this is used to determine the min and max values for plotting
double min=Double.POSITIVE_INFINITY;
double max=Double.NEGATIVE_INFINITY;
double value;
if (m_plotInstances.attribute(m_attribIndex).isNominal()) {
m_minVal = 0;
m_maxVal = m_plotInstances.attribute(m_attribIndex).numValues()-1;
} else {
for (int i=0;i<m_plotInstances.numInstances();i++) {
if (!m_plotInstances.instance(i).isMissing(m_attribIndex)) {
value = m_plotInstances.instance(i).value(m_attribIndex);
if (value < min) {
min = value;
}
if (value > max) {
max = value;
}
}
}
m_minVal = min; m_maxVal = max;
if (min == max) {
m_maxVal += 0.05;
m_minVal -= 0.05;
}
}
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if ((e.getModifiers() & e.BUTTON1_MASK) == e.BUTTON1_MASK) {
setX(m_attribIndex);
if (m_Listeners.size() > 0) {
for (int i=0;i<m_Listeners.size();i++) {
AttributePanelListener l =
(AttributePanelListener)(m_Listeners.elementAt(i));
l.attributeSelectionChange(new AttributePanelEvent(true,
false, m_attribIndex));
}
}
}
else {
//put it on the y axis
setY(m_attribIndex);
if (m_Listeners.size() > 0) {
for (int i=0;i<m_Listeners.size();i++) {
AttributePanelListener l =
(AttributePanelListener)(m_Listeners.elementAt(i));
l.attributeSelectionChange(new AttributePanelEvent(false,
true, m_attribIndex));
}
}
}
}
});
}
/**
* Convert an raw x value to Panel x coordinate.
* @param val the raw x value
* @return an x value for plotting in the panel.
*/
private double convertToPanel(double val) {
double temp = (val - m_minVal)/(m_maxVal - m_minVal);
double temp2 = temp * (this.getWidth() - 10);
return temp2 + 4;
}
/**
* paints all the visible instances to the panel , and recalculates
* their position if need be.
* @param gx The graphics context.
*/
public void paintComponent(Graphics gx) {
super.paintComponent(gx);
int xp, yp, h;
h = this.getWidth();
if (m_plotInstances != null
&& m_plotInstances.numAttributes() > 0
&& m_plotInstances.numInstances() > 0) {
if (m_oldWidth != h) {
m_pointDrawn = new boolean[h][20];
for (int noa = 0; noa < m_plotInstances.numInstances(); noa++) {
if (!m_plotInstances.instance(noa).isMissing(m_attribIndex)
&& !m_plotInstances.instance(noa).isMissing(m_cIndex)) {
m_cached[noa] = (int)convertToPanel(m_plotInstances.
instance(noa).
value(m_attribIndex));
if (m_pointDrawn[m_cached[noa] % h][m_heights[noa]]) {
m_cached[noa] = -9000;
}
else {
m_pointDrawn[m_cached[noa]%h][m_heights[noa]] = true;
}
}
else {
m_cached[noa] = -9000; //this value will not happen
//so it is safe
}
}
m_oldWidth = h;
}
if (m_plotInstances.attribute(m_cIndex).isNominal()) {
for (int noa = 0; noa < m_plotInstances.numInstances(); noa++) {
if (m_cached[noa] != -9000) {
xp = m_cached[noa];
yp = m_heights[noa];
if (m_plotInstances.attribute(m_attribIndex).
isNominal()) {
xp += (int)(Math.random() * 5) - 2;
}
int ci = (int)m_plotInstances.instance(noa).value(m_cIndex);
gx.setColor((Color)m_colorList.elementAt
(ci % m_colorList.size()));
gx.drawRect(xp, yp, 1, 1);
}
}
}
else {
double r;
for (int noa = 0; noa < m_plotInstances.numInstances(); noa++) {
if (m_cached[noa] != -9000) {
r = (m_plotInstances.instance(noa).value(m_cIndex)
- m_minC) / (m_maxC - m_minC);
r = (r * 240) + 15;
gx.setColor(new Color((int)r,150,(int)(255-r)));
xp = m_cached[noa];
yp = m_heights[noa];
if (m_plotInstances.attribute(m_attribIndex).
isNominal()) {
xp += (int)(Math.random() * 5) - 2;
}
gx.drawRect(xp, yp, 1, 1);
}
}
}
}
}
}
/**
* Set the properties for the AttributePanel
*/
private void setProperties() {
if (VisualizeUtils.VISUALIZE_PROPERTIES != null) {
String thisClass = this.getClass().getName();
String barKey = thisClass+".barColour";
String barC = VisualizeUtils.VISUALIZE_PROPERTIES.
getProperty(barKey);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -