KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > graph > TimeGrid


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org)
7  *
8  * Developers: Sasa Markovic (saxon@jrobin.org)
9  * Arne Vandamme (cobralord@jrobin.org)
10  *
11  * (C) Copyright 2003, by Sasa Markovic.
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25 package org.jrobin.graph;
26
27 import java.text.SimpleDateFormat JavaDoc;
28
29 /**
30  * <p>Holds specific information about the Time axis grid of the chart.</p>
31  *
32  * @author Arne Vandamme (cobralord@jrobin.org)
33  */

34 class TimeGrid
35 {
36     // ================================================================
37
// -- Members
38
// ================================================================
39
private long startTime;
40     private long endTime;
41     
42     private TimeAxisUnit tAxis;
43     
44     
45     // ================================================================
46
// -- Constructors
47
// ================================================================
48
/**
49      * Creates a time grid based on a timespan and possibly a time axis
50      * unit specification.
51      * @param startTime Start time of the timespan.
52      * @param endTime End time of the timespan.
53      * @param tAxis TimeAxisUnit specified to determine the grid lines, if the given
54      * TimeAxisUnit is null, one will be automatically determined.
55      * @param firstDayOfWeek First day of a calendar week.
56      */

57     TimeGrid( long startTime, long endTime, TimeAxisUnit tAxis, int firstDayOfWeek )
58     {
59         this.startTime = startTime;
60         this.endTime = endTime;
61         this.tAxis = tAxis;
62         
63         // Set an appropriate time axis it not given yet
64
setTimeAxis( firstDayOfWeek );
65     }
66     
67     
68     // ================================================================
69
// -- Protected methods
70
// ================================================================
71
long getStartTime() {
72         return startTime;
73     }
74
75     long getEndTime() {
76         return endTime;
77     }
78
79     TimeMarker[] getTimeMarkers() {
80         return tAxis.getTimeMarkers( startTime, endTime );
81     }
82
83     long getMajorGridWidth() {
84         return tAxis.getMajorGridWidth();
85     }
86
87     boolean centerLabels() {
88         return tAxis.getCenterLabels();
89     }
90     
91     
92     // ================================================================
93
// -- Private methods
94
// ================================================================
95
/**
96      * Determines a good TimeAxisUnit to use for grid calculation.
97      * A decent grid is selected based on the timespan being used in the chart.
98      *
99      * @param firstDayOfWeek First day of a calendar week.
100      */

101     private void setTimeAxis( int firstDayOfWeek )
102     {
103         if ( tAxis != null )
104             return;
105         
106         double days = (endTime - startTime) / 86400.0;
107
108         if ( days <= 0.75 / 24.0 ) {
109             tAxis = new TimeAxisUnit( TimeAxisUnit.MINUTE, 1, TimeAxisUnit.MINUTE, 5, new SimpleDateFormat JavaDoc("HH:mm"), false, firstDayOfWeek );
110         }
111         else if ( days <= 2.0 / 24.0 ) {
112             tAxis = new TimeAxisUnit( TimeAxisUnit.MINUTE, 5, TimeAxisUnit.MINUTE, 10, new SimpleDateFormat JavaDoc("HH:mm"), false, firstDayOfWeek );
113         }
114         else if ( days <= 3.0 / 24.0 ) {
115             tAxis = new TimeAxisUnit( TimeAxisUnit.MINUTE, 5, TimeAxisUnit.MINUTE, 20, new SimpleDateFormat JavaDoc("HH:mm"), false, firstDayOfWeek );
116         }
117         else if ( days <= 5.0 / 24.0 ) {
118             tAxis = new TimeAxisUnit( TimeAxisUnit.MINUTE, 10, TimeAxisUnit.MINUTE, 30, new SimpleDateFormat JavaDoc("HH:mm"), false, firstDayOfWeek );
119         }
120         else if ( days <= 10.0 / 24.0 ) {
121             tAxis = new TimeAxisUnit( TimeAxisUnit.MINUTE, 15, TimeAxisUnit.HOUR, 1, new SimpleDateFormat JavaDoc("HH:mm"), false, firstDayOfWeek );
122         }
123         else if ( days <= 15.0 / 24.0 ) {
124             tAxis = new TimeAxisUnit( TimeAxisUnit.MINUTE, 30, TimeAxisUnit.HOUR, 2, new SimpleDateFormat JavaDoc("HH:mm"), false, firstDayOfWeek );
125         }
126         else if ( days <= 20.0 / 24.0 ) {
127             tAxis = new TimeAxisUnit( TimeAxisUnit.HOUR, 1, TimeAxisUnit.HOUR, 1, new SimpleDateFormat JavaDoc("HH"), true, firstDayOfWeek );
128         }
129         else if ( days <= 36.0 / 24.0 ) {
130             tAxis = new TimeAxisUnit( TimeAxisUnit.HOUR, 1, TimeAxisUnit.HOUR, 4, new SimpleDateFormat JavaDoc("HH:mm"), false, firstDayOfWeek );
131         }
132         else if ( days <= 2 ) {
133             tAxis = new TimeAxisUnit( TimeAxisUnit.HOUR, 2, TimeAxisUnit.HOUR, 6, new SimpleDateFormat JavaDoc("HH:mm"), false, firstDayOfWeek );
134         }
135         else if ( days <= 3 ) {
136             tAxis = new TimeAxisUnit( TimeAxisUnit.HOUR, 3, TimeAxisUnit.HOUR, 12, new SimpleDateFormat JavaDoc("HH:mm"), false, firstDayOfWeek );
137         }
138         else if ( days < 8 ) {
139             tAxis = new TimeAxisUnit( TimeAxisUnit.HOUR, 6, TimeAxisUnit.DAY, 1, new SimpleDateFormat JavaDoc("EEE dd"), true, firstDayOfWeek );
140         }
141         else if ( days <= 14 ) {
142             tAxis = new TimeAxisUnit( TimeAxisUnit.HOUR, 12, TimeAxisUnit.DAY, 1, new SimpleDateFormat JavaDoc("dd"), true, firstDayOfWeek );
143         }
144         else if ( days <= 43 ) {
145             tAxis = new TimeAxisUnit( TimeAxisUnit.DAY, 1, TimeAxisUnit.WEEK, 1, new SimpleDateFormat JavaDoc("'week' ww"), true, firstDayOfWeek );
146         }
147         else if ( days <= 157 ) {
148             tAxis = new TimeAxisUnit( TimeAxisUnit.WEEK, 1, TimeAxisUnit.WEEK, 1, new SimpleDateFormat JavaDoc("ww"), true, firstDayOfWeek );
149         }
150         else {
151             tAxis = new TimeAxisUnit( TimeAxisUnit.MONTH, 1, TimeAxisUnit.MONTH, 1, new SimpleDateFormat JavaDoc("MMM"), true, firstDayOfWeek );
152         }
153     }
154 }
155
Popular Tags