📄 attributeviewpanel.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.
*/
/*
* Created on 2005-1-20
*
*/
package eti.bi.alphaminer.operation.result.view;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import weka.core.AttributeStats;
import weka.core.Instances;
import weka.core.Utils;
/**
* Creates a panel that shows a visualization of an
* attribute in a dataset. For nominal attribute it
* shows a bar plot, with each bar corresponding to
* each nominal value of the attribute with its height
* equal to the number of records in the dataset.
* For numeric attributes, it shows a bar corresponding
* to its mean value.
*
* @author TWang Jan 20, 2005.
*/
public class AttributeViewPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = -5542035327493153075L;
/** store weka instances and attribute status */
private Instances m_data;
private AttributeStats m_AttributeStats;
/** The maximum value of this attribute
* For the categorical attribute, it is the number of instances in the largest category.
* Note: m_MaxNumValue is alway >=0. If < 0, it will be treated as 0.
* */
private double m_MaxNumValue;
private int m_BarHeight = 0;
private int m_BarWidth = 0;
private int m_AttribIndex = 0;
/** default colour list */
private static final Color [] m_defaultColors = {new Color(0, 200, 6),
Color.red,
Color.cyan,
Color.green,
Color.pink,
Color.orange,
new Color(75, 123, 130),
new Color(255, 0, 255),
new Color(255, 0, 0),
Color.blue,
};
/**The maximum number of categorical levels are 10. If more, only first 10 levels are displayed. */
private static int m_minWidthPerBar = 5;
private static int m_maxDisplayCatNumber = 10;
private int m_ColorNum = 0;
/**
* Sets the instances for use
*
* @param a_Newins a set of WEKA Instances
*/
public void setInstances(Instances a_Newins) {
m_AttribIndex = 0;
m_data = a_Newins;
m_ColorNum = m_defaultColors.length;
m_AttributeStats = null;
}
/**
* Input the index of attribute, and the maxValue in this attribute.
* If this is a categorical attribute, input the records number in the largest the category
* @param a_Index
* @param a_MaxVal
*/
public void viewAttribute(int a_Index, double a_MaxVal) {
m_AttribIndex = a_Index;
m_MaxNumValue = a_MaxVal;
if(m_data != null){
m_AttributeStats = m_data.attributeStats(m_AttribIndex);
}
this.repaint();
}
/**
* Paints this component
*
* @param g The graphics object for this component
*/
public void paintComponent(Graphics g) {
g.clearRect(0,0,this.getWidth(), this.getHeight());
int x = 0;
int y = 0;
if(m_data != null && m_AttributeStats!=null) {
if( m_data.attribute(m_AttribIndex).isNumeric()) {
m_BarWidth = (int) (this.getWidth() / 2);
if(m_MaxNumValue > 0){
m_BarHeight = (int) (m_AttributeStats.numericStats.mean * (this.getHeight() - 20) / m_MaxNumValue );
}else{
m_BarHeight = 0;
}
x = (int) (this.getWidth()/2 - m_BarWidth / 2) ;
y = this.getHeight() - m_BarHeight -2;
// draw the mean value at the top of the bar
g.drawString(Utils.doubleToString(m_AttributeStats.numericStats.mean, 3), x, y-3);
g.setColor(m_defaultColors[m_ColorNum - 1]);
g.fillRect(x, y, m_BarWidth, m_BarHeight);
g.setColor(Color.black);
}
else if (m_data.attribute(m_AttribIndex).isNominal()){
int catNum = Math.min(m_data.attribute(m_AttribIndex).numValues(), m_maxDisplayCatNumber);
m_BarWidth = Math.max( (int) (this.getWidth() / (catNum + 1)), m_minWidthPerBar);
for (int i = 0; i< catNum; i++){
if(m_MaxNumValue > 0){
m_BarHeight = (int) (m_AttributeStats.nominalCounts[i] * (this.getHeight() - 20) / m_MaxNumValue );
}else{
m_BarHeight = 0;
}
x = (int) (this.getWidth()/2 - catNum * m_BarWidth / 2) + i * m_BarWidth;
y = this.getHeight() - m_BarHeight - 2; //leave space to draw a base line
// draw the records number at the top of the bar
g.drawString(Integer.toString(m_AttributeStats.nominalCounts[i]), x+2, y-3);
g.setColor(m_defaultColors[i % m_ColorNum]);
g.fillRect(x, y, m_BarWidth, m_BarHeight);
g.setColor(Color.black);
}
}
// draw the base line
g.setColor(Color.black);
g.drawLine(5, y + m_BarHeight, this.getWidth() - 5, y + m_BarHeight);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -