axis.java
来自「JfreeChart 常用图表例子」· Java 代码 · 共 1,201 行 · 第 1/3 页
JAVA
1,201 行
* Calculates the positions of the ticks for the axis, storing the results * in the tick list (ready for drawing). * * @param g2 the graphics device. * @param state the axis state. * @param dataArea the area inside the axes. * @param edge the edge on which the axis is located. * * @return The list of ticks. */ public abstract List refreshTicks(Graphics2D g2, AxisState state, Rectangle2D dataArea, RectangleEdge edge); /** * Registers an object for notification of changes to the axis. * * @param listener the object that is being registered. */ public void addChangeListener(AxisChangeListener listener) { this.listenerList.add(AxisChangeListener.class, listener); } /** * Deregisters an object for notification of changes to the axis. * * @param listener the object to deregister. */ public void removeChangeListener(AxisChangeListener listener) { this.listenerList.remove(AxisChangeListener.class, listener); } /** * Returns <code>true</code> if the specified object is registered with * the dataset as a listener. Most applications won't need to call this * method, it exists mainly for use by unit testing code. * * @param listener the listener. * * @return A boolean. */ public boolean hasListener(EventListener listener) { List list = Arrays.asList(this.listenerList.getListenerList()); return list.contains(listener); } /** * Notifies all registered listeners that the axis has changed. * The AxisChangeEvent provides information about the change. * * @param event information about the change to the axis. */ protected void notifyListeners(AxisChangeEvent event) { Object[] listeners = this.listenerList.getListenerList(); for (int i = listeners.length - 2; i >= 0; i -= 2) { if (listeners[i] == AxisChangeListener.class) { ((AxisChangeListener) listeners[i + 1]).axisChanged(event); } } } /** * Returns a rectangle that encloses the axis label. This is typically * used for layout purposes (it gives the maximum dimensions of the label). * * @param g2 the graphics device. * @param edge the edge of the plot area along which the axis is measuring. * * @return The enclosing rectangle. */ protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) { Rectangle2D result = new Rectangle2D.Double(); String axisLabel = getLabel(); if (axisLabel != null && !axisLabel.equals("")) { FontMetrics fm = g2.getFontMetrics(getLabelFont()); Rectangle2D bounds = TextUtilities.getTextBounds(axisLabel, g2, fm); RectangleInsets insets = getLabelInsets(); bounds = insets.createOutsetRectangle(bounds); double angle = getLabelAngle(); if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) { angle = angle - Math.PI / 2.0; } double x = bounds.getCenterX(); double y = bounds.getCenterY(); AffineTransform transformer = AffineTransform.getRotateInstance(angle, x, y); Shape labelBounds = transformer.createTransformedShape(bounds); result = labelBounds.getBounds2D(); } return result; } /** * Draws the axis label. * * @param label the label text. * @param g2 the graphics device. * @param plotArea the plot area. * @param dataArea the area inside the axes. * @param edge the location of the axis. * @param state the axis state (<code>null</code> not permitted). * * @return Information about the axis. */ protected AxisState drawLabel(String label, Graphics2D g2, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, AxisState state) { // it is unlikely that 'state' will be null, but check anyway... if (state == null) { throw new IllegalArgumentException("Null 'state' argument."); } if ((label == null) || (label.equals(""))) { return state; } Font font = getLabelFont(); RectangleInsets insets = getLabelInsets(); g2.setFont(font); g2.setPaint(getLabelPaint()); FontMetrics fm = g2.getFontMetrics(); Rectangle2D labelBounds = TextUtilities.getTextBounds(label, g2, fm); if (edge == RectangleEdge.TOP) { AffineTransform t = AffineTransform.getRotateInstance( getLabelAngle(), labelBounds.getCenterX(), labelBounds.getCenterY() ); Shape rotatedLabelBounds = t.createTransformedShape(labelBounds); labelBounds = rotatedLabelBounds.getBounds2D(); double labelx = dataArea.getCenterX(); double labely = state.getCursor() - insets.getBottom() - labelBounds.getHeight() / 2.0; TextUtilities.drawRotatedString( label, g2, (float) labelx, (float) labely, TextAnchor.CENTER, getLabelAngle(), TextAnchor.CENTER ); state.cursorUp( insets.getTop() + labelBounds.getHeight() + insets.getBottom() ); } else if (edge == RectangleEdge.BOTTOM) { AffineTransform t = AffineTransform.getRotateInstance( getLabelAngle(), labelBounds.getCenterX(), labelBounds.getCenterY() ); Shape rotatedLabelBounds = t.createTransformedShape(labelBounds); labelBounds = rotatedLabelBounds.getBounds2D(); double labelx = dataArea.getCenterX(); double labely = state.getCursor() + insets.getTop() + labelBounds.getHeight() / 2.0; TextUtilities.drawRotatedString( label, g2, (float) labelx, (float) labely, TextAnchor.CENTER, getLabelAngle(), TextAnchor.CENTER ); state.cursorDown( insets.getTop() + labelBounds.getHeight() + insets.getBottom() ); } else if (edge == RectangleEdge.LEFT) { AffineTransform t = AffineTransform.getRotateInstance( getLabelAngle() - Math.PI / 2.0, labelBounds.getCenterX(), labelBounds.getCenterY() ); Shape rotatedLabelBounds = t.createTransformedShape(labelBounds); labelBounds = rotatedLabelBounds.getBounds2D(); double labelx = state.getCursor() - insets.getRight() - labelBounds.getWidth() / 2.0; double labely = dataArea.getCenterY(); TextUtilities.drawRotatedString( label, g2, (float) labelx, (float) labely, TextAnchor.CENTER, getLabelAngle() - Math.PI / 2.0, TextAnchor.CENTER ); state.cursorLeft( insets.getLeft() + labelBounds.getWidth() + insets.getRight() ); } else if (edge == RectangleEdge.RIGHT) { AffineTransform t = AffineTransform.getRotateInstance( getLabelAngle() + Math.PI / 2.0, labelBounds.getCenterX(), labelBounds.getCenterY() ); Shape rotatedLabelBounds = t.createTransformedShape(labelBounds); labelBounds = rotatedLabelBounds.getBounds2D(); double labelx = state.getCursor() + insets.getLeft() + labelBounds.getWidth() / 2.0; double labely = dataArea.getY() + dataArea.getHeight() / 2.0; TextUtilities.drawRotatedString( label, g2, (float) labelx, (float) labely, TextAnchor.CENTER, getLabelAngle() + Math.PI / 2.0, TextAnchor.CENTER ); state.cursorRight( insets.getLeft() + labelBounds.getWidth() + insets.getRight() ); } return state; } /** * Draws an axis line at the current cursor position and edge. * * @param g2 the graphics device. * @param cursor the cursor position. * @param dataArea the data area. * @param edge the edge. */ protected void drawAxisLine(Graphics2D g2, double cursor, Rectangle2D dataArea, RectangleEdge edge) { Line2D axisLine = null; if (edge == RectangleEdge.TOP) { axisLine = new Line2D.Double( dataArea.getX(), cursor, dataArea.getMaxX(), cursor ); } else if (edge == RectangleEdge.BOTTOM) { axisLine = new Line2D.Double( dataArea.getX(), cursor, dataArea.getMaxX(), cursor ); } else if (edge == RectangleEdge.LEFT) { axisLine = new Line2D.Double( cursor, dataArea.getY(), cursor, dataArea.getMaxY() ); } else if (edge == RectangleEdge.RIGHT) { axisLine = new Line2D.Double( cursor, dataArea.getY(), cursor, dataArea.getMaxY() ); } g2.setPaint(this.axisLinePaint); g2.setStroke(this.axisLineStroke); g2.draw(axisLine); } /** * Returns a clone of the axis. * * @return A clone. * * @throws CloneNotSupportedException if some component of the axis does * not support cloning. */ public Object clone() throws CloneNotSupportedException { Axis clone = (Axis) super.clone(); // It's up to the plot which clones up to restore the correct references clone.plot = null; clone.listenerList = new EventListenerList(); return clone; } /** * Tests this axis for equality with another object. * * @param obj the object (<code>null</code> permitted). * * @return <code>true</code> or <code>false</code>. */ public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof Axis)) { return false; } Axis that = (Axis) obj; if (this.visible != that.visible) { return false; } if (!ObjectUtilities.equal(this.label, that.label)) { return false; } if (!ObjectUtilities.equal(this.labelFont, that.labelFont)) { return false; } if (!ObjectUtilities.equal(this.labelPaint, that.labelPaint)) { return false; } if (!ObjectUtilities.equal(this.labelInsets, that.labelInsets)) { return false; } if (this.labelAngle != that.labelAngle) { return false; } if (this.axisLineVisible != that.axisLineVisible) { return false; } if (!ObjectUtilities.equal(this.axisLineStroke, that.axisLineStroke)) { return false; } if (!ObjectUtilities.equal(this.axisLinePaint, that.axisLinePaint)) { return false; } if (this.tickLabelsVisible != that.tickLabelsVisible) { return false; } if (!ObjectUtilities.equal(this.tickLabelFont, that.tickLabelFont)) { return false; } if (!ObjectUtilities.equal(this.tickLabelPaint, that.tickLabelPaint)) { return false; } if (!ObjectUtilities.equal( this.tickLabelInsets, that.tickLabelInsets )) { return false; } if (this.tickMarksVisible != that.tickMarksVisible) { return false; } if (this.tickMarkInsideLength != that.tickMarkInsideLength) { return false; } if (this.tickMarkOutsideLength != that.tickMarkOutsideLength) { return false; } if (!ObjectUtilities.equal(this.tickMarkPaint, that.tickMarkPaint)) { return false; } if (!ObjectUtilities.equal(this.tickMarkStroke, that.tickMarkStroke)) { return false; } if (this.fixedDimension != that.fixedDimension) { return false; } return true; } /** * Provides serialization support. * * @param stream the output stream. * * @throws IOException if there is an I/O error. */ private void writeObject(ObjectOutputStream stream) throws IOException { stream.defaultWriteObject(); SerialUtilities.writePaint(this.labelPaint, stream); SerialUtilities.writePaint(this.tickLabelPaint, stream); SerialUtilities.writeStroke(this.axisLineStroke, stream); SerialUtilities.writePaint(this.axisLinePaint, stream); SerialUtilities.writeStroke(this.tickMarkStroke, stream); SerialUtilities.writePaint(this.tickMarkPaint, stream); } /** * Provides serialization support. * * @param stream the input stream. * * @throws IOException if there is an I/O error. * @throws ClassNotFoundException if there is a classpath problem. */ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); this.labelPaint = SerialUtilities.readPaint(stream); this.tickLabelPaint = SerialUtilities.readPaint(stream); this.axisLineStroke = SerialUtilities.readStroke(stream); this.axisLinePaint = SerialUtilities.readPaint(stream); this.tickMarkStroke = SerialUtilities.readStroke(stream); this.tickMarkPaint = SerialUtilities.readPaint(stream); this.listenerList = new EventListenerList(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?