001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2017, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * --------------- 028 * PeriodAxis.java 029 * --------------- 030 * (C) Copyright 2004-2017, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes 036 * ------- 037 * 01-Jun-2004 : Version 1 (DG); 038 * 16-Sep-2004 : Fixed bug in equals() method, added clone() method and 039 * PublicCloneable interface (DG); 040 * 25-Nov-2004 : Updates to support major and minor tick marks (DG); 041 * 25-Feb-2005 : Fixed some tick mark bugs (DG); 042 * 15-Apr-2005 : Fixed some more tick mark bugs (DG); 043 * 26-Apr-2005 : Removed LOGGER (DG); 044 * 16-Jun-2005 : Fixed zooming (DG); 045 * 15-Sep-2005 : Changed configure() method to check autoRange flag, 046 * and added ticks to state (DG); 047 * ------------- JFREECHART 1.0.x --------------------------------------------- 048 * 06-Oct-2006 : Updated for deprecations in RegularTimePeriod and 049 * subclasses (DG); 050 * 22-Mar-2007 : Use new defaultAutoRange attribute (DG); 051 * 31-Jul-2007 : Fix for inverted axis labelling (see bug 1763413) (DG); 052 * 08-Apr-2008 : Notify listeners in setRange(Range, boolean, boolean) - fixes 053 * bug 1932146 (DG); 054 * 16-Jan-2009 : Fixed bug 2490803, a problem in the setRange() method (DG); 055 * 02-Mar-2009 : Added locale - see patch 2569670 by Benjamin Bignell (DG); 056 * 02-Mar-2009 : Fixed draw() method to check tickMarksVisible and 057 * tickLabelsVisible (DG); 058 * 19-May-2009 : Fixed FindBugs warnings, patch by Michal Wozniak (DG); 059 * 02-Jul-2013 : Use ParamChecks (DG); 060 * 25-Jul-2013 : Fix for axis timezone and label formatting, bug 1107 (DG); 061 * 01-Aug-2013 : Added attributedLabel override to support superscripts, 062 * subscripts and more (DG); 063 * 064 */ 065 066package org.jfree.chart.axis; 067 068import java.awt.BasicStroke; 069import java.awt.Color; 070import java.awt.FontMetrics; 071import java.awt.Graphics2D; 072import java.awt.Paint; 073import java.awt.Stroke; 074import java.awt.geom.Line2D; 075import java.awt.geom.Rectangle2D; 076import java.io.IOException; 077import java.io.ObjectInputStream; 078import java.io.ObjectOutputStream; 079import java.io.Serializable; 080import java.lang.reflect.Constructor; 081import java.text.DateFormat; 082import java.text.SimpleDateFormat; 083import java.util.ArrayList; 084import java.util.Arrays; 085import java.util.Calendar; 086import java.util.Collections; 087import java.util.Date; 088import java.util.List; 089import java.util.Locale; 090import java.util.TimeZone; 091 092import org.jfree.chart.event.AxisChangeEvent; 093import org.jfree.chart.plot.Plot; 094import org.jfree.chart.plot.PlotRenderingInfo; 095import org.jfree.chart.plot.ValueAxisPlot; 096import org.jfree.chart.text.TextUtils; 097import org.jfree.chart.ui.RectangleEdge; 098import org.jfree.chart.ui.TextAnchor; 099import org.jfree.chart.util.Args; 100import org.jfree.chart.util.PublicCloneable; 101import org.jfree.chart.util.SerialUtils; 102import org.jfree.data.Range; 103import org.jfree.data.time.Day; 104import org.jfree.data.time.Month; 105import org.jfree.data.time.RegularTimePeriod; 106import org.jfree.data.time.Year; 107 108/** 109 * An axis that displays a date scale based on a 110 * {@link org.jfree.data.time.RegularTimePeriod}. This axis works when 111 * displayed across the bottom or top of a plot, but is broken for display at 112 * the left or right of charts. 113 */ 114public class PeriodAxis extends ValueAxis 115 implements Cloneable, PublicCloneable, Serializable { 116 117 /** For serialization. */ 118 private static final long serialVersionUID = 8353295532075872069L; 119 120 /** The first time period in the overall range. */ 121 private RegularTimePeriod first; 122 123 /** The last time period in the overall range. */ 124 private RegularTimePeriod last; 125 126 /** 127 * The time zone used to convert 'first' and 'last' to absolute 128 * milliseconds. 129 */ 130 private TimeZone timeZone; 131 132 /** 133 * The locale (never {@code null}). 134 * 135 * @since 1.0.13 136 */ 137 private Locale locale; 138 139 /** 140 * A calendar used for date manipulations in the current time zone and 141 * locale. 142 */ 143 private Calendar calendar; 144 145 /** 146 * The {@link RegularTimePeriod} subclass used to automatically determine 147 * the axis range. 148 */ 149 private Class autoRangeTimePeriodClass; 150 151 /** 152 * Indicates the {@link RegularTimePeriod} subclass that is used to 153 * determine the spacing of the major tick marks. 154 */ 155 private Class majorTickTimePeriodClass; 156 157 /** 158 * A flag that indicates whether or not tick marks are visible for the 159 * axis. 160 */ 161 private boolean minorTickMarksVisible; 162 163 /** 164 * Indicates the {@link RegularTimePeriod} subclass that is used to 165 * determine the spacing of the minor tick marks. 166 */ 167 private Class minorTickTimePeriodClass; 168 169 /** The length of the tick mark inside the data area (zero permitted). */ 170 private float minorTickMarkInsideLength = 0.0f; 171 172 /** The length of the tick mark outside the data area (zero permitted). */ 173 private float minorTickMarkOutsideLength = 2.0f; 174 175 /** The stroke used to draw tick marks. */ 176 private transient Stroke minorTickMarkStroke = new BasicStroke(0.5f); 177 178 /** The paint used to draw tick marks. */ 179 private transient Paint minorTickMarkPaint = Color.BLACK; 180 181 /** Info for each labeling band. */ 182 private PeriodAxisLabelInfo[] labelInfo; 183 184 /** 185 * Creates a new axis. 186 * 187 * @param label the axis label. 188 */ 189 public PeriodAxis(String label) { 190 this(label, new Day(), new Day()); 191 } 192 193 /** 194 * Creates a new axis. 195 * 196 * @param label the axis label ({@code null} permitted). 197 * @param first the first time period in the axis range 198 * ({@code null} not permitted). 199 * @param last the last time period in the axis range 200 * ({@code null} not permitted). 201 */ 202 public PeriodAxis(String label, 203 RegularTimePeriod first, RegularTimePeriod last) { 204 this(label, first, last, TimeZone.getDefault(), Locale.getDefault()); 205 } 206 207 /** 208 * Creates a new axis. 209 * 210 * @param label the axis label ({@code null} permitted). 211 * @param first the first time period in the axis range 212 * ({@code null} not permitted). 213 * @param last the last time period in the axis range 214 * ({@code null} not permitted). 215 * @param timeZone the time zone ({@code null} not permitted). 216 * @param locale the locale ({@code null} not permitted). 217 * 218 * @since 1.0.13 219 */ 220 public PeriodAxis(String label, RegularTimePeriod first, 221 RegularTimePeriod last, TimeZone timeZone, Locale locale) { 222 super(label, null); 223 Args.nullNotPermitted(timeZone, "timeZone"); 224 Args.nullNotPermitted(locale, "locale"); 225 this.first = first; 226 this.last = last; 227 this.timeZone = timeZone; 228 this.locale = locale; 229 this.calendar = Calendar.getInstance(timeZone, locale); 230 this.first.peg(this.calendar); 231 this.last.peg(this.calendar); 232 this.autoRangeTimePeriodClass = first.getClass(); 233 this.majorTickTimePeriodClass = first.getClass(); 234 this.minorTickMarksVisible = false; 235 this.minorTickTimePeriodClass = RegularTimePeriod.downsize( 236 this.majorTickTimePeriodClass); 237 setAutoRange(true); 238 this.labelInfo = new PeriodAxisLabelInfo[2]; 239 SimpleDateFormat df0 = new SimpleDateFormat("MMM", locale); 240 df0.setTimeZone(timeZone); 241 this.labelInfo[0] = new PeriodAxisLabelInfo(Month.class, df0); 242 SimpleDateFormat df1 = new SimpleDateFormat("yyyy", locale); 243 df1.setTimeZone(timeZone); 244 this.labelInfo[1] = new PeriodAxisLabelInfo(Year.class, df1); 245 } 246 247 /** 248 * Returns the first time period in the axis range. 249 * 250 * @return The first time period (never {@code null}). 251 */ 252 public RegularTimePeriod getFirst() { 253 return this.first; 254 } 255 256 /** 257 * Sets the first time period in the axis range and sends an 258 * {@link AxisChangeEvent} to all registered listeners. 259 * 260 * @param first the time period ({@code null} not permitted). 261 */ 262 public void setFirst(RegularTimePeriod first) { 263 Args.nullNotPermitted(first, "first"); 264 this.first = first; 265 this.first.peg(this.calendar); 266 fireChangeEvent(); 267 } 268 269 /** 270 * Returns the last time period in the axis range. 271 * 272 * @return The last time period (never {@code null}). 273 */ 274 public RegularTimePeriod getLast() { 275 return this.last; 276 } 277 278 /** 279 * Sets the last time period in the axis range and sends an 280 * {@link AxisChangeEvent} to all registered listeners. 281 * 282 * @param last the time period ({@code null} not permitted). 283 */ 284 public void setLast(RegularTimePeriod last) { 285 Args.nullNotPermitted(last, "last"); 286 this.last = last; 287 this.last.peg(this.calendar); 288 fireChangeEvent(); 289 } 290 291 /** 292 * Returns the time zone used to convert the periods defining the axis 293 * range into absolute milliseconds. 294 * 295 * @return The time zone (never {@code null}). 296 */ 297 public TimeZone getTimeZone() { 298 return this.timeZone; 299 } 300 301 /** 302 * Sets the time zone that is used to convert the time periods into 303 * absolute milliseconds. 304 * 305 * @param zone the time zone ({@code null} not permitted). 306 */ 307 public void setTimeZone(TimeZone zone) { 308 Args.nullNotPermitted(zone, "zone"); 309 this.timeZone = zone; 310 this.calendar = Calendar.getInstance(zone, this.locale); 311 this.first.peg(this.calendar); 312 this.last.peg(this.calendar); 313 fireChangeEvent(); 314 } 315 316 /** 317 * Returns the locale for this axis. 318 * 319 * @return The locale (never ({@code null}). 320 * 321 * @since 1.0.13 322 */ 323 public Locale getLocale() { 324 return this.locale; 325 } 326 327 /** 328 * Returns the class used to create the first and last time periods for 329 * the axis range when the auto-range flag is set to {@code true}. 330 * 331 * @return The class (never {@code null}). 332 */ 333 public Class getAutoRangeTimePeriodClass() { 334 return this.autoRangeTimePeriodClass; 335 } 336 337 /** 338 * Sets the class used to create the first and last time periods for the 339 * axis range when the auto-range flag is set to {@code true} and 340 * sends an {@link AxisChangeEvent} to all registered listeners. 341 * 342 * @param c the class ({@code null} not permitted). 343 */ 344 public void setAutoRangeTimePeriodClass(Class c) { 345 Args.nullNotPermitted(c, "c"); 346 this.autoRangeTimePeriodClass = c; 347 fireChangeEvent(); 348 } 349 350 /** 351 * Returns the class that controls the spacing of the major tick marks. 352 * 353 * @return The class (never {@code null}). 354 */ 355 public Class getMajorTickTimePeriodClass() { 356 return this.majorTickTimePeriodClass; 357 } 358 359 /** 360 * Sets the class that controls the spacing of the major tick marks, and 361 * sends an {@link AxisChangeEvent} to all registered listeners. 362 * 363 * @param c the class (a subclass of {@link RegularTimePeriod} is 364 * expected). 365 */ 366 public void setMajorTickTimePeriodClass(Class c) { 367 Args.nullNotPermitted(c, "c"); 368 this.majorTickTimePeriodClass = c; 369 fireChangeEvent(); 370 } 371 372 /** 373 * Returns the flag that controls whether or not minor tick marks 374 * are displayed for the axis. 375 * 376 * @return A boolean. 377 */ 378 @Override 379 public boolean isMinorTickMarksVisible() { 380 return this.minorTickMarksVisible; 381 } 382 383 /** 384 * Sets the flag that controls whether or not minor tick marks 385 * are displayed for the axis, and sends a {@link AxisChangeEvent} 386 * to all registered listeners. 387 * 388 * @param visible the flag. 389 */ 390 @Override 391 public void setMinorTickMarksVisible(boolean visible) { 392 this.minorTickMarksVisible = visible; 393 fireChangeEvent(); 394 } 395 396 /** 397 * Returns the class that controls the spacing of the minor tick marks. 398 * 399 * @return The class (never {@code null}). 400 */ 401 public Class getMinorTickTimePeriodClass() { 402 return this.minorTickTimePeriodClass; 403 } 404 405 /** 406 * Sets the class that controls the spacing of the minor tick marks, and 407 * sends an {@link AxisChangeEvent} to all registered listeners. 408 * 409 * @param c the class (a subclass of {@link RegularTimePeriod} is 410 * expected). 411 */ 412 public void setMinorTickTimePeriodClass(Class c) { 413 Args.nullNotPermitted(c, "c"); 414 this.minorTickTimePeriodClass = c; 415 fireChangeEvent(); 416 } 417 418 /** 419 * Returns the stroke used to display minor tick marks, if they are 420 * visible. 421 * 422 * @return A stroke (never {@code null}). 423 */ 424 public Stroke getMinorTickMarkStroke() { 425 return this.minorTickMarkStroke; 426 } 427 428 /** 429 * Sets the stroke used to display minor tick marks, if they are 430 * visible, and sends a {@link AxisChangeEvent} to all registered 431 * listeners. 432 * 433 * @param stroke the stroke ({@code null} not permitted). 434 */ 435 public void setMinorTickMarkStroke(Stroke stroke) { 436 Args.nullNotPermitted(stroke, "stroke"); 437 this.minorTickMarkStroke = stroke; 438 fireChangeEvent(); 439 } 440 441 /** 442 * Returns the paint used to display minor tick marks, if they are 443 * visible. 444 * 445 * @return A paint (never {@code null}). 446 */ 447 public Paint getMinorTickMarkPaint() { 448 return this.minorTickMarkPaint; 449 } 450 451 /** 452 * Sets the paint used to display minor tick marks, if they are 453 * visible, and sends a {@link AxisChangeEvent} to all registered 454 * listeners. 455 * 456 * @param paint the paint ({@code null} not permitted). 457 */ 458 public void setMinorTickMarkPaint(Paint paint) { 459 Args.nullNotPermitted(paint, "paint"); 460 this.minorTickMarkPaint = paint; 461 fireChangeEvent(); 462 } 463 464 /** 465 * Returns the inside length for the minor tick marks. 466 * 467 * @return The length. 468 */ 469 @Override 470 public float getMinorTickMarkInsideLength() { 471 return this.minorTickMarkInsideLength; 472 } 473 474 /** 475 * Sets the inside length of the minor tick marks and sends an 476 * {@link AxisChangeEvent} to all registered listeners. 477 * 478 * @param length the length. 479 */ 480 @Override 481 public void setMinorTickMarkInsideLength(float length) { 482 this.minorTickMarkInsideLength = length; 483 fireChangeEvent(); 484 } 485 486 /** 487 * Returns the outside length for the minor tick marks. 488 * 489 * @return The length. 490 */ 491 @Override 492 public float getMinorTickMarkOutsideLength() { 493 return this.minorTickMarkOutsideLength; 494 } 495 496 /** 497 * Sets the outside length of the minor tick marks and sends an 498 * {@link AxisChangeEvent} to all registered listeners. 499 * 500 * @param length the length. 501 */ 502 @Override 503 public void setMinorTickMarkOutsideLength(float length) { 504 this.minorTickMarkOutsideLength = length; 505 fireChangeEvent(); 506 } 507 508 /** 509 * Returns an array of label info records. 510 * 511 * @return An array. 512 */ 513 public PeriodAxisLabelInfo[] getLabelInfo() { 514 return this.labelInfo; 515 } 516 517 /** 518 * Sets the array of label info records and sends an 519 * {@link AxisChangeEvent} to all registered listeners. 520 * 521 * @param info the info. 522 */ 523 public void setLabelInfo(PeriodAxisLabelInfo[] info) { 524 this.labelInfo = info; 525 fireChangeEvent(); 526 } 527 528 /** 529 * Sets the range for the axis, if requested, sends an 530 * {@link AxisChangeEvent} to all registered listeners. As a side-effect, 531 * the auto-range flag is set to {@code false} (optional). 532 * 533 * @param range the range ({@code null} not permitted). 534 * @param turnOffAutoRange a flag that controls whether or not the auto 535 * range is turned off. 536 * @param notify a flag that controls whether or not listeners are 537 * notified. 538 */ 539 @Override 540 public void setRange(Range range, boolean turnOffAutoRange, 541 boolean notify) { 542 long upper = Math.round(range.getUpperBound()); 543 long lower = Math.round(range.getLowerBound()); 544 this.first = createInstance(this.autoRangeTimePeriodClass, 545 new Date(lower), this.timeZone, this.locale); 546 this.last = createInstance(this.autoRangeTimePeriodClass, 547 new Date(upper), this.timeZone, this.locale); 548 super.setRange(new Range(this.first.getFirstMillisecond(), 549 this.last.getLastMillisecond() + 1.0), turnOffAutoRange, 550 notify); 551 } 552 553 /** 554 * Configures the axis to work with the current plot. Override this method 555 * to perform any special processing (such as auto-rescaling). 556 */ 557 @Override 558 public void configure() { 559 if (this.isAutoRange()) { 560 autoAdjustRange(); 561 } 562 } 563 564 /** 565 * Estimates the space (height or width) required to draw the axis. 566 * 567 * @param g2 the graphics device. 568 * @param plot the plot that the axis belongs to. 569 * @param plotArea the area within which the plot (including axes) should 570 * be drawn. 571 * @param edge the axis location. 572 * @param space space already reserved. 573 * 574 * @return The space required to draw the axis (including pre-reserved 575 * space). 576 */ 577 @Override 578 public AxisSpace reserveSpace(Graphics2D g2, Plot plot, 579 Rectangle2D plotArea, RectangleEdge edge, AxisSpace space) { 580 // create a new space object if one wasn't supplied... 581 if (space == null) { 582 space = new AxisSpace(); 583 } 584 585 // if the axis is not visible, no additional space is required... 586 if (!isVisible()) { 587 return space; 588 } 589 590 // if the axis has a fixed dimension, return it... 591 double dimension = getFixedDimension(); 592 if (dimension > 0.0) { 593 space.ensureAtLeast(dimension, edge); 594 } 595 596 // get the axis label size and update the space object... 597 Rectangle2D labelEnclosure = getLabelEnclosure(g2, edge); 598 double labelHeight, labelWidth; 599 double tickLabelBandsDimension = 0.0; 600 601 for (int i = 0; i < this.labelInfo.length; i++) { 602 PeriodAxisLabelInfo info = this.labelInfo[i]; 603 FontMetrics fm = g2.getFontMetrics(info.getLabelFont()); 604 tickLabelBandsDimension 605 += info.getPadding().extendHeight(fm.getHeight()); 606 } 607 608 if (RectangleEdge.isTopOrBottom(edge)) { 609 labelHeight = labelEnclosure.getHeight(); 610 space.add(labelHeight + tickLabelBandsDimension, edge); 611 } 612 else if (RectangleEdge.isLeftOrRight(edge)) { 613 labelWidth = labelEnclosure.getWidth(); 614 space.add(labelWidth + tickLabelBandsDimension, edge); 615 } 616 617 // add space for the outer tick labels, if any... 618 double tickMarkSpace = 0.0; 619 if (isTickMarksVisible()) { 620 tickMarkSpace = getTickMarkOutsideLength(); 621 } 622 if (this.minorTickMarksVisible) { 623 tickMarkSpace = Math.max(tickMarkSpace, 624 this.minorTickMarkOutsideLength); 625 } 626 space.add(tickMarkSpace, edge); 627 return space; 628 } 629 630 /** 631 * Draws the axis on a Java 2D graphics device (such as the screen or a 632 * printer). 633 * 634 * @param g2 the graphics device ({@code null} not permitted). 635 * @param cursor the cursor location (determines where to draw the axis). 636 * @param plotArea the area within which the axes and plot should be drawn. 637 * @param dataArea the area within which the data should be drawn. 638 * @param edge the axis location ({@code null} not permitted). 639 * @param plotState collects information about the plot 640 * ({@code null} permitted). 641 * 642 * @return The axis state (never {@code null}). 643 */ 644 @Override 645 public AxisState draw(Graphics2D g2, double cursor, Rectangle2D plotArea, 646 Rectangle2D dataArea, RectangleEdge edge, 647 PlotRenderingInfo plotState) { 648 649 AxisState axisState = new AxisState(cursor); 650 if (isAxisLineVisible()) { 651 drawAxisLine(g2, cursor, dataArea, edge); 652 } 653 if (isTickMarksVisible()) { 654 drawTickMarks(g2, axisState, dataArea, edge); 655 } 656 if (isTickLabelsVisible()) { 657 for (int band = 0; band < this.labelInfo.length; band++) { 658 axisState = drawTickLabels(band, g2, axisState, dataArea, edge); 659 } 660 } 661 662 if (getAttributedLabel() != null) { 663 axisState = drawAttributedLabel(getAttributedLabel(), g2, plotArea, 664 dataArea, edge, axisState); 665 } else { 666 axisState = drawLabel(getLabel(), g2, plotArea, dataArea, edge, 667 axisState); 668 } 669 return axisState; 670 671 } 672 673 /** 674 * Draws the tick marks for the axis. 675 * 676 * @param g2 the graphics device. 677 * @param state the axis state. 678 * @param dataArea the data area. 679 * @param edge the edge. 680 */ 681 protected void drawTickMarks(Graphics2D g2, AxisState state, 682 Rectangle2D dataArea, RectangleEdge edge) { 683 if (RectangleEdge.isTopOrBottom(edge)) { 684 drawTickMarksHorizontal(g2, state, dataArea, edge); 685 } 686 else if (RectangleEdge.isLeftOrRight(edge)) { 687 drawTickMarksVertical(g2, state, dataArea, edge); 688 } 689 } 690 691 /** 692 * Draws the major and minor tick marks for an axis that lies at the top or 693 * bottom of the plot. 694 * 695 * @param g2 the graphics device. 696 * @param state the axis state. 697 * @param dataArea the data area. 698 * @param edge the edge. 699 */ 700 protected void drawTickMarksHorizontal(Graphics2D g2, AxisState state, 701 Rectangle2D dataArea, RectangleEdge edge) { 702 List ticks = new ArrayList(); 703 double x0; 704 double y0 = state.getCursor(); 705 double insideLength = getTickMarkInsideLength(); 706 double outsideLength = getTickMarkOutsideLength(); 707 RegularTimePeriod t = createInstance(this.majorTickTimePeriodClass, 708 this.first.getStart(), getTimeZone(), this.locale); 709 long t0 = t.getFirstMillisecond(); 710 Line2D inside = null; 711 Line2D outside = null; 712 long firstOnAxis = getFirst().getFirstMillisecond(); 713 long lastOnAxis = getLast().getLastMillisecond() + 1; 714 while (t0 <= lastOnAxis) { 715 ticks.add(new NumberTick(Double.valueOf(t0), "", TextAnchor.CENTER, 716 TextAnchor.CENTER, 0.0)); 717 x0 = valueToJava2D(t0, dataArea, edge); 718 if (edge == RectangleEdge.TOP) { 719 inside = new Line2D.Double(x0, y0, x0, y0 + insideLength); 720 outside = new Line2D.Double(x0, y0, x0, y0 - outsideLength); 721 } 722 else if (edge == RectangleEdge.BOTTOM) { 723 inside = new Line2D.Double(x0, y0, x0, y0 - insideLength); 724 outside = new Line2D.Double(x0, y0, x0, y0 + outsideLength); 725 } 726 if (t0 >= firstOnAxis) { 727 g2.setPaint(getTickMarkPaint()); 728 g2.setStroke(getTickMarkStroke()); 729 g2.draw(inside); 730 g2.draw(outside); 731 } 732 // draw minor tick marks 733 if (this.minorTickMarksVisible) { 734 RegularTimePeriod tminor = createInstance( 735 this.minorTickTimePeriodClass, new Date(t0), 736 getTimeZone(), this.locale); 737 long tt0 = tminor.getFirstMillisecond(); 738 while (tt0 < t.getLastMillisecond() 739 && tt0 < lastOnAxis) { 740 double xx0 = valueToJava2D(tt0, dataArea, edge); 741 if (edge == RectangleEdge.TOP) { 742 inside = new Line2D.Double(xx0, y0, xx0, 743 y0 + this.minorTickMarkInsideLength); 744 outside = new Line2D.Double(xx0, y0, xx0, 745 y0 - this.minorTickMarkOutsideLength); 746 } 747 else if (edge == RectangleEdge.BOTTOM) { 748 inside = new Line2D.Double(xx0, y0, xx0, 749 y0 - this.minorTickMarkInsideLength); 750 outside = new Line2D.Double(xx0, y0, xx0, 751 y0 + this.minorTickMarkOutsideLength); 752 } 753 if (tt0 >= firstOnAxis) { 754 g2.setPaint(this.minorTickMarkPaint); 755 g2.setStroke(this.minorTickMarkStroke); 756 g2.draw(inside); 757 g2.draw(outside); 758 } 759 tminor = tminor.next(); 760 tminor.peg(this.calendar); 761 tt0 = tminor.getFirstMillisecond(); 762 } 763 } 764 t = t.next(); 765 t.peg(this.calendar); 766 t0 = t.getFirstMillisecond(); 767 } 768 if (edge == RectangleEdge.TOP) { 769 state.cursorUp(Math.max(outsideLength, 770 this.minorTickMarkOutsideLength)); 771 } 772 else if (edge == RectangleEdge.BOTTOM) { 773 state.cursorDown(Math.max(outsideLength, 774 this.minorTickMarkOutsideLength)); 775 } 776 state.setTicks(ticks); 777 } 778 779 /** 780 * Draws the tick marks for a vertical axis. 781 * 782 * @param g2 the graphics device. 783 * @param state the axis state. 784 * @param dataArea the data area. 785 * @param edge the edge. 786 */ 787 protected void drawTickMarksVertical(Graphics2D g2, AxisState state, 788 Rectangle2D dataArea, RectangleEdge edge) { 789 // FIXME: implement this... 790 } 791 792 /** 793 * Draws the tick labels for one "band" of time periods. 794 * 795 * @param band the band index (zero-based). 796 * @param g2 the graphics device. 797 * @param state the axis state. 798 * @param dataArea the data area. 799 * @param edge the edge where the axis is located. 800 * 801 * @return The updated axis state. 802 */ 803 protected AxisState drawTickLabels(int band, Graphics2D g2, AxisState state, 804 Rectangle2D dataArea, RectangleEdge edge) { 805 806 // work out the initial gap 807 double delta1 = 0.0; 808 FontMetrics fm = g2.getFontMetrics(this.labelInfo[band].getLabelFont()); 809 if (edge == RectangleEdge.BOTTOM) { 810 delta1 = this.labelInfo[band].getPadding().calculateTopOutset( 811 fm.getHeight()); 812 } 813 else if (edge == RectangleEdge.TOP) { 814 delta1 = this.labelInfo[band].getPadding().calculateBottomOutset( 815 fm.getHeight()); 816 } 817 state.moveCursor(delta1, edge); 818 long axisMin = this.first.getFirstMillisecond(); 819 long axisMax = this.last.getLastMillisecond(); 820 g2.setFont(this.labelInfo[band].getLabelFont()); 821 g2.setPaint(this.labelInfo[band].getLabelPaint()); 822 823 // work out the number of periods to skip for labelling 824 RegularTimePeriod p1 = this.labelInfo[band].createInstance( 825 new Date(axisMin), this.timeZone, this.locale); 826 RegularTimePeriod p2 = this.labelInfo[band].createInstance( 827 new Date(axisMax), this.timeZone, this.locale); 828 DateFormat df = this.labelInfo[band].getDateFormat(); 829 df.setTimeZone(this.timeZone); 830 String label1 = df.format(new Date(p1.getMiddleMillisecond())); 831 String label2 = df.format(new Date(p2.getMiddleMillisecond())); 832 Rectangle2D b1 = TextUtils.getTextBounds(label1, g2, 833 g2.getFontMetrics()); 834 Rectangle2D b2 = TextUtils.getTextBounds(label2, g2, 835 g2.getFontMetrics()); 836 double w = Math.max(b1.getWidth(), b2.getWidth()); 837 long ww = Math.round(java2DToValue(dataArea.getX() + w + 5.0, 838 dataArea, edge)); 839 if (isInverted()) { 840 ww = axisMax - ww; 841 } 842 else { 843 ww = ww - axisMin; 844 } 845 long length = p1.getLastMillisecond() 846 - p1.getFirstMillisecond(); 847 int periods = (int) (ww / length) + 1; 848 849 RegularTimePeriod p = this.labelInfo[band].createInstance( 850 new Date(axisMin), this.timeZone, this.locale); 851 Rectangle2D b = null; 852 long lastXX = 0L; 853 float y = (float) (state.getCursor()); 854 TextAnchor anchor = TextAnchor.TOP_CENTER; 855 float yDelta = (float) b1.getHeight(); 856 if (edge == RectangleEdge.TOP) { 857 anchor = TextAnchor.BOTTOM_CENTER; 858 yDelta = -yDelta; 859 } 860 while (p.getFirstMillisecond() <= axisMax) { 861 float x = (float) valueToJava2D(p.getMiddleMillisecond(), dataArea, 862 edge); 863 String label = df.format(new Date(p.getMiddleMillisecond())); 864 long first = p.getFirstMillisecond(); 865 long last = p.getLastMillisecond(); 866 if (last > axisMax) { 867 // this is the last period, but it is only partially visible 868 // so check that the label will fit before displaying it... 869 Rectangle2D bb = TextUtils.getTextBounds(label, g2, 870 g2.getFontMetrics()); 871 if ((x + bb.getWidth() / 2) > dataArea.getMaxX()) { 872 float xstart = (float) valueToJava2D(Math.max(first, 873 axisMin), dataArea, edge); 874 if (bb.getWidth() < (dataArea.getMaxX() - xstart)) { 875 x = ((float) dataArea.getMaxX() + xstart) / 2.0f; 876 } 877 else { 878 label = null; 879 } 880 } 881 } 882 if (first < axisMin) { 883 // this is the first period, but it is only partially visible 884 // so check that the label will fit before displaying it... 885 Rectangle2D bb = TextUtils.getTextBounds(label, g2, 886 g2.getFontMetrics()); 887 if ((x - bb.getWidth() / 2) < dataArea.getX()) { 888 float xlast = (float) valueToJava2D(Math.min(last, 889 axisMax), dataArea, edge); 890 if (bb.getWidth() < (xlast - dataArea.getX())) { 891 x = (xlast + (float) dataArea.getX()) / 2.0f; 892 } 893 else { 894 label = null; 895 } 896 } 897 898 } 899 if (label != null) { 900 g2.setPaint(this.labelInfo[band].getLabelPaint()); 901 b = TextUtils.drawAlignedString(label, g2, x, y, anchor); 902 } 903 if (lastXX > 0L) { 904 if (this.labelInfo[band].getDrawDividers()) { 905 long nextXX = p.getFirstMillisecond(); 906 long mid = (lastXX + nextXX) / 2; 907 float mid2d = (float) valueToJava2D(mid, dataArea, edge); 908 g2.setStroke(this.labelInfo[band].getDividerStroke()); 909 g2.setPaint(this.labelInfo[band].getDividerPaint()); 910 g2.draw(new Line2D.Float(mid2d, y, mid2d, y + yDelta)); 911 } 912 } 913 lastXX = last; 914 for (int i = 0; i < periods; i++) { 915 p = p.next(); 916 } 917 p.peg(this.calendar); 918 } 919 double used = 0.0; 920 if (b != null) { 921 used = b.getHeight(); 922 // work out the trailing gap 923 if (edge == RectangleEdge.BOTTOM) { 924 used += this.labelInfo[band].getPadding().calculateBottomOutset( 925 fm.getHeight()); 926 } 927 else if (edge == RectangleEdge.TOP) { 928 used += this.labelInfo[band].getPadding().calculateTopOutset( 929 fm.getHeight()); 930 } 931 } 932 state.moveCursor(used, edge); 933 return state; 934 } 935 936 /** 937 * Calculates the positions of the ticks for the axis, storing the results 938 * in the tick list (ready for drawing). 939 * 940 * @param g2 the graphics device. 941 * @param state the axis state. 942 * @param dataArea the area inside the axes. 943 * @param edge the edge on which the axis is located. 944 * 945 * @return The list of ticks. 946 */ 947 @Override 948 public List refreshTicks(Graphics2D g2, AxisState state, 949 Rectangle2D dataArea, RectangleEdge edge) { 950 return Collections.EMPTY_LIST; 951 } 952 953 /** 954 * Converts a data value to a coordinate in Java2D space, assuming that the 955 * axis runs along one edge of the specified dataArea. 956 * <p> 957 * Note that it is possible for the coordinate to fall outside the area. 958 * 959 * @param value the data value. 960 * @param area the area for plotting the data. 961 * @param edge the edge along which the axis lies. 962 * 963 * @return The Java2D coordinate. 964 */ 965 @Override 966 public double valueToJava2D(double value, Rectangle2D area, 967 RectangleEdge edge) { 968 969 double result = Double.NaN; 970 double axisMin = this.first.getFirstMillisecond(); 971 double axisMax = this.last.getLastMillisecond(); 972 if (RectangleEdge.isTopOrBottom(edge)) { 973 double minX = area.getX(); 974 double maxX = area.getMaxX(); 975 if (isInverted()) { 976 result = maxX + ((value - axisMin) / (axisMax - axisMin)) 977 * (minX - maxX); 978 } 979 else { 980 result = minX + ((value - axisMin) / (axisMax - axisMin)) 981 * (maxX - minX); 982 } 983 } 984 else if (RectangleEdge.isLeftOrRight(edge)) { 985 double minY = area.getMinY(); 986 double maxY = area.getMaxY(); 987 if (isInverted()) { 988 result = minY + (((value - axisMin) / (axisMax - axisMin)) 989 * (maxY - minY)); 990 } 991 else { 992 result = maxY - (((value - axisMin) / (axisMax - axisMin)) 993 * (maxY - minY)); 994 } 995 } 996 return result; 997 998 } 999 1000 /** 1001 * Converts a coordinate in Java2D space to the corresponding data value, 1002 * assuming that the axis runs along one edge of the specified dataArea. 1003 * 1004 * @param java2DValue the coordinate in Java2D space. 1005 * @param area the area in which the data is plotted. 1006 * @param edge the edge along which the axis lies. 1007 * 1008 * @return The data value. 1009 */ 1010 @Override 1011 public double java2DToValue(double java2DValue, Rectangle2D area, 1012 RectangleEdge edge) { 1013 1014 double result; 1015 double min = 0.0; 1016 double max = 0.0; 1017 double axisMin = this.first.getFirstMillisecond(); 1018 double axisMax = this.last.getLastMillisecond(); 1019 if (RectangleEdge.isTopOrBottom(edge)) { 1020 min = area.getX(); 1021 max = area.getMaxX(); 1022 } 1023 else if (RectangleEdge.isLeftOrRight(edge)) { 1024 min = area.getMaxY(); 1025 max = area.getY(); 1026 } 1027 if (isInverted()) { 1028 result = axisMax - ((java2DValue - min) / (max - min) 1029 * (axisMax - axisMin)); 1030 } 1031 else { 1032 result = axisMin + ((java2DValue - min) / (max - min) 1033 * (axisMax - axisMin)); 1034 } 1035 return result; 1036 } 1037 1038 /** 1039 * Rescales the axis to ensure that all data is visible. 1040 */ 1041 @Override 1042 protected void autoAdjustRange() { 1043 1044 Plot plot = getPlot(); 1045 if (plot == null) { 1046 return; // no plot, no data 1047 } 1048 1049 if (plot instanceof ValueAxisPlot) { 1050 ValueAxisPlot vap = (ValueAxisPlot) plot; 1051 1052 Range r = vap.getDataRange(this); 1053 if (r == null) { 1054 r = getDefaultAutoRange(); 1055 } 1056 1057 long upper = Math.round(r.getUpperBound()); 1058 long lower = Math.round(r.getLowerBound()); 1059 this.first = createInstance(this.autoRangeTimePeriodClass, 1060 new Date(lower), this.timeZone, this.locale); 1061 this.last = createInstance(this.autoRangeTimePeriodClass, 1062 new Date(upper), this.timeZone, this.locale); 1063 setRange(r, false, false); 1064 } 1065 1066 } 1067 1068 /** 1069 * Tests the axis for equality with an arbitrary object. 1070 * 1071 * @param obj the object ({@code null} permitted). 1072 * 1073 * @return A boolean. 1074 */ 1075 @Override 1076 public boolean equals(Object obj) { 1077 if (obj == this) { 1078 return true; 1079 } 1080 if (!(obj instanceof PeriodAxis)) { 1081 return false; 1082 } 1083 PeriodAxis that = (PeriodAxis) obj; 1084 if (!this.first.equals(that.first)) { 1085 return false; 1086 } 1087 if (!this.last.equals(that.last)) { 1088 return false; 1089 } 1090 if (!this.timeZone.equals(that.timeZone)) { 1091 return false; 1092 } 1093 if (!this.locale.equals(that.locale)) { 1094 return false; 1095 } 1096 if (!this.autoRangeTimePeriodClass.equals( 1097 that.autoRangeTimePeriodClass)) { 1098 return false; 1099 } 1100 if (!(isMinorTickMarksVisible() == that.isMinorTickMarksVisible())) { 1101 return false; 1102 } 1103 if (!this.majorTickTimePeriodClass.equals( 1104 that.majorTickTimePeriodClass)) { 1105 return false; 1106 } 1107 if (!this.minorTickTimePeriodClass.equals( 1108 that.minorTickTimePeriodClass)) { 1109 return false; 1110 } 1111 if (!this.minorTickMarkPaint.equals(that.minorTickMarkPaint)) { 1112 return false; 1113 } 1114 if (!this.minorTickMarkStroke.equals(that.minorTickMarkStroke)) { 1115 return false; 1116 } 1117 if (!Arrays.equals(this.labelInfo, that.labelInfo)) { 1118 return false; 1119 } 1120 return super.equals(obj); 1121 } 1122 1123 /** 1124 * Returns a hash code for this object. 1125 * 1126 * @return A hash code. 1127 */ 1128 @Override 1129 public int hashCode() { 1130 return super.hashCode(); 1131 } 1132 1133 /** 1134 * Returns a clone of the axis. 1135 * 1136 * @return A clone. 1137 * 1138 * @throws CloneNotSupportedException this class is cloneable, but 1139 * subclasses may not be. 1140 */ 1141 @Override 1142 public Object clone() throws CloneNotSupportedException { 1143 PeriodAxis clone = (PeriodAxis) super.clone(); 1144 clone.timeZone = (TimeZone) this.timeZone.clone(); 1145 clone.labelInfo = (PeriodAxisLabelInfo[]) this.labelInfo.clone(); 1146 return clone; 1147 } 1148 1149 /** 1150 * A utility method used to create a particular subclass of the 1151 * {@link RegularTimePeriod} class that includes the specified millisecond, 1152 * assuming the specified time zone. 1153 * 1154 * @param periodClass the class. 1155 * @param millisecond the time. 1156 * @param zone the time zone. 1157 * @param locale the locale. 1158 * 1159 * @return The time period. 1160 */ 1161 private RegularTimePeriod createInstance(Class periodClass, 1162 Date millisecond, TimeZone zone, Locale locale) { 1163 RegularTimePeriod result = null; 1164 try { 1165 Constructor c = periodClass.getDeclaredConstructor(new Class[] { 1166 Date.class, TimeZone.class, Locale.class}); 1167 result = (RegularTimePeriod) c.newInstance(new Object[] { 1168 millisecond, zone, locale}); 1169 } 1170 catch (Exception e) { 1171 try { 1172 Constructor c = periodClass.getDeclaredConstructor(new Class[] { 1173 Date.class}); 1174 result = (RegularTimePeriod) c.newInstance(new Object[] { 1175 millisecond}); 1176 } 1177 catch (Exception e2) { 1178 // do nothing 1179 } 1180 } 1181 return result; 1182 } 1183 1184 /** 1185 * Provides serialization support. 1186 * 1187 * @param stream the output stream. 1188 * 1189 * @throws IOException if there is an I/O error. 1190 */ 1191 private void writeObject(ObjectOutputStream stream) throws IOException { 1192 stream.defaultWriteObject(); 1193 SerialUtils.writeStroke(this.minorTickMarkStroke, stream); 1194 SerialUtils.writePaint(this.minorTickMarkPaint, stream); 1195 } 1196 1197 /** 1198 * Provides serialization support. 1199 * 1200 * @param stream the input stream. 1201 * 1202 * @throws IOException if there is an I/O error. 1203 * @throws ClassNotFoundException if there is a classpath problem. 1204 */ 1205 private void readObject(ObjectInputStream stream) 1206 throws IOException, ClassNotFoundException { 1207 stream.defaultReadObject(); 1208 this.minorTickMarkStroke = SerialUtils.readStroke(stream); 1209 this.minorTickMarkPaint = SerialUtils.readPaint(stream); 1210 } 1211 1212}