📄 stripchart.java
字号:
System.err.println("Interrupting"); m_updateHandler.interrupt(); m_updateHandler = null; } synchronized (m_dataList) { m_dataList = new LinkedList(); } m_outputFrame.dispose(); m_outputFrame = null; } }); m_outputFrame.pack(); m_outputFrame.setSize(600,150); m_outputFrame.setResizable(false); m_outputFrame.setVisible(true); int iwidth = m_plotPanel.getWidth(); int iheight = m_plotPanel.getHeight(); m_osi = m_plotPanel.createImage(iwidth, iheight); Graphics m = m_osi.getGraphics(); m.fillRect(0,0,iwidth,iheight); m_previousY[0] = -1; setRefreshWidth(); if (m_updateHandler == null) { System.err.println("Starting handler"); startHandler(); } } } private int convertToPanelY(double yval) { int height = m_plotPanel.getHeight(); double temp = (yval - m_min) / (m_max - m_min); temp = temp * height; temp = height - temp; return (int)temp; } /** * Update the plot * * @param dataPoint contains y values to plot */ protected void updateChart(double [] dataPoint) { if (m_previousY[0] == -1) { int iw = m_plotPanel.getWidth(); int ih = m_plotPanel.getHeight(); m_osi = m_plotPanel.createImage(iw, ih); Graphics m = m_osi.getGraphics(); m.fillRect(0,0,iw,ih); m_previousY[0] = convertToPanelY(0); m_iheight = ih; m_iwidth = iw; } if (dataPoint.length-1 != m_previousY.length) { m_previousY = new double [dataPoint.length-1]; // m_plotCount = 0; for (int i = 0; i < dataPoint.length-1; i++) { m_previousY[i] = convertToPanelY(0); } } Graphics osg = m_osi.getGraphics(); Graphics g = m_plotPanel.getGraphics(); osg.copyArea(m_refreshWidth,0,m_iwidth-m_refreshWidth, m_iheight,-m_refreshWidth,0); osg.setColor(Color.black); osg.fillRect(m_iwidth-m_refreshWidth,0, m_iwidth, m_iheight); // paint the old scale onto the plot if a scale update has occured if (m_yScaleUpdate) { String maxVal = numToString(m_oldMax); String minVal = numToString(m_oldMin); String midVal = numToString((m_oldMax - m_oldMin) / 2.0); if (m_labelMetrics == null) { m_labelMetrics = g.getFontMetrics(m_labelFont); } osg.setFont(m_labelFont); int wmx = m_labelMetrics.stringWidth(maxVal); int wmn = m_labelMetrics.stringWidth(minVal); int wmd = m_labelMetrics.stringWidth(midVal); int hf = m_labelMetrics.getAscent(); osg.setColor(m_colorList[m_colorList.length-1]); osg.drawString(maxVal, m_iwidth-wmx, hf-2); osg.drawString(midVal, m_iwidth-wmd, (m_iheight / 2)+(hf / 2)); osg.drawString(minVal, m_iwidth-wmn, m_iheight-1); m_yScaleUpdate = false; } double pos; for (int i = 0; i < dataPoint.length-1; i++) { osg.setColor(m_colorList[(i % m_colorList.length)]); pos = convertToPanelY(dataPoint[i]); osg.drawLine(m_iwidth-m_refreshWidth, (int)m_previousY[i], m_iwidth-1, (int)pos); m_previousY[i] = pos; if (dataPoint[dataPoint.length-1] % m_xValFreq == 0) { // draw the actual y value onto the plot for this curve String val = numToString(dataPoint[i]); if (m_labelMetrics == null) { m_labelMetrics = g.getFontMetrics(m_labelFont); } int hf = m_labelMetrics.getAscent(); if (pos - hf < 0) { pos += hf; } int w = m_labelMetrics.stringWidth(val); osg.setFont(m_labelFont); osg.drawString(val, m_iwidth-w, (int)pos); } } // last element in the data point array contains the data point number if (dataPoint[dataPoint.length-1] % m_xValFreq == 0) { String xVal = ""+(int)dataPoint[dataPoint.length-1]; osg.setColor(m_colorList[m_colorList.length-1]); int w = m_labelMetrics.stringWidth(xVal); osg.setFont(m_labelFont); osg.drawString(xVal, m_iwidth-w, m_iheight - 1); } g.drawImage(m_osi,0,0,m_plotPanel); // System.err.println("Finished"); // m_plotCount++; } private static String numToString(double num) { int precision = 1; int whole = (int)Math.abs(num); double decimal = Math.abs(num) - whole; int nondecimal; nondecimal = (whole > 0) ? (int)(Math.log(whole) / Math.log(10)) : 1; precision = (decimal > 0) ? (int)Math.abs(((Math.log(Math.abs(num)) / Math.log(10))))+2 : 1; if (precision > 5) { precision = 1; } String numString = weka.core.Utils.doubleToString(num, nondecimal+1+precision ,precision); return numString; } /** * Accept a data point (encapsulated in a chart event) to plot * * @param e a <code>ChartEvent</code> value */ public void acceptDataPoint(ChartEvent e) { if (e.getReset()) { m_xCount = 0; m_max = 1; m_min = 0; } if (m_outputFrame != null) { boolean refresh = false; if (e.getLegendText() != null & e.getLegendText() != m_legendText) { m_legendText = e.getLegendText(); refresh = true; } if (e.getMin() != m_min || e.getMax() != m_max) { m_oldMax = m_max; m_oldMin = m_min; m_max = e.getMax(); m_min = e.getMin(); refresh = true; m_yScaleUpdate = true; } if (refresh) { m_legendPanel.repaint(); m_scalePanel.repaint(); } acceptDataPoint(e.getDataPoint()); } m_xCount++; } /** * Accept a data point to plot * * @param dataPoint a <code>double[]</code> value */ public void acceptDataPoint(double [] dataPoint) { if (m_outputFrame != null && (m_xCount % m_refreshFrequency == 0 )) { double [] dp = new double[dataPoint.length+1]; dp[dp.length-1] = m_xCount; System.arraycopy(dataPoint, 0, dp, 0, dataPoint.length); // check for out of scale values for (int i = 0; i < dataPoint.length; i++) { if (dataPoint[i] < m_min) { m_oldMin = m_min; m_min = dataPoint[i]; m_yScaleUpdate = true; } if (dataPoint[i] > m_max) { m_oldMax = m_max; m_max = dataPoint[i]; m_yScaleUpdate = true; } } synchronized(m_dataList) { m_dataList.add(m_dataList.size(), dp); // m_dataList.push(dp); m_dataList.notifyAll(); /* if (m_dataList.size() != 0) { System.err.println("***** "+m_dataList.size()); } */ // System.err.println(m_xCount); } } } /** * Set the visual appearance of this bean * * @param newVisual a <code>BeanVisual</code> value */ public void setVisual(BeanVisual newVisual) { m_visual = newVisual; } /** * Get the visual appearance of this bean */ public BeanVisual getVisual() { return m_visual; } /** * Use the default visual appearance for this bean */ public void useDefaultVisual() { m_visual.loadIcons(BeanVisual.ICON_PATH+"StripChart.gif", BeanVisual.ICON_PATH+"StripChart_animated.gif"); } /** * Stop any processing that the bean might be doing. */ public void stop() { // nothing to be done } /** * Set a logger * * @param logger a <code>weka.gui.Logger</code> value */ public void setLog(weka.gui.Logger logger) { m_log = logger; } /** * Returns true if, at this time, * the object will accept a connection via the named event * * @param eventName the name of the event * @return true if the object will accept a connection */ public boolean connectionAllowed(String eventName) { if (m_listenee == null) { return true; } return false; } /** * Notify this object that it has been registered as a listener with * a source for recieving events described by the named event * This object is responsible for recording this fact. * * @param eventName the event * @param source the source with which this object has been registered as * a listener */ public void connectionNotification(String eventName, Object source) { if (connectionAllowed(eventName)) { m_listenee = source; } } /** * Notify this object that it has been deregistered as a listener with * a source for named event. This object is responsible * for recording this fact. * * @param eventName the event * @param source the source with which this object has been registered as * a listener */ public void disconnectionNotification(String eventName, Object source) { m_listenee = null; } /** * Describe <code>enumerateRequests</code> method here. * * @return an <code>Enumeration</code> value */ public Enumeration enumerateRequests() { Vector newVector = new Vector(0); if (m_outputFrame == null) { newVector.addElement("Show chart"); } return newVector.elements(); } /** * Describe <code>performRequest</code> method here. * * @param request a <code>String</code> value * @exception IllegalArgumentException if an error occurs */ public void performRequest(String request) { if (request.compareTo("Show chart") == 0) { showChart(); } else { throw new IllegalArgumentException(request + " not supported (StripChart)"); } } /** * Tests out the StripChart from the command line * * @param args ignored */ public static void main(String [] args) { try { final javax.swing.JFrame jf = new javax.swing.JFrame("Weka Knowledge Flow : StipChart"); jf.getContentPane().setLayout(new BorderLayout()); final StripChart jd = new StripChart(); jf.getContentPane().add(jd, BorderLayout.CENTER); jf.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { jf.dispose(); System.exit(0); } }); jf.pack(); jf.setVisible(true); jd.showChart(); Random r = new Random(1); for (int i = 0; i < 1020; i++) { double [] pos = new double[1]; pos[0] = r.nextDouble(); jd.acceptDataPoint(pos); } System.err.println("Done sending data"); } catch (Exception ex) { ex.printStackTrace(); System.err.println(ex.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -