📄 attributevisualizationpanel.java
字号:
y = y-Math.round(m_histBarClassCounts[i][j]*heightRatio);
//selecting the colour corresponding to the current class.
g.setColor( (Color)m_colorList.elementAt(j) );
g.fillRect(x, y, barWidth,
Math.round(m_histBarClassCounts[i][j]*heightRatio));
g.setColor(Color.black);
}
//drawing the bar count at the top of the bar if it is less than
//interval width. draw it 1px up to avoid touching the bar.
if(m_fm.stringWidth(Integer.toString(sum))<intervalWidth)
g.drawString(Integer.toString(sum), x, y-1);
//advancing x to the next bar by adding bar width and padding
//of both the bars (i.e. RHS padding of the bar just drawn and LHS
//padding of the new bar).
x = x+barWidth+(int)( (Math.floor(intervalWidth*0.2F))<1 ?
1:(Math.floor(intervalWidth*0.2F)) );
//reseting sum for the next bar.
sum=0;
}
}
//else if class attribute is numeric or not set then draw black bars.
else {
intervalWidth = (this.getWidth()/(float)m_histBarCounts.length);
//same as in the case of nominal class (see inside of if stmt
//corresponding to the current else above).
if(intervalWidth>5)
barWidth = (int)Math.floor(intervalWidth*0.8F);
else
barWidth = 1;
//same as in the case of nominal class (see inside of if stmt
//corresponding to the current else above).
x = x + (int)( (Math.floor(intervalWidth*0.1F))<1 ?
1:(Math.floor(intervalWidth*0.1F)) );
//same as in the case of nominal class
if( this.getWidth() - (m_histBarCounts.length*barWidth+
(int)( (Math.floor(intervalWidth*0.2F))<1 ?
1:(Math.floor(intervalWidth*0.2F)) ) *
m_histBarCounts.length) > 2 ) {
x += (this.getWidth() -(m_histBarCounts.length*barWidth +
(int)((Math.floor(intervalWidth*0.2F))<1 ?
1:(Math.floor(intervalWidth*0.2F)))*
m_histBarCounts.length))/2;
}
for(int i=0; i<m_histBarCounts.length; i++) {
//calculating the proportion of the height of the component
//compared to the maxValue in our attribute.
heightRatio = (this.getHeight()-(float)m_fm.getHeight() -
buttonHeight) / m_maxValue;
y = this.getHeight()-Math.round(m_histBarCounts[i]*heightRatio);
g.fillRect(x, y, barWidth,
Math.round(m_histBarCounts[i]*heightRatio));
//draw the bar count if it's width is smaller than intervalWidth.
//draw it 1px above to avoid touching the bar.
if(m_fm.stringWidth(Integer.toString(m_histBarCounts[i])) <
intervalWidth)
g.drawString(Integer.toString(m_histBarCounts[i]), x, y-1);
//Advance x to the next bar by adding bar-width and padding
//of the bars (RHS padding of current bar & LHS padding of next
//bar).
x = x+barWidth+(int)( (Math.floor(intervalWidth*0.2F))<1 ?
1:(Math.floor(intervalWidth*0.2F)) );
}
}
} //<--end if m_as.nominalCount!=null
//if the current attribute is numeric then draw a histogram.
else if(m_as.numericStats != null &&
(m_histBarClassCounts!=null || m_histBarCounts!=null)) {
float heightRatio, intervalWidth;
int x=0, y=0, barWidth;
//If the class attribute is set and is not numeric then draw coloured
//subbars for the histogram bars
if((m_classIndex >=0) &&
(m_data.attribute(m_classIndex).isNominal())) {
//There is a padding of 3px on each side of the histogram.
barWidth = ((this.getWidth()-6)/m_histBarClassCounts.length)<1 ?
1 : ((this.getWidth()-6)/m_histBarClassCounts.length);
//initializing x to start at the start of the 1st bar after padding.
x = 3;
//Adding appropriate value to x to account for a "centered"
//histogram
if( (this.getWidth() -
(x + m_histBarClassCounts.length*barWidth)) > 5 ) {
//We take the current value of x (histogram's RHS padding) and add
//the barWidths of all the bars to it to us the size of
//our histogram. We subtract that from the width of the panel
//giving us the extra space that would be left if the histogram is
//drawn and divide that by 2 to get the midpoint of that extra
//space. That space is then added to our x, hence making the
//histogram centered.
x += ( this.getWidth() -
(x + m_histBarClassCounts.length*barWidth) ) / 2;
}
for(int i=0; i<m_histBarClassCounts.length; i++) {
//Calculating height ratio. Leave space of 19 for an axis line at
//the bottom
heightRatio = (this.getHeight()-(float)m_fm.getHeight() -
buttonHeight-19) / m_maxValue;
y = this.getHeight()-19;
//This would hold the count of the bar (sum of sub-bars).
int sum = 0;
for(int j=0; j<m_histBarClassCounts[i].length; j++) {
y = y-Math.round(m_histBarClassCounts[i][j]*heightRatio);
//System.out.println("Filling x:"+x+" y:"+y+" width:"+barWidth+
// " height:"+
// (m_histBarClassCounts[i][j]*heightRatio));
//selecting the color corresponding to our class
g.setColor( (Color)m_colorList.elementAt(j) );
//drawing the bar if its width is greater than 1
if(barWidth>1)
g.fillRect(x, y,
barWidth,
Math.round(m_histBarClassCounts[i][j]*heightRatio));
//otherwise drawing a line
else if((m_histBarClassCounts[i][j]*heightRatio)>0)
g.drawLine(x, y, x,
y+Math.round(m_histBarClassCounts[i][j]*heightRatio));
g.setColor(Color.black);
sum = sum + m_histBarClassCounts[i][j];
}
//Drawing bar count on the top of the bar if it is < barWidth
if(m_fm.stringWidth(" "+Integer.toString(sum))<barWidth)
g.drawString(" "+Integer.toString(sum), x, y-1);
//Moving x to the next bar
x = x+barWidth;
}
//Now drawing the axis line at the bottom of the histogram
//initializing x again to the start of the plot
x = 3;
if( (this.getWidth() -
(x + m_histBarClassCounts.length*barWidth)) > 5 )
x += (this.getWidth() -
(x + m_histBarClassCounts.length*barWidth))/2;
g.drawLine(x, this.getHeight()-17,
(barWidth==1)?x+barWidth*m_histBarClassCounts.length-1 :
x+barWidth*m_histBarClassCounts.length,
this.getHeight()-17); //axis line -- see footnote 2.
g.drawLine(x, this.getHeight()-16,
x, this.getHeight()-12); //minimum line
g.drawString(Utils.doubleToString(m_as.numericStats.min, 2),
x,
this.getHeight()-12+m_fm.getHeight()); //minimum value
g.drawLine(x+(barWidth*m_histBarClassCounts.length)/2,
this.getHeight()-16,
x+(barWidth*m_histBarClassCounts.length)/2,
this.getHeight()-12); //median line
//Drawing median value. X position for drawing the value is: from
//start of the plot take the mid point and subtract from it half
//of the width of the value to draw.
g.drawString(Utils.doubleToString(m_as.numericStats.max/2+m_as.numericStats.min/2, 2),
x+(barWidth*m_histBarClassCounts.length)/2 -
m_fm.stringWidth(Utils.doubleToString(m_as.numericStats.max/2+m_as.numericStats.min/2, 2))/2,
this.getHeight()-12+m_fm.getHeight()); //median value
g.drawLine((barWidth==1) ? x+barWidth*m_histBarClassCounts.length-1:
x+barWidth*m_histBarClassCounts.length,
this.getHeight()-16,
(barWidth==1) ? x+barWidth*m_histBarClassCounts.length-1:
x+barWidth*m_histBarClassCounts.length,
this.getHeight()-12); //maximum line
g.drawString(Utils.doubleToString(m_as.numericStats.max, 2),
(barWidth==1) ?
x+barWidth*m_histBarClassCounts.length-m_fm.stringWidth(Utils.doubleToString(m_as.numericStats.max, 2))-1:
x+barWidth*m_histBarClassCounts.length-m_fm.stringWidth(Utils.doubleToString(m_as.numericStats.max, 2)),
this.getHeight()-12+m_fm.getHeight()); //maximum value -- see 2.
}
else { //if class attribute is numeric
//There is a padding of 3px on each side of the histogram.
barWidth = ((this.getWidth()-6)/m_histBarCounts.length) < 1 ?
1:((this.getWidth()-6)/m_histBarCounts.length);
//Same as above. Pls inside of the if stmt.
x = 3;
if( (this.getWidth() - (x + m_histBarCounts.length*barWidth)) > 5 )
x += (this.getWidth() - (x + m_histBarCounts.length*barWidth))/2;
//Same as above
for(int i=0; i<m_histBarCounts.length; i++) {
//calculating the ration of the component's height compared to
//the maxValue in our current attribute. Leaving 19 pixels to
//draw the axis at the bottom of the histogram.
heightRatio = (this.getHeight()-(float)m_fm.getHeight() -
buttonHeight-19) / m_maxValue;
y = this.getHeight() -
Math.round(m_histBarCounts[i]*heightRatio)-19;
//System.out.println("Filling x:"+x+" y:"+y+" width:"+barWidth+
// " height:"+(m_histBarCounts[i]*heightRatio));
//same as in the if stmt above
if(barWidth>1)
g.drawRect(x, y, barWidth,
Math.round(m_histBarCounts[i]*heightRatio));
else if((m_histBarCounts[i]*heightRatio)>0)
g.drawLine(x, y,
x, y+Math.round(m_histBarCounts[i]*heightRatio));
if(m_fm.stringWidth(" "+Integer.toString(m_histBarCounts[i])) <
barWidth)
g.drawString(" "+Integer.toString(m_histBarCounts[i]), x, y-1);
x = x+barWidth;
}
//Now drawing the axis at the bottom of the histogram
x = 3;
if( (this.getWidth() - (x + m_histBarCounts.length*barWidth)) > 5 )
x += (this.getWidth() - (x + m_histBarCounts.length*barWidth))/2;
//This is exact the same as in the if stmt above. See the inside of
//the stmt for details
g.drawLine(x, this.getHeight()-17,
(barWidth==1) ? x+barWidth*m_histBarCounts.length-1 :
x+barWidth*m_histBarCounts.length,
this.getHeight()-17); //axis line
g.drawLine(x, this.getHeight()-16,
x, this.getHeight()-12); //minimum line
g.drawString(Utils.doubleToString(m_as.numericStats.min, 2),
x,
this.getHeight()-12+m_fm.getHeight()); //minimum value
g.drawLine(x+(barWidth*m_histBarCounts.length)/2,
this.getHeight()-16,
x+(barWidth*m_histBarCounts.length)/2,
this.getHeight()-12); //median line
g.drawString(Utils.doubleToString(m_as.numericStats.max/2+m_as.numericStats.min/2, 2),
x+(barWidth*m_histBarCounts.length)/2 -
m_fm.stringWidth(Utils.doubleToString(m_as.numericStats.max/2+m_as.numericStats.min/2, 2))/2,
this.getHeight()-12+m_fm.getHeight()); //median value
g.drawLine((barWidth==1) ? x+barWidth*m_histBarCounts.length-1 :
x+barWidth*m_histBarCounts.length,
this.getHeight()-16,
(barWidth==1) ? x+barWidth*m_histBarCounts.length-1 :
x+barWidth*m_histBarCounts.length,
this.getHeight()-12); //maximum line
g.drawString(Utils.doubleToString(m_as.numericStats.max, 2),
(barWidth==1) ?
x+barWidth*m_histBarCounts.length-m_fm.stringWidth(Utils.doubleToString(m_as.numericStats.max, 2))-1 :
x+barWidth*m_histBarCounts.length-m_fm.stringWidth(Utils.doubleToString(m_as.numericStats.max, 2)),
this.getHeight()-12+m_fm.getHeight()); //maximum value
}
//System.out.println("barWidth:"+barWidth+
// " histBarCount:"+m_histBarCounts.length);
} else {
g.clearRect(0, 0, this.getWidth(), this.getHeight());
g.drawString("Attribute is neither numeric nor nominal.",
this.getWidth()/2 - m_fm.
stringWidth("Attribute is neither numeric nor nominal.")/2,
this.getHeight()/2-m_fm.getHeight()/2);
}
} //<--end if of calculation thread
else { //if still calculation thread is running plot
g.clearRect(0, 0, this.getWidth(), this.getHeight());
g.drawString("Calculating. Please Wait...",
this.getWidth()/2 - m_fm.stringWidth("Calculating. Please Wait...")/2,
this.getHeight()/2-m_fm.getHeight()/2);
}
} //<--end if(m_as==null) this means
}
/**
* Main method to test this class from command line
*
* @param args The arff file and the index of the attribute to use
*/
public static void main(String [] args) {
if(args.length!=3) {
final JFrame jf = new JFrame("AttribVisualization");
AttributeVisualizationPanel ap = new AttributeVisualizationPanel();
try {
Instances ins = new Instances( new FileReader(args[0]) );
ap.setInstances(ins);
System.out.println("Loaded: "+args[0]+
"\nRelation: "+ap.m_data.relationName()+
"\nAttributes: "+ap.m_data.numAttributes());
ap.setAttribute( Integer.parseInt(args[1]) );
}
catch(Exception ex) { ex.printStackTrace(); System.exit(-1); }
System.out.println("The attributes are: ");
for(int i=0; i<ap.m_data.numAttributes(); i++)
System.out.println(ap.m_data.attribute(i).name());
jf.setSize(500, 300);
jf.getContentPane().setLayout( new BorderLayout() );
jf.getContentPane().add(ap, BorderLayout.CENTER );
jf.setDefaultCloseOperation( jf.EXIT_ON_CLOSE );
jf.setVisible(true);
}
else
System.out.println("Usage: java AttributeVisualizationPanel"+
" [arff file] [index of attribute]");
}
}
/*
* t =(int) Math.ceil((float)(
* (m_data.instance(k).value(m_attribIndex)-m_as.numericStats.min)
* / barRange));
* 1.
* This equation gives a value between (i-1)+smallfraction and i if the
* attribute m_attribIndex for the current instances lies in the ith
* interval. We then increment the value of our i-1th field of our
* histogram/barplot array.
* If, for example, barRange=3 then, apart from the 1st
* interval, each interval i has values in the range
* (minValue+3*i-1, minValue+3*i]. The 1st interval has range
* [minValue, minValue+i]. Hence it can be seen in the code we specifically
* handle t=0 separately.
*
*/
/**
* (barWidth==1)?x+barWidth*m_histBarClassCounts.length-1 :
* x+barWidth*m_histBarClassCounts.length
* 2.
* In the case barWidth==1 we subtract 1 otherwise the line become one pixel
* longer than the actual size of the histogram
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -