periodaxis.java
来自「JfreeChart 常用图表例子」· Java 代码 · 共 1,167 行 · 第 1/3 页
JAVA
1,167 行
if (stroke == null) { throw new IllegalArgumentException("Null 'stroke' argument."); } this.minorTickMarkStroke = stroke; notifyListeners(new AxisChangeEvent(this)); } /** * Returns the paint used to display minor tick marks, if they are * visible. * * @return A paint (never <code>null</code>). */ public Paint getMinorTickMarkPaint() { return this.minorTickMarkPaint; } /** * Sets the paint used to display minor tick marks, if they are * visible, and sends a {@link AxisChangeEvent} to all registered * listeners. * * @param paint the paint (<code>null</code> not permitted). */ public void setMinorTickMarkPaint(Paint paint) { if (paint == null) { throw new IllegalArgumentException("Null 'paint' argument."); } this.minorTickMarkPaint = paint; notifyListeners(new AxisChangeEvent(this)); } /** * Returns the inside length for the minor tick marks. * * @return The length. */ public float getMinorTickMarkInsideLength() { return this.minorTickMarkInsideLength; } /** * Sets the inside length of the minor tick marks and sends an * {@link AxisChangeEvent} to all registered listeners. * * @param length the length. */ public void setMinorTickMarkInsideLength(float length) { this.minorTickMarkInsideLength = length; notifyListeners(new AxisChangeEvent(this)); } /** * Returns the outside length for the minor tick marks. * * @return The length. */ public float getMinorTickMarkOutsideLength() { return this.minorTickMarkOutsideLength; } /** * Sets the outside length of the minor tick marks and sends an * {@link AxisChangeEvent} to all registered listeners. * * @param length the length. */ public void setMinorTickMarkOutsideLength(float length) { this.minorTickMarkOutsideLength = length; notifyListeners(new AxisChangeEvent(this)); } /** * Returns an array of label info records. * * @return An array. */ public PeriodAxisLabelInfo[] getLabelInfo() { return this.labelInfo; } /** * Sets the array of label info records. * * @param info the info. */ public void setLabelInfo(PeriodAxisLabelInfo[] info) { this.labelInfo = info; } /** * Returns the range for the axis. * * @return The axis range (never <code>null</code>). */ public Range getRange() { // TODO: find a cleaner way to do this... return new Range( this.first.getFirstMillisecond(this.timeZone), this.last.getLastMillisecond(this.timeZone) ); } /** * Configures the axis to work with the current plot. Override this method * to perform any special processing (such as auto-rescaling). */ public void configure() { autoAdjustRange(); } /** * Estimates the space (height or width) required to draw the axis. * * @param g2 the graphics device. * @param plot the plot that the axis belongs to. * @param plotArea the area within which the plot (including axes) should * be drawn. * @param edge the axis location. * @param space space already reserved. * * @return The space required to draw the axis (including pre-reserved * space). */ public AxisSpace reserveSpace(Graphics2D g2, Plot plot, Rectangle2D plotArea, RectangleEdge edge, AxisSpace space) { // create a new space object if one wasn't supplied... if (space == null) { space = new AxisSpace(); } // if the axis is not visible, no additional space is required... if (!isVisible()) { return space; } // if the axis has a fixed dimension, return it... double dimension = getFixedDimension(); if (dimension > 0.0) { space.ensureAtLeast(dimension, edge); } // get the axis label size and update the space object... Rectangle2D labelEnclosure = getLabelEnclosure(g2, edge); double labelHeight = 0.0; double labelWidth = 0.0; double tickLabelBandsDimension = 0.0; for (int i = 0; i < this.labelInfo.length; i++) { PeriodAxisLabelInfo info = this.labelInfo[i]; FontMetrics fm = g2.getFontMetrics(info.getLabelFont()); tickLabelBandsDimension += info.getPadding().extendHeight(fm.getHeight()); } if (RectangleEdge.isTopOrBottom(edge)) { labelHeight = labelEnclosure.getHeight(); space.add(labelHeight + tickLabelBandsDimension, edge); } else if (RectangleEdge.isLeftOrRight(edge)) { labelWidth = labelEnclosure.getWidth(); space.add(labelWidth + tickLabelBandsDimension, edge); } // add space for the outer tick labels, if any... double tickMarkSpace = 0.0; if (isTickMarksVisible()) { tickMarkSpace = getTickMarkOutsideLength(); } if (this.minorTickMarksVisible) { tickMarkSpace = Math.max( tickMarkSpace, this.minorTickMarkOutsideLength ); } space.add(tickMarkSpace, edge); return space; } /** * Draws the axis on a Java 2D graphics device (such as the screen or a * printer). * * @param g2 the graphics device (<code>null</code> not permitted). * @param cursor the cursor location (determines where to draw the axis). * @param plotArea the area within which the axes and plot should be drawn. * @param dataArea the area within which the data should be drawn. * @param edge the axis location (<code>null</code> not permitted). * @param plotState collects information about the plot * (<code>null</code> permitted). * * @return The axis state (never <code>null</code>). */ public AxisState draw(Graphics2D g2, double cursor, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, PlotRenderingInfo plotState) { AxisState axisState = new AxisState(cursor); if (isAxisLineVisible()) { drawAxisLine(g2, cursor, dataArea, edge); } drawTickMarks(g2, axisState, dataArea, edge); for (int band = 0; band < this.labelInfo.length; band++) { axisState = drawTickLabels(band, g2, axisState, dataArea, edge); } // draw the axis label (note that 'state' is passed in *and* // returned)... axisState = drawLabel( getLabel(), g2, plotArea, dataArea, edge, axisState ); return axisState; } /** * Draws the tick marks for the axis. * * @param g2 the graphics device. * @param state the axis state. * @param dataArea the data area. * @param edge the edge. */ protected void drawTickMarks(Graphics2D g2, AxisState state, Rectangle2D dataArea, RectangleEdge edge) { if (RectangleEdge.isTopOrBottom(edge)) { drawTickMarksHorizontal(g2, state, dataArea, edge); } else if (RectangleEdge.isLeftOrRight(edge)) { drawTickMarksVertical(g2, state, dataArea, edge); } } /** * Draws the major and minor tick marks for an axis that lies at the top or * bottom of the plot. * * @param g2 the graphics device. * @param state the axis state. * @param dataArea the data area. * @param edge the edge. */ protected void drawTickMarksHorizontal(Graphics2D g2, AxisState state, Rectangle2D dataArea, RectangleEdge edge) { double x0 = dataArea.getX(); double y0 = state.getCursor(); double insideLength = getTickMarkInsideLength(); double outsideLength = getTickMarkOutsideLength(); RegularTimePeriod t = RegularTimePeriod.createInstance( this.majorTickTimePeriodClass, this.first.getStart(), getTimeZone() ); long t0 = t.getFirstMillisecond(getTimeZone()); Line2D inside = null; Line2D outside = null; long firstOnAxis = getFirst().getFirstMillisecond(getTimeZone()); long lastOnAxis = getLast().getLastMillisecond(getTimeZone()); while (t0 <= lastOnAxis) { x0 = valueToJava2D(t0, dataArea, edge); if (edge == RectangleEdge.TOP) { inside = new Line2D.Double(x0, y0, x0, y0 + insideLength); outside = new Line2D.Double(x0, y0, x0, y0 - outsideLength); } else if (edge == RectangleEdge.BOTTOM) { inside = new Line2D.Double(x0, y0, x0, y0 - insideLength); outside = new Line2D.Double(x0, y0, x0, y0 + outsideLength); } if (t0 > firstOnAxis) { g2.setPaint(getTickMarkPaint()); g2.setStroke(getTickMarkStroke()); g2.draw(inside); g2.draw(outside); } // draw minor tick marks if (this.minorTickMarksVisible) { RegularTimePeriod tminor = RegularTimePeriod.createInstance( this.minorTickTimePeriodClass, new Date(t0), getTimeZone() ); long tt0 = tminor.getFirstMillisecond(getTimeZone()); while (tt0 < t.getLastMillisecond(getTimeZone()) && tt0 < lastOnAxis) { double xx0 = valueToJava2D(tt0, dataArea, edge); if (edge == RectangleEdge.TOP) { inside = new Line2D.Double( xx0, y0, xx0, y0 + this.minorTickMarkInsideLength ); outside = new Line2D.Double( xx0, y0, xx0, y0 - this.minorTickMarkOutsideLength ); } else if (edge == RectangleEdge.BOTTOM) { inside = new Line2D.Double( xx0, y0, xx0, y0 - this.minorTickMarkInsideLength ); outside = new Line2D.Double( xx0, y0, xx0, y0 + this.minorTickMarkOutsideLength ); } if (tt0 >= firstOnAxis) { g2.setPaint(this.minorTickMarkPaint); g2.setStroke(this.minorTickMarkStroke); g2.draw(inside); g2.draw(outside); } tminor = tminor.next(); tt0 = tminor.getFirstMillisecond(getTimeZone()); } } t = t.next(); t0 = t.getFirstMillisecond(getTimeZone()); } if (edge == RectangleEdge.TOP) { state.cursorUp( Math.max(outsideLength, this.minorTickMarkOutsideLength) ); } else if (edge == RectangleEdge.BOTTOM) { state.cursorDown( Math.max(outsideLength, this.minorTickMarkOutsideLength) ); } } /** * Draws the tick marks for a vertical axis. * * @param g2 the graphics device. * @param state the axis state. * @param dataArea the data area. * @param edge the edge. */ protected void drawTickMarksVertical(Graphics2D g2, AxisState state, Rectangle2D dataArea, RectangleEdge edge) { } /** * Draws the tick labels for one "band" of time periods. * * @param band the band index (zero-based). * @param g2 the graphics device. * @param state the axis state. * @param dataArea the data area. * @param edge the edge where the axis is located. * * @return The updated axis state. */ protected AxisState drawTickLabels(int band, Graphics2D g2, AxisState state, Rectangle2D dataArea, RectangleEdge edge) { // work out the initial gap double delta1 = 0.0; FontMetrics fm = g2.getFontMetrics(this.labelInfo[band].getLabelFont()); if (edge == RectangleEdge.BOTTOM) { delta1 = this.labelInfo[band].getPadding().calculateTopOutset( fm.getHeight() ); } else if (edge == RectangleEdge.TOP) { delta1 = this.labelInfo[band].getPadding().calculateBottomOutset( fm.getHeight() ); } state.moveCursor(delta1, edge); long axisMin = this.first.getFirstMillisecond(this.timeZone); long axisMax = this.last.getLastMillisecond(this.timeZone); g2.setFont(this.labelInfo[band].getLabelFont()); g2.setPaint(this.labelInfo[band].getLabelPaint()); // work out the number of periods to skip for labelling RegularTimePeriod p1 = this.labelInfo[band].createInstance( new Date(axisMin), this.timeZone ); RegularTimePeriod p2 = this.labelInfo[band].createInstance( new Date(axisMax), this.timeZone ); String label1 = this.labelInfo[band].getDateFormat().format( new Date(p1.getMiddleMillisecond(this.timeZone)) ); String label2 = this.labelInfo[band].getDateFormat().format( new Date(p2.getMiddleMillisecond(this.timeZone)) ); Rectangle2D b1 = TextUtilities.getTextBounds(
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?