View Javadoc

1   package org.codehaus.mojo.dashboard.report.plugin.chart.time;
2   
3   /*
4    * Copyright 2007 David Vicente
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Date;
20  import java.util.List;
21  import java.util.ResourceBundle;
22  
23  import org.codehaus.mojo.dashboard.report.plugin.chart.AbstractChartStrategy;
24  import org.codehaus.mojo.dashboard.report.plugin.utils.TimePeriod;
25  import org.jfree.chart.axis.NumberAxis;
26  import org.jfree.chart.labels.StandardXYItemLabelGenerator;
27  import org.jfree.chart.labels.XYItemLabelGenerator;
28  import org.jfree.data.general.Dataset;
29  import org.jfree.data.time.Day;
30  import org.jfree.data.time.Hour;
31  import org.jfree.data.time.Minute;
32  import org.jfree.data.time.Month;
33  import org.jfree.data.time.RegularTimePeriod;
34  import org.jfree.data.time.TimeSeriesCollection;
35  import org.jfree.data.time.Week;
36  
37  public abstract class AbstractTimeChartStrategy extends AbstractChartStrategy
38  {
39  
40      /**
41       * Date axis label
42       */
43      public static String xAxisLabel = "Date";
44  
45      /**
46       * Value axis label
47       */
48      public static String yAxisLabel = "Values";
49  
50      /**
51       * dataset used to store graph datas
52       */
53      protected TimeSeriesCollection defaultdataset = new TimeSeriesCollection();
54  
55      /**
56       * Resource bundle
57       */
58      protected ResourceBundle bundle;
59  
60      /**
61       * datas
62       */
63      protected List mResults;
64  
65      /**
66       * Time thick unit
67       */
68      protected TimePeriod timePeriod;
69  
70      /**
71       * period class
72       */
73      protected Class periodClass;
74  
75      /**
76       * start date of generated graph
77       */
78      private Date startDate;
79  
80      /**
81       * end date of generated graph
82       */
83      private Date endDate;
84  
85      /**
86       * Default constructor
87       *
88       * @param bundle
89       * @param title
90       * @param results
91       * @param timeUnit
92       * @param startDate
93       * @param endDate
94       */
95      public AbstractTimeChartStrategy( ResourceBundle bundle, String title, List results, String timeUnit,
96                                        Date startDate, Date endDate )
97      {
98          this.setTitle( title );
99          this.bundle = bundle;
100         this.mResults = results;
101         this.startDate = startDate;
102         this.endDate = endDate;
103         retreivePeriodClass( timeUnit );
104 
105     }
106 
107     /**
108      *
109      * @see org.codehaus.mojo.dashboard.report.plugin.chart.IChartStrategy#getDataset()
110      */
111     public Dataset getDataset()
112     {
113         fillDataset();
114         if ( defaultdataset.getSeriesCount() > 0 )
115         {
116             this.setDatasetEmpty( false );
117         }
118         return defaultdataset;
119     }
120 
121     /**
122      * retreive XAxis label : Date
123      */
124     public String getXAxisLabel()
125     {
126         return AbstractTimeChartStrategy.xAxisLabel;
127     }
128 
129     /**
130      * retreive YAxil label : value
131      */
132     public String getYAxisLabel()
133     {
134         return AbstractTimeChartStrategy.yAxisLabel;
135     }
136 
137     /**
138      *
139      * @param timeUnit
140      */
141     private void retreivePeriodClass( String timeUnit )
142     {
143         this.timePeriod = TimePeriod.getPeriod( timeUnit );
144 
145         periodClass = null;
146         if ( timePeriod.equals( TimePeriod.MINUTE ) )
147         {
148             periodClass = Minute.class;
149         }
150         else if ( timePeriod.equals( TimePeriod.HOUR ) )
151         {
152             periodClass = Hour.class;
153         }
154         else if ( timePeriod.equals( TimePeriod.DAY ) )
155         {
156             periodClass = Day.class;
157         }
158         else if ( timePeriod.equals( TimePeriod.WEEK ) )
159         {
160             periodClass = Week.class;
161         }
162         else if ( timePeriod.equals( TimePeriod.MONTH ) )
163         {
164             periodClass = Month.class;
165         }
166         else
167         {
168             periodClass = Day.class;
169         }
170     }
171 
172     /**
173      *
174      * @param keyDate
175      * @return
176      */
177     protected RegularTimePeriod getChartDate( Date keyDate )
178     {
179         RegularTimePeriod chartDate = null;
180         if ( timePeriod.equals( TimePeriod.MINUTE ) )
181         {
182             chartDate = new Minute( keyDate );
183         }
184         else if ( timePeriod.equals( TimePeriod.HOUR ) )
185         {
186             chartDate = new Hour( keyDate );
187         }
188         else if ( timePeriod.equals( TimePeriod.DAY ) )
189         {
190             chartDate = new Day( keyDate );
191         }
192         else if ( timePeriod.equals( TimePeriod.WEEK ) )
193         {
194             chartDate = new Week( keyDate );
195         }
196         else if ( timePeriod.equals( TimePeriod.MONTH ) )
197         {
198             chartDate = new Month( keyDate );
199         }
200         else
201         {
202             chartDate = new Hour( keyDate );
203         }
204         return chartDate;
205     }
206 
207     /**
208      *
209      * @return
210      */
211     public TimePeriod getTimePeriod()
212     {
213         return timePeriod;
214     }
215 
216     /**
217      *
218      * @return
219      */
220     public NumberAxis getRangeAxis()
221     {
222         NumberAxis valueaxis = new NumberAxis();
223         valueaxis.setLowerMargin( 0.0D );
224         valueaxis.setUpperMargin( 0.25D );
225         valueaxis.setLabel( this.getYAxisLabel() );
226         return valueaxis;
227     }
228 
229     /**
230      *
231      * @return
232      */
233     public Date getEndDate()
234     {
235         return endDate;
236     }
237 
238     /**
239      *
240      * @return
241      */
242     public Date getStartDate()
243     {
244         return startDate;
245     }
246 
247     /**
248      *
249      * @return
250      */
251     public XYItemLabelGenerator getLabelGenerator()
252     {
253         return new StandardXYItemLabelGenerator();
254     }
255 }