📄 plotbox.java
字号:
/** Set the title font. */ public void setTitleFont (String fullfontname) { // Can't use Font.decode() here, it is not present in jdk1.0.2 //_titlefont = Font.decode(fullfontname); _titlefont = getFontByName(fullfontname); _titleFontMetrics = getFontMetrics(_titlefont); } /** * Set the label for the X (horizontal) axis. The label will * appear on the subsequent call to <code>paint()</code> or * <code>drawPlot()</code>. */ public void setXLabel (String label) { _xlabel = label; } /** * Control whether the X axis is drawn with a logarithmic scale. */ public void setXLog (boolean xlog) { _xlog = xlog; } /** * Set the X (horizontal) range of the plot. If this is not done * explicitly, then the range is computed automatically from data * available when <code>paint()</code> or <code>drawPlot()</code> * are called. If min and max are identical, then the range is * arbitrarily spread by 1. */ public void setXRange (double min, double max) { if (_debug > 7) System.out.println("PlotBox: setXRange"); _xRangeGiven = true; _setXRange(min,max); } /** * Set the label for the Y (vertical) axis. The label will * appear on the subsequent call to <code>paint()</code> or * <code>drawPlot()</code>. */ public void setYLabel (String label) { _ylabel = label; } /** * Control whether the Y axis is drawn with a logarithmic scale. */ public void setYLog (boolean ylog) { _ylog = ylog; } /** * Set the Y (vertical) range of the plot. If this is not done * explicitly, then the range is computed automatically from data * available when <code>paint()</code> or <code>drawPlot()</code> * are called. If min and max are identical, then the range is * arbitrarily spread by 0.1. */ public void setYRange (double min, double max) { _yRangeGiven = true; _setYRange(min,max); } ////////////////////////////////////////////////////////////////////////// //// protected methods //// /** * Put a mark corresponding to the specified dataset at the * specified x and y position. The mark is drawn in the * current color. In this base class, a point is a * filled rectangle 6 pixels across. Note that marks greater than * about 6 pixels in size will not look very good since they will * overlap axis labels and may not fit well in the legend. The * <i>clip</i> argument, if <code>true</code>, states * that the point should not be drawn if * it is out of range. */ protected void _drawPoint(Graphics graphics, int dataset, long xpos, long ypos, boolean clip) { boolean pointinside = ypos <= _lry && ypos >= _uly && xpos <= _lrx && xpos >= _ulx; if (!pointinside && clip) {return;} graphics.fillRect((int)xpos-6, (int)ypos-6, 6, 6); } /** Hook for child classes to do any file preprocessing */ protected void _newFile(){ } /** * Hook to parse a binary stream. * @exception PlotDataException if there is a serious data format problem. * @exception java.io.IOException if an I/O error occurs. */ protected void _parseBinaryStream(DataInputStream in) throws PlotDataException, IOException { throw new PlotDataException("Binary data not supported in the" + "baseclass"); } /** * Parse a line that gives plotting information. In this base * class, only lines pertaining to the title and labels are processed. * Everything else is ignored. Return true if the line is recognized. */ protected boolean _parseLine (String line) { // Parse commands in the input file, ignoring lines with // syntax errors or unrecognized commands. if (_debug > 20) System.out.println("PlotBox: parseLine "+ line); // We convert the line to lower case so that the command // names are case insensitive. String lcLine = new String(line.toLowerCase()); if (lcLine.startsWith("#")) { // comment character return true; } if (lcLine.startsWith("titletext:")) { setTitle((line.substring(10)).trim()); return true; } if (lcLine.startsWith("xlabel:")) { setXLabel((line.substring(7)).trim()); return true; } if (lcLine.startsWith("ylabel:")) { setYLabel((line.substring(7)).trim()); return true; } if (lcLine.startsWith("xrange:")) { int comma = line.indexOf(",", 7); if (comma > 0) { String min = (line.substring(7,comma)).trim(); String max = (line.substring(comma+1)).trim(); try { Double dmin = new Double(min); Double dmax = new Double(max); setXRange(dmin.doubleValue(), dmax.doubleValue()); } catch (NumberFormatException e) { // ignore if format is bogus. } } return true; } if (lcLine.startsWith("yrange:")) { int comma = line.indexOf(",", 7); if (comma > 0) { String min = (line.substring(7,comma)).trim(); String max = (line.substring(comma+1)).trim(); try { Double dmin = new Double(min); Double dmax = new Double(max); setYRange(dmin.doubleValue(), dmax.doubleValue()); } catch (NumberFormatException e) { // ignore if format is bogus. } } return true; } if (lcLine.startsWith("xticks:")) { // example: // XTicks "label" 0, "label" 1, "label" 3 _parsePairs(line.substring(7), true); return true; } if (lcLine.startsWith("xlog:")) { if (lcLine.indexOf("off",5) >= 0) { _xlog = false; } else { _xlog = true; } return true; } if (lcLine.startsWith("ylog:")) { if (lcLine.indexOf("off",5) >= 0) { _ylog = false; } else { _ylog = true; } return true; } if (lcLine.startsWith("grid:")) { if (lcLine.indexOf("off",5) >= 0) { _grid = false; } else { _grid = true; } return true; } if (lcLine.startsWith("color:")) { if (lcLine.indexOf("off",6) >= 0) { _usecolor = false; } else { _usecolor = true; } return true; } return false; } /** Set the visibility of the Fill button. */ protected void _setButtonsVisibility(boolean vis) { // _fillButton.setVisible(vis); if (vis) { _fillButton.show(); // FIXME: show() is // deprecated in JDK1.1, but we need to compile under // 1.0.2 for netscape3.x compatibility. } else { _fillButton.hide(); // FIXME: hide() is // deprecated in JDK1.1, but we need to compile under // 1.0.2 for netscape3.x compatibility. } } ////////////////////////////////////////////////////////////////////////// //// protected variables //// // If non-zero, print out debugging messages. Use setDebug() to set this. protected int _debug = 0; // The graphics context to operate in. Note that printing will call // paint with a different graphics object, so we have to pass this // around properly. // 'transient' means that this field with not be serialized. protected transient Graphics _graphics = null; // The range of the data to be plotted. protected double _yMax = 0, _yMin = 0, _xMax = 0, _xMin = 0; // The factor we pad by so that we don't plot points on the axes. protected static final double _PADDING = 0.05; // Whether the ranges have been given. protected boolean _xRangeGiven = false; protected boolean _yRangeGiven = false; // The minimum and maximum values registered so far, for auto ranging. protected double _xBottom = Double.MAX_VALUE; protected double _xTop = - Double.MAX_VALUE; protected double _yBottom = Double.MAX_VALUE; protected double _yTop = - Double.MAX_VALUE; // Whether to draw the axes using a logarithmic scale. protected boolean _xlog = false, _ylog = false; // For use in calculating log base 10. A log times this is a log base 10. protected static final double _LOG10SCALE = 1/Math.log(10); // Whether to draw a background grid. protected boolean _grid = true; // Color of the background, settable from HTML. protected Color _background = null; // Color of the foreground, settable from HTML. protected Color _foreground = null; // Derived classes can increment these to make space around the plot. protected int _topPadding = 10; protected int _bottomPadding = 5; protected int _rightPadding = 10; protected int _leftPadding = 10; // The plot rectangle in pixels. // The naming convention is: "_ulx" = "upper left x", where "x" is // the horizontal dimension. protected int _ulx = 1 , _uly = 1, _lrx = 100, _lry = 100; // Scaling used in plotting points. protected double _yscale = 1.0, _xscale = 1.0; // Indicator whether to use _colors protected boolean _usecolor = true; // Default _colors, by data set. // There are 11 colors so that combined with the // 10 marks of the Plot class, we can distinguish 110 // distinct data sets. static protected Color[] _colors = { new Color(0xffffff), // white
new Color(0xff0000), // red new Color(0x0000ff), // blue new Color(0x14ff14), // green-ish new Color(0x000000), // black new Color(0xffa500), // orange new Color(0x53868b), // cadetblue4 new Color(0xff7f50), // coral new Color(0x55bb2f), // dark green-ish new Color(0x90422d), // sienna-ish new Color(0xa0a0a0), // grey-ish new Color(0x00aaaa), // cyan-ish }; // Width and height of component in pixels. protected int _width = 400, _height = 400; ////////////////////////////////////////////////////////////////////////// //// private methods //// /** * Draw the legend in the upper right corner and return the width * (in pixels) used up. The arguments give the upper right corner * of the region where the legend should be placed. */ private int _drawLegend(Graphics graphics, int urx, int ury) { // FIXME: consolidate all these for efficiency graphics.setFont(_labelfont); int spacing = _labelFontMetrics.getHeight(); Enumeration v = _legendStrings.elements(); Enumeration i = _legendDatasets.elements(); int ypos = ury + spacing; int maxwidth = 0; while (v.hasMoreElements()) { String legend = (String) v.nextElement(); // NOTE: relies on _legendDatasets having the same num. of entries. int dataset = ((Integer) i.nextElement()).intValue(); if (dataset >= 0) { if (_usecolor) { // Points are only distinguished up to the number of colors int color = dataset % _colors.length; graphics.setColor(_colors[color]); } _drawPoint(graphics, dataset, urx-3, ypos-3, false); graphics.setColor(_foreground);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -