segmentedtimeline.java

来自「JfreeChart 常用图表例子」· Java 代码 · 共 1,722 行 · 第 1/5 页

JAVA
1,722
字号
    /**     * Factory method to create a 15-min, 9:00 AM thought 4:00 PM, Monday      * through Friday SegmentedTimeline.     * <P>     * This timeline uses a segmentSize of FIFTEEN_MIN_SEGMENT_SIZE. The      * segment group is defined as 28 included segments (9:00 AM through      * 4:00 PM) and 68 excluded segments (4:00 PM through 9:00 AM the next day).     * <P>     * In order to exclude Saturdays and Sundays it uses a baseTimeline that      * only includes Monday through Friday days.     * <P>     * The <code>startTime</code> of the resulting timeline will be 9:00 AM      * after the startTime of the baseTimeline. This will correspond to 9:00 AM     * of the first Monday after 1/1/1900.     *     * @return A fully initialized SegmentedTimeline.     */    public static SegmentedTimeline newFifteenMinuteTimeline() {        SegmentedTimeline timeline             = new SegmentedTimeline(FIFTEEN_MINUTE_SEGMENT_SIZE, 28, 68);        timeline.setStartTime(            FIRST_MONDAY_AFTER_1900 + 36 * timeline.getSegmentSize()        );        timeline.setBaseTimeline(newMondayThroughFridayTimeline());        return timeline;    }        /**     * Returns the flag that controls whether or not the daylight saving      * adjustment is applied.     *      * @return A boolean.     */    public boolean getAdjustForDaylightSaving() {        return this.adjustForDaylightSaving;       }        /**     * Sets the flag that controls whether or not the daylight saving adjustment     * is applied.     *      * @param adjust  the flag.     */    public void setAdjustForDaylightSaving(boolean adjust) {        this.adjustForDaylightSaving = adjust;       }    ////////////////////////////////////////////////////////////////////////////    // operations    ////////////////////////////////////////////////////////////////////////////    /**     * Sets the start time for the timeline. This is the beginning of segment      * zero.     *     * @param millisecond  the start time (encoded as in java.util.Date).     */    public void setStartTime(long millisecond) {        this.startTime = millisecond;    }    /**     * Returns the start time for the timeline. This is the beginning of      * segment zero.     *      * @return The start time.     */    public long getStartTime() {        return this.startTime;    }    /**     * Returns the number of segments excluded per segment group.     *      * @return The number of segments excluded.     */    public int getSegmentsExcluded() {        return this.segmentsExcluded;    }    /**     * Returns the size in milliseconds of the segments excluded per segment      * group.     *      * @return The size in milliseconds.     */    public long getSegmentsExcludedSize() {        return this.segmentsExcludedSize;    }    /**     * Returns the number of segments in a segment group. This will be equal to     * segments included plus segments excluded.     *      * @return The number of segments.     */    public int getGroupSegmentCount() {        return this.groupSegmentCount;    }    /**     * Returns the size in milliseconds of a segment group. This will be equal      * to size of the segments included plus the size of the segments excluded.     *      * @return The segment group size in milliseconds.     */    public long getSegmentsGroupSize() {        return this.segmentsGroupSize;    }    /**     * Returns the number of segments included per segment group.     *      * @return The number of segments.     */    public int getSegmentsIncluded() {        return this.segmentsIncluded;    }    /**     * Returns the size in ms of the segments included per segment group.     *      * @return The segment size in milliseconds.     */    public long getSegmentsIncludedSize() {        return this.segmentsIncludedSize;    }    /**     * Returns the size of one segment in ms.     *      * @return The segment size in milliseconds.     */    public long getSegmentSize() {        return this.segmentSize;    }    /**     * Returns a list of all the exception segments. This list is not      * modifiable.     *      * @return The exception segments.     */    public List getExceptionSegments() {        return Collections.unmodifiableList(this.exceptionSegments);    }    /**     * Sets the exception segments list.     *      * @param exceptionSegments  the exception segments.     */    public void setExceptionSegments(List exceptionSegments) {        this.exceptionSegments = exceptionSegments;    }    /**     * Returns our baseTimeline, or <code>null</code> if none.     *      * @return The base timeline.     */    public SegmentedTimeline getBaseTimeline() {        return this.baseTimeline;    }    /**     * Sets the base timeline.     *      * @param baseTimeline  the timeline.     */    public void setBaseTimeline(SegmentedTimeline baseTimeline) {        // verify that baseTimeline is compatible with us        if (baseTimeline != null) {            if (baseTimeline.getSegmentSize() < this.segmentSize) {                throw new IllegalArgumentException(                    "baseTimeline.getSegmentSize() is smaller than segmentSize"                );            }             else if (baseTimeline.getStartTime() > this.startTime) {                throw new IllegalArgumentException(                    "baseTimeline.getStartTime() is after startTime"                );            }             else if ((baseTimeline.getSegmentSize() % this.segmentSize) != 0) {                throw new IllegalArgumentException(                    "baseTimeline.getSegmentSize() is not multiple of "                    + "segmentSize"                );            }             else if (((this.startTime                     - baseTimeline.getStartTime()) % this.segmentSize) != 0) {                throw new IllegalArgumentException(                    "baseTimeline is not aligned"                );            }        }        this.baseTimeline = baseTimeline;    }    /**     * Translates a value relative to the domain value (all Dates) into a value     * relative to the segmented timeline. The values relative to the segmented     * timeline are all consecutives starting at zero at the startTime.     *     * @param millisecond  the millisecond (as encoded by java.util.Date).     *      * @return The timeline value.     */    public long toTimelineValue(long millisecond) {          long result;        long rawMilliseconds = millisecond - this.startTime;        long groupMilliseconds = rawMilliseconds % this.segmentsGroupSize;        long groupIndex = rawMilliseconds / this.segmentsGroupSize;                if (groupMilliseconds >= this.segmentsIncludedSize) {            result = toTimelineValue(                this.startTime + this.segmentsGroupSize * (groupIndex + 1)            );        }         else {                   Segment segment = getSegment(millisecond);            if (segment.inExceptionSegments()) {                result = toTimelineValue(segment.getSegmentEnd() + 1);            }                  else {                long shiftedSegmentedValue = millisecond - this.startTime;                long x = shiftedSegmentedValue % this.segmentsGroupSize;                long y = shiftedSegmentedValue / this.segmentsGroupSize;                long wholeExceptionsBeforeDomainValue =                    getExceptionSegmentCount(this.startTime, millisecond - 1);//                long partialTimeInException = 0;//                Segment ss = getSegment(millisecond);//                if (ss.inExceptionSegments()) {//                    partialTimeInException = millisecond                 //     - ss.getSegmentStart();//                }                if (x < this.segmentsIncludedSize) {                    result = this.segmentsIncludedSize * y                              + x - wholeExceptionsBeforeDomainValue                              * this.segmentSize;                             // - partialTimeInException;;                 }                else {                    result = this.segmentsIncludedSize * (y + 1)                              - wholeExceptionsBeforeDomainValue                              * this.segmentSize;                             // - partialTimeInException;                }            }        }        return result;    }    /**     * Translates a date into a value relative to the segmented timeline. The      * values relative to the segmented timeline are all consecutives starting      * at zero at the startTime.     *     * @param date  date relative to the domain.     *      * @return The timeline value (in milliseconds).     */    public long toTimelineValue(Date date) {        return toTimelineValue(getTime(date));        //return toTimelineValue(dateDomainValue.getTime());    }    /**     * Translates a value relative to the timeline into a millisecond.     *     * @param timelineValue  the timeline value (in milliseconds).     *      * @return The domain value (in milliseconds).     */    public long toMillisecond(long timelineValue) {                // calculate the result as if no exceptions        Segment result = new Segment(this.startTime + timelineValue             + (timelineValue / this.segmentsIncludedSize)             * this.segmentsExcludedSize);                long lastIndex = this.startTime;        // adjust result for any exceptions in the result calculated        while (lastIndex <= result.segmentStart) {            // skip all whole exception segments in the range            long exceptionSegmentCount;            while ((exceptionSegmentCount = getExceptionSegmentCount(                 lastIndex, (result.millisecond / this.segmentSize)                  * this.segmentSize - 1)) > 0            ) {                 lastIndex = result.segmentStart;                // move forward exceptionSegmentCount segments skipping                 // excluded segments                for (int i = 0; i < exceptionSegmentCount; i++) {                    do {                        result.inc();                    }                    while (result.inExcludeSegments());                }            }            lastIndex = result.segmentStart;            // skip exception or excluded segments we may fall on            while (result.inExceptionSegments() || result.inExcludeSegments()) {                result.inc();                lastIndex += this.segmentSize;            }            lastIndex++;        }        return getTimeFromLong(result.millisecond);     }    /**     * Converts a date/time value to take account of daylight savings time.     *      * @param date  the milliseconds.     *      * @return The milliseconds.     */    public long getTimeFromLong(long date) {        long result = date;        if (this.adjustForDaylightSaving) {            this.workingCalendarNoDST.setTime(new Date(date));            this.workingCalendar.set(                this.workingCalendarNoDST.get(Calendar.YEAR),                this.workingCalendarNoDST.get(Calendar.MONTH),                this.workingCalendarNoDST.get(Calendar.DATE),                this.workingCalendarNoDST.get(Calendar.HOUR_OF_DAY),                this.workingCalendarNoDST.get(Calendar.MINUTE),                this.workingCalendarNoDST.get(Calendar.SECOND)            );            this.workingCalendar.set(                Calendar.MILLISECOND,                 this.workingCalendarNoDST.get(Calendar.MILLISECOND)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?