/** Calculate the rectangle occupied by the data */ protected Rectangle getDataRectangle(Graphics g, Rectangle r) { Axis a; int waxis; int x = r.x; int y = r.y; int width = r.width; int height = r.height; for (int i = 0; i < axis.size(); i++) { a = ((Axis) axis.elementAt(i)); waxis = a.getAxisWidth(g); switch (a.getAxisPos()) { case Axis.LEFT: x += waxis; width -= waxis; break; case Axis.RIGHT: width -= waxis; break; case Axis.TOP: y += waxis; height -= waxis; break; case Axis.BOTTOM: height -= waxis; break; } } return new Rectangle(x, y, width, height); }
/** * Detach a previously attached Axis. * * @param the Axis to dettach. */ public void detachAxis(Axis a) { if (a != null) { a.detachAll(); a.g2d = null; axis.removeElement(a); } }
/** * Create and attach an Axis to the graph. The position of the axis is one of Axis.TOP, * Axis.BOTTOM, Axis.LEFT or Axis.RIGHT. * * @param position Position of the axis in the drawing window. */ public Axis createAxis(int position) { Axis a; try { a = new Axis(position); a.g2d = this; axis.addElement(a); } catch (Exception e) { System.out.println("Failed to create Axis"); e.printStackTrace(); return null; } return a; }
/** * Attach a previously created Axis. Only Axes that have been attached will be drawn * * @param the Axis to attach. */ public void attachAxis(Axis a) { if (a == null) return; try { axis.addElement(a); a.g2d = this; } catch (Exception e) { System.out.println("Failed to attach Axis"); e.printStackTrace(); } }
/** * Draw the Axis. As each axis is drawn and aligned less of the canvas is avaliable to plot the * data. The returned Rectangle is the canvas area that the data is plotted in. */ protected Rectangle drawAxis(Graphics g, Rectangle r) { Axis a; int waxis; Rectangle dr; int x; int y; int width; int height; if (square) r = ForceSquare(g, r); dr = getDataRectangle(g, r); x = dr.x; y = dr.y; width = dr.width; height = dr.height; if (clearAll) { Color c = g.getColor(); g.setColor(DataBackground); g.fillRect(x, y, width, height); g.setColor(c); } // Draw a frame around the data area (If requested) if (frame) drawFrame(g, x, y, width, height); // Now draw the axis in the order specified aligning them with the final // data area. for (int i = 0; i < axis.size(); i++) { a = ((Axis) axis.elementAt(i)); a.data_window = new Dimension(width, height); switch (a.getAxisPos()) { case Axis.LEFT: r.x += a.width; r.width -= a.width; a.positionAxis(r.x, r.x, y, y + height); if (r.x == x) { a.gridcolor = gridcolor; a.drawgrid = drawgrid; a.zerocolor = zerocolor; a.drawzero = drawzero; } a.drawAxis(g); a.drawgrid = false; a.drawzero = false; break; case Axis.RIGHT: r.width -= a.width; a.positionAxis(r.x + r.width, r.x + r.width, y, y + height); if (r.x + r.width == x + width) { a.gridcolor = gridcolor; a.drawgrid = drawgrid; a.zerocolor = zerocolor; a.drawzero = drawzero; } a.drawAxis(g); a.drawgrid = false; a.drawzero = false; break; case Axis.TOP: r.y += a.width; r.height -= a.width; a.positionAxis(x, x + width, r.y, r.y); if (r.y == y) { a.gridcolor = gridcolor; a.drawgrid = drawgrid; a.zerocolor = zerocolor; a.drawzero = drawzero; } a.drawAxis(g); a.drawgrid = false; a.drawzero = false; break; case Axis.BOTTOM: r.height -= a.width; a.positionAxis(x, x + width, r.y + r.height, r.y + r.height); if (r.y + r.height == y + height) { a.gridcolor = gridcolor; a.drawgrid = drawgrid; a.zerocolor = zerocolor; a.drawzero = drawzero; } a.drawAxis(g); a.drawgrid = false; a.drawzero = false; break; } } return r; }
/** * Force the plot to have an aspect ratio of 1 by forcing the axes to have the same range. If the * range of the axes are very different some extremely odd things can occur. All axes are forced * to have the same range, so more than 2 axis is pointless. */ protected Rectangle ForceSquare(Graphics g, Rectangle r) { Axis a; Rectangle dr; int x = r.x; int y = r.y; int width = r.width; int height = r.height; double xmin; double xmax; double ymin; double ymax; double xrange = 0.0; double yrange = 0.0; double range; double aspect; if (dataset == null | dataset.isEmpty()) return r; /* ** Force all the axis to have the same range. This of course ** means that anything other than one xaxis and one yaxis ** is a bit pointless. */ for (int i = 0; i < axis.size(); i++) { a = (Axis) axis.elementAt(i); range = a.maximum - a.minimum; if (a.isVertical()) { yrange = Math.max(range, yrange); } else { xrange = Math.max(range, xrange); } } if (xrange <= 0 | yrange <= 0) return r; if (xrange > yrange) range = xrange; else range = yrange; for (int i = 0; i < axis.size(); i++) { a = (Axis) axis.elementAt(i); a.maximum = a.minimum + range; } /* ** Get the new data rectangle */ dr = getDataRectangle(g, r); /* ** Modify the data rectangle so that it is square. */ if (dr.width > dr.height) { x += (dr.width - dr.height) / 2.0; width -= dr.width - dr.height; } else { y += (dr.height - dr.width) / 2.0; height -= dr.height - dr.width; } return new Rectangle(x, y, width, height); }