📄 plot2d.java
字号:
} 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 */ private static void drawTriangleDown(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 a data point at a given set of panel coordinates at a given * size and connects a line to the previous point. * @param x the x coord * @param y the y coord * @param xprev the x coord of the previous point * @param yprev the y coord of the previous point * @param size the size of the point * @param shape the shape of the data point (square is reserved for nominal * error data points). Shapes: 0=x, 1=plus, 2=diamond, 3=triangle(up), * 4 = triangle (down). * @param gx the graphics context */ protected static void drawDataPoint(double x, double y, double xprev, double yprev, int size, int shape, Graphics gx) { drawDataPoint(x,y,size,shape,gx); // connect a line to the previous point gx.drawLine((int)x, (int)y, (int)xprev, (int)yprev); } /** * Draws a data point at a given set of panel coordinates at a given * size. * @param x the x coord * @param y the y coord * @param size the size of the point * @param shape the shape of the data point (square is reserved for nominal * error data points). Shapes: 0=x, 1=plus, 2=diamond, 3=triangle(up), * 4 = triangle (down). * @param gx the graphics context */ protected static void drawDataPoint(double x, double y, int size, int shape, Graphics gx) { Font lf = new Font("Monospaced", Font.PLAIN, 12); FontMetrics fm = gx.getFontMetrics(lf); if (size == 0) { size = 1; } if (shape != ERROR_SHAPE && shape != MISSING_SHAPE) { shape = shape % 5; } switch (shape) { case X_SHAPE: drawX(gx, x, y, size); break; case PLUS_SHAPE: drawPlus(gx, x, y, size); break; case DIAMOND_SHAPE: drawDiamond(gx, x, y, size); break; case TRIANGLEUP_SHAPE: drawTriangleUp(gx, x, y, size); break; case TRIANGLEDOWN_SHAPE: drawTriangleDown(gx, x, y, size); break; case ERROR_SHAPE: // draws the nominal error shape gx.drawRect((int)(x-size),(int)(y-size),(size*2),(size*2)); break; case MISSING_SHAPE: int hf = fm.getAscent(); int width = fm.stringWidth("M"); gx.drawString("M",(int)(x-(width / 2)), (int)(y+(hf / 2))); break; } } /** * Updates the perturbed values for the plots when the jitter value is * changed */ private void updatePturb() { double xj=0; double yj=0; for (int j=0;j<m_plots.size();j++) { PlotData2D temp_plot = (PlotData2D)(m_plots.elementAt(j)); for (int i=0;i<temp_plot.m_plotInstances.numInstances();i++) { if (temp_plot.m_plotInstances.instance(i).isMissing(m_xIndex) || temp_plot.m_plotInstances.instance(i).isMissing(m_yIndex)) { } else { if (m_JitterVal > 0) { xj = m_JRand.nextGaussian(); yj = m_JRand.nextGaussian(); } temp_plot.m_pointLookup[i][2] = pturbX(temp_plot.m_pointLookup[i][0],xj); temp_plot.m_pointLookup[i][3] = pturbY(temp_plot.m_pointLookup[i][1],yj); } } } } /** * Fills the lookup caches for the plots. Also calculates errors for * numeric predictions (if any) in plots */ private void fillLookup() { for (int j=0;j<m_plots.size();j++) { PlotData2D temp_plot = (PlotData2D)(m_plots.elementAt(j)); if (temp_plot.m_plotInstances.numInstances() > 0 && temp_plot.m_plotInstances.numAttributes() > 0) { for (int i=0;i<temp_plot.m_plotInstances.numInstances();i++) { if (temp_plot.m_plotInstances.instance(i).isMissing(m_xIndex) || temp_plot.m_plotInstances.instance(i).isMissing(m_yIndex)) { temp_plot.m_pointLookup[i][0] = Double.NEGATIVE_INFINITY; temp_plot.m_pointLookup[i][1] = Double.NEGATIVE_INFINITY; } else { double x = convertToPanelX(temp_plot.m_plotInstances. instance(i).value(m_xIndex)); double y = convertToPanelY(temp_plot.m_plotInstances. instance(i).value(m_yIndex)); temp_plot.m_pointLookup[i][0] = x; temp_plot.m_pointLookup[i][1] = y; } } } } } /** * Draws the data points and predictions (if provided). * @param gx the graphics context */ private void paintData(Graphics gx) { for (int j=0;j<m_plots.size();j++) { PlotData2D temp_plot = (PlotData2D)(m_plots.elementAt(j)); for (int i=0;i<temp_plot.m_plotInstances.numInstances();i++) { if (temp_plot.m_plotInstances.instance(i).isMissing(m_xIndex) || temp_plot.m_plotInstances.instance(i).isMissing(m_yIndex)) { } else { double x = (temp_plot.m_pointLookup[i][0] + temp_plot.m_pointLookup[i][2]); double y = (temp_plot.m_pointLookup[i][1] + temp_plot.m_pointLookup[i][3]); double prevx = 0; double prevy = 0; if (i > 0) { prevx = (temp_plot.m_pointLookup[i - 1][0] + temp_plot.m_pointLookup[i - 1][2]); prevy = (temp_plot.m_pointLookup[i - 1][1] + temp_plot.m_pointLookup[i - 1][3]); } int x_range = (int)x - m_XaxisStart; int y_range = (int)y - m_YaxisStart; if (x_range >= 0 && y_range >= 0) { if (m_drawnPoints[x_range][y_range] == i || m_drawnPoints[x_range][y_range] == 0 || temp_plot.m_displayAllPoints == true) { m_drawnPoints[x_range][y_range] = i; if (temp_plot.m_plotInstances.attribute(m_cIndex).isNominal()) { if (temp_plot.m_plotInstances.attribute(m_cIndex).numValues() > m_colorList.size() && !temp_plot.m_useCustomColour) { extendColourMap(temp_plot.m_plotInstances. attribute(m_cIndex).numValues()); } Color ci; if (temp_plot.m_plotInstances.instance(i). isMissing(m_cIndex)) { ci = Color.gray; } else { int ind = (int)temp_plot.m_plotInstances.instance(i). value(m_cIndex); ci = (Color)m_colorList.elementAt(ind); } if (!temp_plot.m_useCustomColour) { gx.setColor(ci); } else { gx.setColor(temp_plot.m_customColour); } if (temp_plot.m_plotInstances.instance(i). isMissing(m_cIndex)) { if (temp_plot.m_connectPoints[i] == true) { drawDataPoint(x,y,prevx,prevy,temp_plot.m_shapeSize[i], MISSING_SHAPE,gx); } else { drawDataPoint(x,y,temp_plot.m_shapeSize[i], MISSING_SHAPE,gx); } } else { if (temp_plot.m_shapeType[i] == CONST_AUTOMATIC_SHAPE) { if (temp_plot.m_connectPoints[i] == true) { drawDataPoint(x,y,prevx,prevy, temp_plot.m_shapeSize[i],j,gx); } else { drawDataPoint(x,y,temp_plot.m_shapeSize[i],j,gx); } } else { if (temp_plot.m_connectPoints[i] == true) { drawDataPoint(x,y,prevx,prevy,temp_plot.m_shapeSize[i], temp_plot.m_shapeType[i],gx); } else { drawDataPoint(x,y,temp_plot.m_shapeSize[i], temp_plot.m_shapeType[i],gx); } } } } else { double r; Color ci = null; if (!temp_plot.m_plotInstances.instance(i). isMissing(m_cIndex)) { r = (temp_plot.m_plotInstances.instance(i). value(m_cIndex) - m_minC) / (m_maxC - m_minC); r = (r * 240) + 15; ci = new Color((int)r,150,(int)(255-r)); } else { ci = Color.gray; } if (!temp_plot.m_useCustomColour) { gx.setColor(ci); } else { gx.setColor(temp_plot.m_customColour); } if (temp_plot.m_plotInstances.instance(i). isMissing(m_cIndex)) { if (temp_plot.m_connectPoints[i] == true) { drawDataPoint(x,y,prevx,prevy,temp_plot.m_shapeSize[i], MISSING_SHAPE,gx);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -