KickJava   Java API By Example, From Geeks To Geeks.

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


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  * (C) Copyright 2003, by Sasa Markovic.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22 package org.jrobin.graph;
23
24 import org.jrobin.core.XmlWriter;
25
26 import java.util.*;
27 import java.text.SimpleDateFormat JavaDoc;
28
29 /**
30  * <p>Class used to determine the chart grid shown on the X (time) axis.</p>
31  *
32  * @author Arne Vandamme (cobralord@jrobin.org)
33  */

34 public class TimeAxisUnit
35 {
36     // ================================================================
37
// -- Members
38
// ================================================================
39
private static final int[] calendarUnit =
40                 {
41                     Calendar.SECOND,
42                     Calendar.MINUTE,
43                     Calendar.HOUR_OF_DAY,
44                     Calendar.DAY_OF_MONTH,
45                     Calendar.WEEK_OF_YEAR,
46                     Calendar.MONTH,
47                     Calendar.YEAR
48                 };
49
50     // Indices in the calendarUnit table
51
public static final int SECOND = 0; /** constant for seconds */
52     public static final int MINUTE = 1; /** constant for minutes */
53     public static final int HOUR = 2; /** constant for hours */
54     public static final int DAY = 3; /** constant for days */
55     public static final int WEEK = 4; /** constant for weeks */
56     public static final int MONTH = 5; /** constant for months */
57     public static final int YEAR = 6; /** constant for years */
58     
59     // Days of the week
60
public static final int MONDAY = Calendar.MONDAY;
61     public static final int TUESDAY = Calendar.TUESDAY;
62     public static final int WEDNESDAY = Calendar.WEDNESDAY;
63     public static final int THURSDAY = Calendar.THURSDAY;
64     public static final int FRIDAY = Calendar.FRIDAY;
65     public static final int SATURDAY = Calendar.SATURDAY;
66     public static final int SUNDAY = Calendar.SUNDAY;
67
68     private static final String JavaDoc[] UNIT_NAMES = {
69         "SECOND", "MINUTE", "HOUR", "DAY", "WEEK", "MONTH", "YEAR"
70     };
71     
72     private static final String JavaDoc[] DAY_NAMES = {
73         "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"
74     };
75
76     private int minGridTimeUnit = HOUR; // minor grid
77
private int minGridUnitSteps = 1;
78     private int majGridTimeUnit = HOUR; // major grid
79
private int majGridUnitSteps = 6;
80     
81     private boolean centerLabels = false;
82     private SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("HH:mm", Locale.ENGLISH );
83     
84     private int firstDayOfWeek = MONDAY; // first day of a week
85

86     
87     // ================================================================
88
// -- Constructors
89
// ================================================================
90
/**
91      * Creates a TimeAxisUnit object to use as X axis grid specification.
92      * There are both minor and major grid lines, the major lines are accompanied by a time label.
93      *
94      * To define a grid line you must define a specific time unit, and a number of time steps.
95      * A grid line will appear everey steps*unit. Possible units are defined in the
96      * {@link org.jrobin.graph.TimeAxisUnit TimeAxisUnit} class, and are <i>SECOND, MINUTE, HOUR, DAY,
97      * WEEK, MONTH</i> and <i>YEAR</i>.
98      *
99      * @param minGridTimeUnit Time unit for the minor grid lines.
100      * @param minGridUnitSteps Time unit steps for the minor grid lines.
101      * @param majGridTimeUnit Time unit for the major grid lines.
102      * @param majGridUnitSteps Time unit steps for the major grid lines.
103      * @param dateFormat Format to use to convert the specific time into a label string.
104      * @param centerLabels True if labels (major grid) should be centered between two major grid lines.
105      * @param firstDayOfWeek First day of a calendar week.
106      */

107     TimeAxisUnit( int minGridTimeUnit, int minGridUnitSteps,
108                   int majGridTimeUnit, int majGridUnitSteps,
109                   SimpleDateFormat JavaDoc dateFormat, boolean centerLabels, int firstDayOfWeek )
110     {
111         this.minGridTimeUnit = minGridTimeUnit;
112         this.minGridUnitSteps = minGridUnitSteps;
113         this.majGridTimeUnit = majGridTimeUnit;
114         this.majGridUnitSteps = majGridUnitSteps;
115         this.dateFormat = new SimpleDateFormat JavaDoc( dateFormat.toPattern(), Locale.ENGLISH );
116         this.centerLabels = centerLabels;
117         this.firstDayOfWeek = firstDayOfWeek;
118     }
119     
120     
121     // ================================================================
122
// -- Protected methods
123
// ================================================================
124
/**
125      * Returns a set of markers making up the grid for the X axis.
126      * All markers are situated in a given timespan.
127      * @param start Start time in seconds of the timespan.
128      * @param stop End time in seconds of the timespan.
129      * @return List of markers as a TimeMarker array.
130      */

131     TimeMarker[] getTimeMarkers( long start, long stop )
132     {
133         start *= 1000; // Discard milliseconds
134
stop *= 1000;
135     
136         Calendar cMaj = Calendar.getInstance();
137         Calendar cMin = Calendar.getInstance();
138     
139         // Set the start calculation point for the grids
140
setStartPoint(cMaj, majGridTimeUnit, start);
141         setStartPoint(cMin, minGridTimeUnit, start);
142     
143         // Find first visible grid point
144
long minPoint = cMin.getTimeInMillis();
145         long majPoint = cMaj.getTimeInMillis();
146     
147         while ( majPoint < start )
148             majPoint = getNextPoint(cMaj, majGridTimeUnit, majGridUnitSteps);
149         while ( minPoint < start )
150             minPoint = getNextPoint(cMin, minGridTimeUnit, minGridUnitSteps);
151     
152         ArrayList markerList = new ArrayList();
153             
154         // Marker list does not care in what order the markers are returned, we could
155
// get minor and major grid sequentially, but, if we did that, we might draw the marker
156
// more than once if the major and minor overlap, which is most likely slower than
157
// this way of calculating the markers.
158
//
159
// In short: the first while() loop is not *necessary* to get correct results
160
while ( minPoint <= stop && majPoint <= stop )
161         {
162             if ( minPoint < majPoint )
163             {
164                 markerList.add( new TimeMarker( minPoint, "", false ) );
165                 minPoint = getNextPoint( cMin, minGridTimeUnit, minGridUnitSteps );
166             }
167             else if ( minPoint == majPoint ) // Special case, but will happen most of the time
168
{
169                 markerList.add( new TimeMarker( majPoint, dateFormat.format(cMaj.getTime()), true ) );
170                 majPoint = getNextPoint( cMaj, majGridTimeUnit, majGridUnitSteps );
171                 minPoint = getNextPoint( cMin, minGridTimeUnit, minGridUnitSteps );
172             }
173             else
174             {
175                 markerList.add( new TimeMarker( majPoint, dateFormat.format(cMaj.getTime()), true ) );
176                 majPoint = getNextPoint( cMaj, majGridTimeUnit, majGridUnitSteps );
177             }
178         }
179
180         while ( minPoint <= stop )
181         {
182             markerList.add( new TimeMarker( minPoint, "", false ) );
183             minPoint = getNextPoint( cMin, minGridTimeUnit, minGridUnitSteps );
184         }
185     
186         while ( majPoint <= stop )
187         {
188             markerList.add( new TimeMarker( majPoint, dateFormat.format(cMaj.getTime()), true ) );
189             majPoint = getNextPoint( cMaj, majGridTimeUnit, majGridUnitSteps );
190         }
191     
192         return (TimeMarker[]) markerList.toArray( new TimeMarker[0] );
193     }
194
195
196     /**
197      * Calculates and returns the number of pixels between two major grid lines.
198      * @return Number of pixels between two major grid lines.
199      */

200     long getMajorGridWidth()
201     {
202         Calendar c = Calendar.getInstance();
203         long now = c.getTimeInMillis() / 1000;
204     
205         c.add( calendarUnit[majGridTimeUnit], majGridUnitSteps );
206     
207         return (c.getTimeInMillis() / 1000) - now;
208     }
209
210     boolean getCenterLabels() {
211         return centerLabels;
212     }
213
214     int getMinGridTimeUnit() {
215         return minGridTimeUnit;
216     }
217
218     int getMinGridUnitSteps() {
219         return minGridUnitSteps;
220     }
221
222     int getMajGridTimeUnit() {
223         return majGridTimeUnit;
224     }
225
226     int getMajGridUnitSteps() {
227         return majGridUnitSteps;
228     }
229
230     boolean isCenterLabels() {
231         return centerLabels;
232     }
233
234     public SimpleDateFormat JavaDoc getDateFormat() {
235         return dateFormat;
236     }
237
238
239     // ================================================================
240
// -- Private methods
241
// ================================================================
242
/**
243      * Rounds a given exact timestamp to a more rounded timestamp used
244      * for starting grid line calculation.
245      * @param t Calendar to use for calculation.
246      * @param unit Time unit on which to round the value.
247      * @param exactStart Exact timestamp of the time.
248      */

249     private void setStartPoint( Calendar t, int unit, long exactStart )
250     {
251         t.setTimeInMillis( exactStart );
252         t.setFirstDayOfWeek( firstDayOfWeek );
253         
254         for (int i = 0; i < HOUR && i <= unit; i++)
255             t.set( calendarUnit[i], 0 );
256         
257         if ( unit >= HOUR )
258             t.set( Calendar.HOUR_OF_DAY, 0 );
259         
260         if ( unit == WEEK )
261             t.set( Calendar.DAY_OF_WEEK, t.getFirstDayOfWeek() );
262         else if ( unit == MONTH )
263             t.set( Calendar.DAY_OF_MONTH, 1 );
264         else if ( unit == YEAR )
265         {
266             t.set( Calendar.DATE, 1 );
267             t.set( Calendar.MONTH, 0 );
268         }
269     }
270     
271     /**
272      * Retrieves the next grid line point, based on a time unit and time unit step.
273      * @param t Calendar to use for calculation.
274      * @param unit Time unit to add to the given (previous) time.
275      * @param unitSteps Number of times to add the unit to the given (previous) time.
276      * @return Timestamp of the next grid line point.
277      */

278     private long getNextPoint( Calendar t, int unit, int unitSteps )
279     {
280         t.add( calendarUnit[unit], unitSteps );
281         
282         return t.getTimeInMillis();
283     }
284
285     static String JavaDoc getUnitName(int unit) {
286         return UNIT_NAMES[unit];
287     }
288     
289     static String JavaDoc getDayName( int dayIndex ) {
290         return DAY_NAMES[dayIndex];
291     }
292
293     void exportXmlTemplate(XmlWriter xml) {
294         xml.startTag("time_axis");
295         xml.writeTag("min_grid_time_unit", TimeAxisUnit.getUnitName(getMinGridTimeUnit()));
296         xml.writeTag("min_grid_unit_steps", getMinGridUnitSteps());
297         xml.writeTag("maj_grid_time_unit", TimeAxisUnit.getUnitName(getMajGridTimeUnit()));
298         xml.writeTag("maj_grid_unit_steps", getMajGridUnitSteps());
299         xml.writeTag("date_format", getDateFormat().toPattern());
300         xml.writeTag("center_labels", getCenterLabels());
301         
302         if ( firstDayOfWeek != MONDAY )
303             xml.writeTag( "first_day_of_week", getDayName(firstDayOfWeek) );
304         
305         xml.closeTag(); // time_axis
306
}
307
308 }
309
Popular Tags