Example usage for org.jfree.chart.axis DateTickUnit getUnit

List of usage examples for org.jfree.chart.axis DateTickUnit getUnit

Introduction

In this page you can find the example usage for org.jfree.chart.axis DateTickUnit getUnit.

Prototype

public int getUnit() 

Source Link

Document

Returns the date unit.

Usage

From source file:figs.treeVisualization.gui.PhyloDateAxis.java

/**
 * Returns the previous "standard" date, for a given date and tick unit.
 *
 * @param date  the reference date./* www  .  j  a va2 s .c  o  m*/
 * @param unit  the tick unit.
 *
 * @return The previous "standard" date.
 */
@Override
protected Date previousStandardDate(Date date, DateTickUnit unit) {

    int hours;
    int days;
    int months;
    int years;

    Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
    calendar.setTime(getMinimumDate());
    int current = calendar.get(unit.getCalendarField());

    // We only care about DAY, MONTH, YEAR
    DateTickMarkPosition tickMarkPosition = this.getTickMarkPosition();
    switch (unit.getUnit()) {
    case (DateTickUnit.DAY):
        years = calendar.get(Calendar.YEAR);
        months = calendar.get(Calendar.MONTH);

        if (tickMarkPosition == DateTickMarkPosition.START) {
            hours = 0;
        } else if (tickMarkPosition == DateTickMarkPosition.MIDDLE) {
            hours = 12;
        } else {
            hours = 23;
        }
        calendar.clear(Calendar.MILLISECOND);
        calendar.set(years, months, current, hours, 0, 0);

        long result = calendar.getTime().getTime();
        if (result > date.getTime()) {
            // move it back a day
            calendar.set(years, months, current - 1, hours, 0, 0);
        }
        return calendar.getTime();
    case (DateTickUnit.MONTH):
        years = calendar.get(Calendar.YEAR);
        calendar.clear(Calendar.MILLISECOND);
        calendar.set(years, current, 1, 0, 0, 0);
        // TODO:
        /*
        Month month = new Month(calendar.getTime());
        Date standardDate = calculateDateForPosition(
            month, tickMarkPosition
        );
        long millis = standardDate.getTime();
        if (millis > date.getTime()) {
            month = (Month) month.previous();
            standardDate = calculateDateForPosition(
                month, tickMarkPosition
            );
        }
        return standardDate;
        */
        return calendar.getTime();
    case (DateTickUnit.YEAR):
        if (tickMarkPosition == DateTickMarkPosition.START) {
            months = 0;
            days = 1;
        } else if (tickMarkPosition == DateTickMarkPosition.MIDDLE) {
            months = 6;
            days = 1;
        } else {
            months = 11;
            days = 31;
        }
        calendar.clear(Calendar.MILLISECOND);
        calendar.set(current, months, days, 0, 0, 0);
        return calendar.getTime();
    default:
        return calendar.getTime();
    }

}

From source file:ucar.unidata.idv.control.chart.TimeSeriesChart.java

/**
 * Make the plot/*from   w w  w .ja va 2s.c  o m*/
 *
 * @return The plot_
 */
public Plot doMakePlot() {

    IdvPreferenceManager pref = control.getControlContext().getIdv().getPreferenceManager();
    TimeZone timeZone = pref.getDefaultTimeZone();
    NumberAxis valueAxis = new FixedWidthNumberAxis("");
    final SimpleDateFormat sdf = new SimpleDateFormat(
            ((dateFormat != null) ? dateFormat : pref.getDefaultDateFormat()));
    sdf.setTimeZone(timeZone);
    DateAxis timeAxis = new DateAxis("Time (" + timeZone.getID() + ")", timeZone) {

        protected List xxxxxrefreshTicksHorizontal(Graphics2D g2, Rectangle2D dataArea, RectangleEdge edge) {

            List ticks = super.refreshTicksHorizontal(g2, dataArea, edge);

            List<Tick> result = new java.util.ArrayList<Tick>();

            Font tickLabelFont = getTickLabelFont();
            g2.setFont(tickLabelFont);

            if (isAutoTickUnitSelection()) {
                selectAutoTickUnit(g2, dataArea, edge);
            }

            DateTickUnit unit = getTickUnit();
            Date tickDate = calculateLowestVisibleTickValue(unit);
            Date upperDate = getMaximumDate();

            Date firstDate = null;
            while (tickDate.before(upperDate)) {

                if (!isHiddenValue(tickDate.getTime())) {
                    // work out the value, label and position
                    String tickLabel;
                    DateFormat formatter = getDateFormatOverride();
                    if (firstDate == null) {
                        if (formatter != null) {
                            tickLabel = formatter.format(tickDate);
                        } else {
                            tickLabel = getTickUnit().dateToString(tickDate);
                        }
                        firstDate = tickDate;
                    } else {
                        double msdiff = tickDate.getTime() - firstDate.getTime();
                        int hours = (int) (msdiff / 1000 / 60 / 60);
                        tickLabel = hours + "H";
                    }
                    //                tickLabel = tickLabel;
                    TextAnchor anchor = null;
                    TextAnchor rotationAnchor = null;
                    double angle = 0.0;
                    if (isVerticalTickLabels()) {
                        anchor = TextAnchor.CENTER_RIGHT;
                        rotationAnchor = TextAnchor.CENTER_RIGHT;
                        if (edge == RectangleEdge.TOP) {
                            angle = Math.PI / 2.0;
                        } else {
                            angle = -Math.PI / 2.0;
                        }
                    } else {
                        if (edge == RectangleEdge.TOP) {
                            anchor = TextAnchor.BOTTOM_CENTER;
                            rotationAnchor = TextAnchor.BOTTOM_CENTER;
                        } else {
                            anchor = TextAnchor.TOP_CENTER;
                            rotationAnchor = TextAnchor.TOP_CENTER;
                        }
                    }

                    Tick tick = new DateTick(tickDate, tickLabel, anchor, rotationAnchor, angle);
                    result.add(tick);
                    tickDate = unit.addToDate(tickDate, getTimeZone());
                } else {
                    tickDate = unit.rollDate(tickDate, getTimeZone());

                    continue;
                }

                // could add a flag to make the following correction optional...
                switch (unit.getUnit()) {

                case (DateTickUnit.MILLISECOND):
                case (DateTickUnit.SECOND):
                case (DateTickUnit.MINUTE):
                case (DateTickUnit.HOUR):
                case (DateTickUnit.DAY):
                    break;

                case (DateTickUnit.MONTH):
                    tickDate = calculateDateForPositionX(new Month(tickDate, getTimeZone()),
                            getTickMarkPosition());

                    break;

                case (DateTickUnit.YEAR):
                    tickDate = calculateDateForPositionX(new Year(tickDate, getTimeZone()),
                            getTickMarkPosition());

                    break;

                default:
                    break;

                }

            }

            return result;

        }

        private Date calculateDateForPositionX(RegularTimePeriod period, DateTickMarkPosition position) {

            if (position == null) {
                throw new IllegalArgumentException("Null 'position' argument.");
            }
            Date result = null;
            if (position == DateTickMarkPosition.START) {
                result = new Date(period.getFirstMillisecond());
            } else if (position == DateTickMarkPosition.MIDDLE) {
                result = new Date(period.getMiddleMillisecond());
            } else if (position == DateTickMarkPosition.END) {
                result = new Date(period.getLastMillisecond());
            }

            return result;

        }

    };
    timeAxis.setDateFormatOverride(sdf);

    final XYPlot[] xyPlotHolder = { null };

    xyPlotHolder[0] = new MyXYPlot(new TimeSeriesCollection(), timeAxis, valueAxis, null) {
        public void drawBackground(Graphics2D g2, Rectangle2D area) {
            super.drawBackground(g2, area);
            drawSunriseSunset(g2, xyPlotHolder[0], area);
        }
    };

    if (animationTimeAnnotation != null) {
        xyPlotHolder[0].addAnnotation(animationTimeAnnotation);
    }

    return xyPlotHolder[0];

}