📄 plot2d.java
字号:
public void setInstances(Instances inst) throws Exception {
//System.err.println("Setting Instances");
PlotData2D tempPlot = new PlotData2D(inst);
tempPlot.setPlotName("master plot");
setMasterPlot(tempPlot);
}
/**
* Set the master plot.
* @param master the plot to make the master plot
* @exception Exception if the plot could not be set.
*/
public void setMasterPlot(PlotData2D master) throws Exception {
if (master.m_plotInstances == null) {
throw new Exception("No instances in plot data!");
}
removeAllPlots();
m_masterPlot = master;
m_plots.addElement(m_masterPlot);
m_plotInstances = m_masterPlot.m_plotInstances;
m_xIndex=0;
m_yIndex=0;
m_cIndex=0;
determineBounds();
}
/**
* Clears all plots
*/
public void removeAllPlots() {
m_masterPlot = null;
m_plotInstances = null;
m_plots = new FastVector();
m_xIndex = 0; m_yIndex = 0; m_cIndex = 0;
}
/**
* Add a plot to the list of plots to display
* @param newPlot the new plot to add
* @exception Exception if the plot could not be added
*/
public void addPlot(PlotData2D newPlot) throws Exception {
if (newPlot.m_plotInstances == null) {
throw new Exception("No instances in plot data!");
}
if (m_masterPlot != null) {
if (m_masterPlot.m_plotInstances.
equalHeaders(newPlot.m_plotInstances) == false) {
throw new Exception("Plot2D :Plot data's instances are incompatable "
+" with master plot");
}
} else {
m_masterPlot = newPlot;
m_plotInstances = m_masterPlot.m_plotInstances;
}
m_plots.addElement(newPlot);
setXindex(m_xIndex);
setYindex(m_yIndex);
setCindex(m_cIndex);
}
/**
* Set up fonts and font metrics
* @param gx the graphics context
*/
private void setFonts(Graphics gx) {
if (m_labelMetrics == null) {
m_labelFont = new Font("Monospaced", Font.PLAIN, 12);
m_labelMetrics = gx.getFontMetrics(m_labelFont);
}
gx.setFont(m_labelFont);
}
/**
* Pops up a window displaying attribute information on any instances
* at a point+-plotting_point_size (in panel coordinates)
*
* @param x the x value of the clicked point
* @param y the y value of the clicked point
* @param newFrame true if instance info is to be displayed in a
* new frame.
*/
public void searchPoints(int x, int y, final boolean newFrame) {
if (m_masterPlot.m_plotInstances != null) {
int longest=0;
for (int j=0;j<m_masterPlot.m_plotInstances.numAttributes();j++) {
if (m_masterPlot.m_plotInstances.attribute(j).name().length() >
longest) {
longest = m_masterPlot.m_plotInstances.attribute(j).name().length();
}
}
StringBuffer insts = new StringBuffer();
for (int jj=0;jj<m_plots.size();jj++) {
PlotData2D temp_plot = (PlotData2D)(m_plots.elementAt(jj));
for (int i=0;i<temp_plot.m_plotInstances.numInstances();i++) {
if (temp_plot.m_pointLookup[i][0] != Double.NEGATIVE_INFINITY) {
double px = temp_plot.m_pointLookup[i][0] +
temp_plot.m_pointLookup[i][2];
double py = temp_plot.m_pointLookup[i][1] +
temp_plot.m_pointLookup[i][3];
// double size = temp_plot.m_pointLookup[i][2];
double size = temp_plot.m_shapeSize[i];
if ((x >= px-size) && (x <= px+size) &&
(y >= py-size) && (y <= py+size)) {
{
insts.append("\nPlot : "+temp_plot.m_plotName
+"\nInstance: "+i+"\n");
for (int j=0;j<temp_plot.m_plotInstances.numAttributes();j++) {
for (int k = 0;k <
(longest-temp_plot.m_plotInstances.
attribute(j).name().length()); k++) {
insts.append(" ");
}
insts.append(temp_plot.m_plotInstances.attribute(j).name());
insts.append(" : ");
if (temp_plot.m_plotInstances.instance(i).isMissing(j)) {
insts.append("Missing");
} else if (temp_plot.m_plotInstances.attribute(j).
isNominal()) {
insts.append(temp_plot.m_plotInstances.
attribute(j).
value((int)temp_plot.m_plotInstances.
instance(i).value(j)));
} else {
insts.append(temp_plot.m_plotInstances.
instance(i).value(j));
}
insts.append("\n");
}
}
}
}
}
}
if (insts.length() > 0) {
// Pop up a new frame
if (newFrame || m_InstanceInfo == null) {
JTextArea jt = new JTextArea();
jt.setFont(new Font("Monospaced", Font.PLAIN,12));
jt.setEditable(false);
jt.setText(insts.toString());
final JFrame jf = new JFrame("Weka : Instance info");
final JFrame testf = m_InstanceInfo;
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (!newFrame || testf == null) {
m_InstanceInfo = null;
}
jf.dispose();
}
});
jf.getContentPane().setLayout(new BorderLayout());
jf.getContentPane().add(new JScrollPane(jt), BorderLayout.CENTER);
jf.pack();
jf.setSize(320, 400);
jf.setVisible(true);
if (m_InstanceInfo == null) {
m_InstanceInfo = jf;
m_InstanceInfoText = jt;
}
} else {
// Overwrite info in existing frame
m_InstanceInfoText.setText(insts.toString());
}
}
}
}
/**
* Determine the min and max values for axis and colouring attributes
*/
public void determineBounds() {
double value,min,max;
// find maximums minimums over all plots
m_minX = ((PlotData2D)m_plots.elementAt(0)).m_minX;
m_maxX = ((PlotData2D)m_plots.elementAt(0)).m_maxX;
m_minY = ((PlotData2D)m_plots.elementAt(0)).m_minY;
m_maxY = ((PlotData2D)m_plots.elementAt(0)).m_maxY;
m_minC = ((PlotData2D)m_plots.elementAt(0)).m_minC;
m_maxC = ((PlotData2D)m_plots.elementAt(0)).m_maxC;
for (int i=1;i<m_plots.size();i++) {
value = ((PlotData2D)m_plots.elementAt(i)).m_minX;
if (value < m_minX) {
m_minX = value;
}
value = ((PlotData2D)m_plots.elementAt(i)).m_maxX;
if (value > m_maxX) {
m_maxX = value;
}
value = ((PlotData2D)m_plots.elementAt(i)).m_minY;
if (value < m_minY) {
m_minY= value;
}
value = ((PlotData2D)m_plots.elementAt(i)).m_maxY;
if (value > m_maxY) {
m_maxY = value;
}
value = ((PlotData2D)m_plots.elementAt(i)).m_minC;
if (value < m_minC) {
m_minC = value;
}
value = ((PlotData2D)m_plots.elementAt(i)).m_maxC;
if (value > m_maxC) {
m_maxC = value;
}
}
fillLookup();
this.repaint();
}
//to convert screen coords to attrib values
// note that I use a double to avoid accuracy
//headaches with ints
/**
* convert a Panel x coordinate to a raw x value.
* @param scx The Panel x coordinate
* @return A raw x value.
*/
public double convertToAttribX(double scx) {
double temp = m_XaxisEnd - m_XaxisStart;
double temp2 = ((scx - m_XaxisStart) * (m_maxX - m_minX)) / temp;
temp2 = temp2 + m_minX;
return temp2;
}
/**
* convert a Panel y coordinate to a raw y value.
* @param scy The Panel y coordinate
* @return A raw y value.
*/
public double convertToAttribY(double scy) {
double temp = m_YaxisEnd - m_YaxisStart;
double temp2 = ((scy - m_YaxisEnd) * (m_maxY - m_minY)) / temp;
temp2 = -(temp2 - m_minY);
return temp2;
}
//////
/**
* returns a value by which an x value can be peturbed. Makes sure
* that the x value+pturb stays within the plot bounds
* @param xvalP the x coordinate to be peturbed
* @param xj a random number to use in calculating a peturb value
* @return a peturb value
*/
int pturbX(double xvalP, double xj) {
int xpturb = 0;
if (m_JitterVal > 0) {
xpturb = (int)((double)m_JitterVal * (xj / 2.0));
if (((xvalP + xpturb) < m_XaxisStart) ||
((xvalP + xpturb) > m_XaxisEnd)) {
xpturb *= -1;
}
}
return xpturb;
}
/**
* Convert an raw x value to Panel x coordinate.
* @param xval the raw x value
* @return an x value for plotting in the panel.
*/
public double convertToPanelX(double xval) {
double temp = (xval - m_minX)/(m_maxX - m_minX);
double temp2 = temp * (m_XaxisEnd - m_XaxisStart);
temp2 = temp2 + m_XaxisStart;
return temp2;
}
/**
* returns a value by which a y value can be peturbed. Makes sure
* that the y value+pturb stays within the plot bounds
* @param yvalP the y coordinate to be peturbed
* @param yj a random number to use in calculating a peturb value
* @return a peturb value
*/
int pturbY(double yvalP, double yj) {
int ypturb = 0;
if (m_JitterVal > 0) {
ypturb = (int)((double)m_JitterVal * (yj / 2.0));
if (((yvalP + ypturb) < m_YaxisStart) ||
((yvalP + ypturb) > m_YaxisEnd)) {
ypturb *= -1;
}
}
return ypturb;
}
/**
* Convert an raw y value to Panel y coordinate.
* @param yval the raw y value
* @return an y value for plotting in the panel.
*/
public double convertToPanelY(double yval) {
double temp = (yval - m_minY)/(m_maxY - m_minY);
double temp2 = temp * (m_YaxisEnd - m_YaxisStart);
temp2 = m_YaxisEnd - temp2;
return temp2;
}
/**
* Draws an X.
* @param gx the graphics context
* @param x the x coord
* @param y the y coord
* @param size the size of the shape
*/
private static void drawX(Graphics gx, double x, double y, int size) {
gx.drawLine((int)(x-size),(int)(y-size),
(int)(x+size),(int)(y+size));
gx.drawLine((int)(x+size),(int)(y-size),
(int)(x-size),(int)(y+size));
}
/**
* Draws a plus.
* @param gx the graphics context
* @param x the x coord
* @param y the y coord
* @param size the size of the shape
*/
private static void drawPlus(Graphics gx, double x, double y, int size) {
gx.drawLine((int)(x-size),(int)(y),
(int)(x+size),(int)(y));
gx.drawLine((int)(x),(int)(y-size),
(int)(x),(int)(y+size));
}
/**
* Draws a diamond.
* @param gx the graphics context
* @param x the x coord
* @param y the y coord
* @param size the size of the shape
*/
private static void drawDiamond(Graphics gx, double x, double y, int size) {
gx.drawLine((int)(x-size),(int)(y),
(int)(x),(int)(y-size));
gx.drawLine((int)(x),(int)(y-size),
(int)(x+size),(int)(y));
gx.drawLine((int)(x+size),(int)(y),
(int)(x),(int)(y+size));
gx.drawLine((int)(x),(int)(y+size),
(int)(x-size),(int)(y));
}
/**
* Draws an triangle (point at top).
* @param gx the graphics context
* @param x the x coord
* @param y the y coord
* @param size the size of the shape
*/
private static void drawTriangleUp(Graphics gx, double x,
double y, int size) {
gx.drawLine((int)(x),(int)(y-size),
(int)(x-size),(int)(y+size));
gx.drawLine((int)(x-size),(int)(y+size),
(int)(x+size),(int)(y+size));
gx.drawLine((int)(x+size),(int)(y+size),
(int)(x),(int)(y-size));
}
/**
* Draws an triangle (point at bottom).
* @param gx the graphics context
* @param x the x coord
* @param y the y coord
* @param size the size of the shape
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -