org.jfree.data.time.DateRange Java Examples

The following examples show how to use org.jfree.data.time.DateRange. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DateAxisTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test that the setRange() method works.
 */
public void testSetRange() {

    DateAxis axis = new DateAxis("Test Axis");
    Calendar calendar = Calendar.getInstance();
    calendar.set(1999, Calendar.JANUARY, 3);
    Date d1 = calendar.getTime();
    calendar.set(1999, Calendar.JANUARY, 31);
    Date d2 = calendar.getTime();
    axis.setRange(d1, d2);

    DateRange range = (DateRange) axis.getRange();
    assertEquals(d1, range.getLowerDate());
    assertEquals(d2, range.getUpperDate());

}
 
Example #2
Source File: DateAxisTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test that the setRange() method works.
 */
public void testSetRange() {

    DateAxis axis = new DateAxis("Test Axis");
    Calendar calendar = Calendar.getInstance();
    calendar.set(1999, Calendar.JANUARY, 3);
    Date d1 = calendar.getTime();
    calendar.set(1999, Calendar.JANUARY, 31);
    Date d2 = calendar.getTime();
    axis.setRange(d1, d2);

    DateRange range = (DateRange) axis.getRange();
    assertEquals(d1, range.getLowerDate());
    assertEquals(d2, range.getUpperDate());

}
 
Example #3
Source File: DateAxisTest.java    From ECG-Viewer with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test that the setRange() method works.
 */
@Test
public void testSetRange() {

    DateAxis axis = new DateAxis("Test Axis");
    Calendar calendar = Calendar.getInstance();
    calendar.set(1999, Calendar.JANUARY, 3);
    Date d1 = calendar.getTime();
    calendar.set(1999, Calendar.JANUARY, 31);
    Date d2 = calendar.getTime();
    axis.setRange(d1, d2);

    DateRange range = (DateRange) axis.getRange();
    assertEquals(d1, range.getLowerDate());
    assertEquals(d2, range.getUpperDate());

}
 
Example #4
Source File: DateAxisTest.java    From SIMVA-SoS with Apache License 2.0 6 votes vote down vote up
/**
 * Test that the setRange() method works.
 */
@Test
public void testSetRange() {

    DateAxis axis = new DateAxis("Test Axis");
    Calendar calendar = Calendar.getInstance();
    calendar.set(1999, Calendar.JANUARY, 3);
    Date d1 = calendar.getTime();
    calendar.set(1999, Calendar.JANUARY, 31);
    Date d2 = calendar.getTime();
    axis.setRange(d1, d2);

    DateRange range = (DateRange) axis.getRange();
    assertEquals(d1, range.getLowerDate());
    assertEquals(d2, range.getUpperDate());

}
 
Example #5
Source File: DateAxisTest.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Test that the setRange() method works.
 */
@Test
public void testSetRange() {

    DateAxis axis = new DateAxis("Test Axis");
    Calendar calendar = Calendar.getInstance();
    calendar.set(1999, Calendar.JANUARY, 3);
    Date d1 = calendar.getTime();
    calendar.set(1999, Calendar.JANUARY, 31);
    Date d2 = calendar.getTime();
    axis.setRange(d1, d2);

    DateRange range = (DateRange) axis.getRange();
    assertEquals(d1, range.getLowerDate());
    assertEquals(d2, range.getUpperDate());

}
 
Example #6
Source File: DateAxisTest.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Test that the setRange() method works.
 */
@Test
public void testSetRange() {

    DateAxis axis = new DateAxis("Test Axis");
    Calendar calendar = Calendar.getInstance();
    calendar.set(1999, Calendar.JANUARY, 3);
    Date d1 = calendar.getTime();
    calendar.set(1999, Calendar.JANUARY, 31);
    Date d2 = calendar.getTime();
    axis.setRange(d1, d2);

    DateRange range = (DateRange) axis.getRange();
    assertEquals(d1, range.getLowerDate());
    assertEquals(d2, range.getUpperDate());

}
 
Example #7
Source File: DateAxisTest.java    From openstock with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Test that the setRange() method works.
 */
@Test
public void testSetRange() {

    DateAxis axis = new DateAxis("Test Axis");
    Calendar calendar = Calendar.getInstance();
    calendar.set(1999, Calendar.JANUARY, 3);
    Date d1 = calendar.getTime();
    calendar.set(1999, Calendar.JANUARY, 31);
    Date d2 = calendar.getTime();
    axis.setRange(d1, d2);

    DateRange range = (DateRange) axis.getRange();
    assertEquals(d1, range.getLowerDate());
    assertEquals(d2, range.getUpperDate());

}
 
Example #8
Source File: DateAxis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified 
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we 
 * just look at two values: the lower bound and the upper bound for the 
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 */
private double estimateMaximumTickLabelHeight(Graphics2D g2, 
                                              DateTickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getTop() + tickLabelInsets.getBottom();

    Font tickLabelFont = getTickLabelFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = tickLabelFont.getLineMetrics("ABCxyz", frc);
    if (!isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of 
        // the font)...
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        DateRange range = (DateRange) getRange();
        Date lower = range.getLowerDate();
        Date upper = range.getUpperDate();
        String lowerStr = null;
        String upperStr = null;
        DateFormat formatter = getDateFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.dateToString(lower);
            upperStr = unit.dateToString(upper);
        }
        FontMetrics fm = g2.getFontMetrics(tickLabelFont);
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #9
Source File: DateAxis.java    From opensim-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified 
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we
 * just look at two values: the lower bound and the upper bound for the 
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 */
private double estimateMaximumTickLabelWidth(Graphics2D g2, 
                                             DateTickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getLeft() + tickLabelInsets.getRight();

    Font tickLabelFont = getTickLabelFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = tickLabelFont.getLineMetrics("ABCxyz", frc);
    if (isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of 
        // the font)...
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        DateRange range = (DateRange) getRange();
        Date lower = range.getLowerDate();
        Date upper = range.getUpperDate();
        String lowerStr = null;
        String upperStr = null;
        DateFormat formatter = getDateFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.dateToString(lower);
            upperStr = unit.dateToString(upper);
        }
        FontMetrics fm = g2.getFontMetrics(tickLabelFont);
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #10
Source File: PeriodAxisTest.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * A test for bug 1932146.
 */
@Test
public void test1932146() {
    PeriodAxis axis = new PeriodAxis("TestAxis");
    axis.addChangeListener(this);
    this.lastEvent = null;
    axis.setRange(new DateRange(0L, 1000L));
    assertTrue(this.lastEvent != null);
}
 
Example #11
Source File: DateAxis.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we
 * just look at two values: the lower bound and the upper bound for the
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 */
private double estimateMaximumTickLabelWidth(Graphics2D g2, 
        DateTickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getLeft() + tickLabelInsets.getRight();

    Font tickLabelFont = getTickLabelFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = tickLabelFont.getLineMetrics("ABCxyz", frc);
    if (isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of
        // the font)...
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        DateRange range = (DateRange) getRange();
        Date lower = range.getLowerDate();
        Date upper = range.getUpperDate();
        String lowerStr, upperStr;
        DateFormat formatter = getDateFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.dateToString(lower);
            upperStr = unit.dateToString(upper);
        }
        FontMetrics fm = g2.getFontMetrics(tickLabelFont);
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #12
Source File: DateAxis.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Sets the axis range and sends an {@link AxisChangeEvent} to all
 * registered listeners.
 *
 * @param lower  the lower bound for the axis.
 * @param upper  the upper bound for the axis.
 */
@Override
public void setRange(double lower, double upper) {
    if (lower >= upper) {
        throw new IllegalArgumentException("Requires 'lower' < 'upper'.");
    }
    setRange(new DateRange(lower, upper));
}
 
Example #13
Source File: DateAxis.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Translates a Java2D coordinate into the corresponding data value.  To
 * perform this translation, you need to know the area used for plotting
 * data, and which edge the axis is located on.
 *
 * @param java2DValue  the coordinate in Java2D space.
 * @param area  the rectangle (in Java2D space) where the data is to be
 *              plotted.
 * @param edge  the axis location.
 *
 * @return A data value.
 */
@Override
public double java2DToValue(double java2DValue, Rectangle2D area, 
        RectangleEdge edge) {

    DateRange range = (DateRange) getRange();
    double axisMin = this.timeline.toTimelineValue(range.getLowerMillis());
    double axisMax = this.timeline.toTimelineValue(range.getUpperMillis());

    double min = 0.0;
    double max = 0.0;
    if (RectangleEdge.isTopOrBottom(edge)) {
        min = area.getX();
        max = area.getMaxX();
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        min = area.getMaxY();
        max = area.getY();
    }

    double result;
    if (isInverted()) {
         result = axisMax - ((java2DValue - min) / (max - min)
                  * (axisMax - axisMin));
    }
    else {
         result = axisMin + ((java2DValue - min) / (max - min)
                  * (axisMax - axisMin));
    }

    return this.timeline.toMillisecond((long) result);
}
 
Example #14
Source File: DateAxis.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Translates the data value to the display coordinates (Java 2D User Space)
 * of the chart.
 *
 * @param value  the date to be plotted.
 * @param area  the rectangle (in Java2D space) where the data is to be
 *              plotted.
 * @param edge  the axis location.
 *
 * @return The coordinate corresponding to the supplied data value.
 */
@Override
public double valueToJava2D(double value, Rectangle2D area,
        RectangleEdge edge) {

    value = this.timeline.toTimelineValue((long) value);

    DateRange range = (DateRange) getRange();
    double axisMin = this.timeline.toTimelineValue(range.getLowerMillis());
    double axisMax = this.timeline.toTimelineValue(range.getUpperMillis());
    double result = 0.0;
    if (RectangleEdge.isTopOrBottom(edge)) {
        double minX = area.getX();
        double maxX = area.getMaxX();
        if (isInverted()) {
            result = maxX + ((value - axisMin) / (axisMax - axisMin))
                     * (minX - maxX);
        }
        else {
            result = minX + ((value - axisMin) / (axisMax - axisMin))
                     * (maxX - minX);
        }
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        double minY = area.getMinY();
        double maxY = area.getMaxY();
        if (isInverted()) {
            result = minY + (((value - axisMin) / (axisMax - axisMin))
                     * (maxY - minY));
        }
        else {
            result = maxY - (((value - axisMin) / (axisMax - axisMin))
                     * (maxY - minY));
        }
    }
    return result;
}
 
Example #15
Source File: DateAxis.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Sets the axis range and sends an {@link AxisChangeEvent} to all
 * registered listeners.
 *
 * @param lower  the lower bound for the axis.
 * @param upper  the upper bound for the axis.
 */
@Override
public void setRange(double lower, double upper) {
    if (lower >= upper) {
        throw new IllegalArgumentException("Requires 'lower' < 'upper'.");
    }
    setRange(new DateRange(lower, upper));
}
 
Example #16
Source File: DateAxis.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Translates the data value to the display coordinates (Java 2D User Space)
 * of the chart.
 *
 * @param value  the date to be plotted.
 * @param area  the rectangle (in Java2D space) where the data is to be
 *              plotted.
 * @param edge  the axis location.
 *
 * @return The coordinate corresponding to the supplied data value.
 */
@Override
public double valueToJava2D(double value, Rectangle2D area,
        RectangleEdge edge) {

    value = this.timeline.toTimelineValue((long) value);

    DateRange range = (DateRange) getRange();
    double axisMin = this.timeline.toTimelineValue(range.getLowerMillis());
    double axisMax = this.timeline.toTimelineValue(range.getUpperMillis());
    double result = 0.0;
    if (RectangleEdge.isTopOrBottom(edge)) {
        double minX = area.getX();
        double maxX = area.getMaxX();
        if (isInverted()) {
            result = maxX + ((value - axisMin) / (axisMax - axisMin))
                     * (minX - maxX);
        }
        else {
            result = minX + ((value - axisMin) / (axisMax - axisMin))
                     * (maxX - minX);
        }
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        double minY = area.getMinY();
        double maxY = area.getMaxY();
        if (isInverted()) {
            result = minY + (((value - axisMin) / (axisMax - axisMin))
                     * (maxY - minY));
        }
        else {
            result = maxY - (((value - axisMin) / (axisMax - axisMin))
                     * (maxY - minY));
        }
    }
    return result;
}
 
Example #17
Source File: DateAxis.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Translates a Java2D coordinate into the corresponding data value.  To
 * perform this translation, you need to know the area used for plotting
 * data, and which edge the axis is located on.
 *
 * @param java2DValue  the coordinate in Java2D space.
 * @param area  the rectangle (in Java2D space) where the data is to be
 *              plotted.
 * @param edge  the axis location.
 *
 * @return A data value.
 */
@Override
public double java2DToValue(double java2DValue, Rectangle2D area, 
        RectangleEdge edge) {

    DateRange range = (DateRange) getRange();
    double axisMin = this.timeline.toTimelineValue(range.getLowerMillis());
    double axisMax = this.timeline.toTimelineValue(range.getUpperMillis());

    double min = 0.0;
    double max = 0.0;
    if (RectangleEdge.isTopOrBottom(edge)) {
        min = area.getX();
        max = area.getMaxX();
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        min = area.getMaxY();
        max = area.getY();
    }

    double result;
    if (isInverted()) {
         result = axisMax - ((java2DValue - min) / (max - min)
                  * (axisMax - axisMin));
    }
    else {
         result = axisMin + ((java2DValue - min) / (max - min)
                  * (axisMax - axisMin));
    }

    return this.timeline.toMillisecond((long) result);
}
 
Example #18
Source File: DateAxis.java    From opensim-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Translates a Java2D coordinate into the corresponding data value.  To 
 * perform this translation, you need to know the area used for plotting 
 * data, and which edge the axis is located on.
 *
 * @param java2DValue  the coordinate in Java2D space.
 * @param area  the rectangle (in Java2D space) where the data is to be 
 *              plotted.
 * @param edge  the axis location.
 *
 * @return A data value.
 */
public double java2DToValue(double java2DValue, Rectangle2D area, 
                            RectangleEdge edge) {
    
    DateRange range = (DateRange) getRange();
    double axisMin = this.timeline.toTimelineValue(range.getLowerDate());
    double axisMax = this.timeline.toTimelineValue(range.getUpperDate());

    double min = 0.0;
    double max = 0.0;
    if (RectangleEdge.isTopOrBottom(edge)) {
        min = area.getX();
        max = area.getMaxX();
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        min = area.getMaxY();
        max = area.getY();
    }

    double result;
    if (isInverted()) {
         result = axisMax - ((java2DValue - min) / (max - min) 
                  * (axisMax - axisMin));
    }
    else {
         result = axisMin + ((java2DValue - min) / (max - min) 
                  * (axisMax - axisMin));
    }

    return this.timeline.toMillisecond((long) result); 
}
 
Example #19
Source File: DateAxis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified 
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we
 * just look at two values: the lower bound and the upper bound for the 
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 */
private double estimateMaximumTickLabelWidth(Graphics2D g2, 
                                             DateTickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getLeft() + tickLabelInsets.getRight();

    Font tickLabelFont = getTickLabelFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = tickLabelFont.getLineMetrics("ABCxyz", frc);
    if (isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of 
        // the font)...
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        DateRange range = (DateRange) getRange();
        Date lower = range.getLowerDate();
        Date upper = range.getUpperDate();
        String lowerStr = null;
        String upperStr = null;
        DateFormat formatter = getDateFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.dateToString(lower);
            upperStr = unit.dateToString(upper);
        }
        FontMetrics fm = g2.getFontMetrics(tickLabelFont);
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #20
Source File: DateAxis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a Java2D coordinate into the corresponding data value.  To 
 * perform this translation, you need to know the area used for plotting 
 * data, and which edge the axis is located on.
 *
 * @param java2DValue  the coordinate in Java2D space.
 * @param area  the rectangle (in Java2D space) where the data is to be 
 *              plotted.
 * @param edge  the axis location.
 *
 * @return A data value.
 */
public double java2DToValue(double java2DValue, Rectangle2D area, 
                            RectangleEdge edge) {
    
    DateRange range = (DateRange) getRange();
    double axisMin = this.timeline.toTimelineValue(range.getLowerDate());
    double axisMax = this.timeline.toTimelineValue(range.getUpperDate());

    double min = 0.0;
    double max = 0.0;
    if (RectangleEdge.isTopOrBottom(edge)) {
        min = area.getX();
        max = area.getMaxX();
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        min = area.getMaxY();
        max = area.getY();
    }

    double result;
    if (isInverted()) {
         result = axisMax - ((java2DValue - min) / (max - min) 
                  * (axisMax - axisMin));
    }
    else {
         result = axisMin + ((java2DValue - min) / (max - min) 
                  * (axisMax - axisMin));
    }

    return this.timeline.toMillisecond((long) result); 
}
 
Example #21
Source File: DateAxis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates the data value to the display coordinates (Java 2D User Space)
 * of the chart.
 *
 * @param value  the date to be plotted.
 * @param area  the rectangle (in Java2D space) where the data is to be 
 *              plotted.
 * @param edge  the axis location.
 *
 * @return The coordinate corresponding to the supplied data value.
 */
public double valueToJava2D(double value, Rectangle2D area, 
                            RectangleEdge edge) {
    
    value = this.timeline.toTimelineValue((long) value);

    DateRange range = (DateRange) getRange();
    double axisMin = this.timeline.toTimelineValue(range.getLowerDate());
    double axisMax = this.timeline.toTimelineValue(range.getUpperDate());
    double result = 0.0;
    if (RectangleEdge.isTopOrBottom(edge)) {
        double minX = area.getX();
        double maxX = area.getMaxX();
        if (isInverted()) {
            result = maxX + ((value - axisMin) / (axisMax - axisMin)) 
                     * (minX - maxX);
        }
        else {
            result = minX + ((value - axisMin) / (axisMax - axisMin)) 
                     * (maxX - minX);
        }
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        double minY = area.getMinY();
        double maxY = area.getMaxY();
        if (isInverted()) {
            result = minY + (((value - axisMin) / (axisMax - axisMin)) 
                     * (maxY - minY));
        }
        else {
            result = maxY - (((value - axisMin) / (axisMax - axisMin)) 
                     * (maxY - minY));
        }
    }
    return result;

}
 
Example #22
Source File: DateAxis.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we
 * just look at two values: the lower bound and the upper bound for the
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 */
private double estimateMaximumTickLabelWidth(Graphics2D g2, 
        DateTickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getLeft() + tickLabelInsets.getRight();

    Font tickLabelFont = getTickLabelFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = tickLabelFont.getLineMetrics("ABCxyz", frc);
    if (isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of
        // the font)...
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        DateRange range = (DateRange) getRange();
        Date lower = range.getLowerDate();
        Date upper = range.getUpperDate();
        String lowerStr, upperStr;
        DateFormat formatter = getDateFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.dateToString(lower);
            upperStr = unit.dateToString(upper);
        }
        FontMetrics fm = g2.getFontMetrics(tickLabelFont);
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #23
Source File: PeriodAxisTest.java    From openstock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * A test for bug 1932146.
 */
@Test
public void test1932146() {
    PeriodAxis axis = new PeriodAxis("TestAxis");
    axis.addChangeListener(this);
    this.lastEvent = null;
    axis.setRange(new DateRange(0L, 1000L));
    assertTrue(this.lastEvent != null);
}
 
Example #24
Source File: DateAxis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we
 * just look at two values: the lower bound and the upper bound for the
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 */
private double estimateMaximumTickLabelHeight(Graphics2D g2,
                                              DateTickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getTop() + tickLabelInsets.getBottom();

    Font tickLabelFont = getTickLabelFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = tickLabelFont.getLineMetrics("ABCxyz", frc);
    if (!isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of
        // the font)...
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        DateRange range = (DateRange) getRange();
        Date lower = range.getLowerDate();
        Date upper = range.getUpperDate();
        String lowerStr = null;
        String upperStr = null;
        DateFormat formatter = getDateFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.dateToString(lower);
            upperStr = unit.dateToString(upper);
        }
        FontMetrics fm = g2.getFontMetrics(tickLabelFont);
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #25
Source File: DateAxis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we
 * just look at two values: the lower bound and the upper bound for the
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 */
private double estimateMaximumTickLabelWidth(Graphics2D g2,
                                             DateTickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getLeft() + tickLabelInsets.getRight();

    Font tickLabelFont = getTickLabelFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = tickLabelFont.getLineMetrics("ABCxyz", frc);
    if (isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of
        // the font)...
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        DateRange range = (DateRange) getRange();
        Date lower = range.getLowerDate();
        Date upper = range.getUpperDate();
        String lowerStr = null;
        String upperStr = null;
        DateFormat formatter = getDateFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.dateToString(lower);
            upperStr = unit.dateToString(upper);
        }
        FontMetrics fm = g2.getFontMetrics(tickLabelFont);
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #26
Source File: DateAxis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates a Java2D coordinate into the corresponding data value.  To
 * perform this translation, you need to know the area used for plotting
 * data, and which edge the axis is located on.
 *
 * @param java2DValue  the coordinate in Java2D space.
 * @param area  the rectangle (in Java2D space) where the data is to be
 *              plotted.
 * @param edge  the axis location.
 *
 * @return A data value.
 */
public double java2DToValue(double java2DValue, Rectangle2D area,
                            RectangleEdge edge) {

    DateRange range = (DateRange) getRange();
    double axisMin = this.timeline.toTimelineValue(range.getLowerMillis());
    double axisMax = this.timeline.toTimelineValue(range.getUpperMillis());

    double min = 0.0;
    double max = 0.0;
    if (RectangleEdge.isTopOrBottom(edge)) {
        min = area.getX();
        max = area.getMaxX();
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        min = area.getMaxY();
        max = area.getY();
    }

    double result;
    if (isInverted()) {
         result = axisMax - ((java2DValue - min) / (max - min)
                  * (axisMax - axisMin));
    }
    else {
         result = axisMin + ((java2DValue - min) / (max - min)
                  * (axisMax - axisMin));
    }

    return this.timeline.toMillisecond((long) result);
}
 
Example #27
Source File: DateAxis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Translates the data value to the display coordinates (Java 2D User Space)
 * of the chart.
 *
 * @param value  the date to be plotted.
 * @param area  the rectangle (in Java2D space) where the data is to be
 *              plotted.
 * @param edge  the axis location.
 *
 * @return The coordinate corresponding to the supplied data value.
 */
public double valueToJava2D(double value, Rectangle2D area,
                            RectangleEdge edge) {

    value = this.timeline.toTimelineValue((long) value);

    DateRange range = (DateRange) getRange();
    double axisMin = this.timeline.toTimelineValue(range.getLowerMillis());
    double axisMax = this.timeline.toTimelineValue(range.getUpperMillis());
    double result = 0.0;
    if (RectangleEdge.isTopOrBottom(edge)) {
        double minX = area.getX();
        double maxX = area.getMaxX();
        if (isInverted()) {
            result = maxX + ((value - axisMin) / (axisMax - axisMin))
                     * (minX - maxX);
        }
        else {
            result = minX + ((value - axisMin) / (axisMax - axisMin))
                     * (maxX - minX);
        }
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        double minY = area.getMinY();
        double maxY = area.getMaxY();
        if (isInverted()) {
            result = minY + (((value - axisMin) / (axisMax - axisMin))
                     * (maxY - minY));
        }
        else {
            result = maxY - (((value - axisMin) / (axisMax - axisMin))
                     * (maxY - minY));
        }
    }
    return result;

}
 
Example #28
Source File: DateRangeTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Confirm that a DateRange is immutable.
 */
public void testImmutable() {
    Date d1 = new Date(10L);
    Date d2 = new Date(20L);
    DateRange r = new DateRange(d1, d2);
    d1.setTime(11L);
    assertEquals(new Date(10L), r.getLowerDate());
    r.getUpperDate().setTime(22L);
    assertEquals(new Date(20L), r.getUpperDate());
}
 
Example #29
Source File: DateAxis.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Estimates the maximum width of the tick labels, assuming the specified
 * tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we
 * just look at two values: the lower bound and the upper bound for the
 * axis.  These two values will usually be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return The estimated maximum width of the tick labels.
 */
private double estimateMaximumTickLabelHeight(Graphics2D g2,
        DateTickUnit unit) {

    RectangleInsets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.getTop() + tickLabelInsets.getBottom();

    Font tickLabelFont = getTickLabelFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = tickLabelFont.getLineMetrics("ABCxyz", frc);
    if (!isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of
        // the font)...
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        DateRange range = (DateRange) getRange();
        Date lower = range.getLowerDate();
        Date upper = range.getUpperDate();
        String lowerStr, upperStr;
        DateFormat formatter = getDateFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.dateToString(lower);
            upperStr = unit.dateToString(upper);
        }
        FontMetrics fm = g2.getFontMetrics(tickLabelFont);
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example #30
Source File: DateAxis.java    From opensim-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Translates the data value to the display coordinates (Java 2D User Space)
 * of the chart.
 *
 * @param value  the date to be plotted.
 * @param area  the rectangle (in Java2D space) where the data is to be 
 *              plotted.
 * @param edge  the axis location.
 *
 * @return The coordinate corresponding to the supplied data value.
 */
public double valueToJava2D(double value, Rectangle2D area, 
                            RectangleEdge edge) {
    
    value = this.timeline.toTimelineValue((long) value);

    DateRange range = (DateRange) getRange();
    double axisMin = this.timeline.toTimelineValue(range.getLowerDate());
    double axisMax = this.timeline.toTimelineValue(range.getUpperDate());
    double result = 0.0;
    if (RectangleEdge.isTopOrBottom(edge)) {
        double minX = area.getX();
        double maxX = area.getMaxX();
        if (isInverted()) {
            result = maxX + ((value - axisMin) / (axisMax - axisMin)) 
                     * (minX - maxX);
        }
        else {
            result = minX + ((value - axisMin) / (axisMax - axisMin)) 
                     * (maxX - minX);
        }
    }
    else if (RectangleEdge.isLeftOrRight(edge)) {
        double minY = area.getMinY();
        double maxY = area.getMaxY();
        if (isInverted()) {
            result = minY + (((value - axisMin) / (axisMax - axisMin)) 
                     * (maxY - minY));
        }
        else {
            result = maxY - (((value - axisMin) / (axisMax - axisMin)) 
                     * (maxY - minY));
        }
    }
    return result;

}