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.awt.BasicStroke;
20  import java.awt.Color;
21  import java.awt.Font;
22  import java.awt.Paint;
23  import java.util.Date;
24  
25  import org.jfree.chart.ChartFactory;
26  import org.jfree.chart.axis.DateTickUnit;
27  import org.jfree.chart.labels.ItemLabelAnchor;
28  import org.jfree.chart.labels.ItemLabelPosition;
29  import org.jfree.chart.plot.XYPlot;
30  import org.jfree.chart.renderer.xy.XYItemRenderer;
31  import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
32  import org.jfree.data.time.DateRange;
33  import org.jfree.data.xy.XYDataset;
34  import org.jfree.ui.RectangleInsets;
35  import org.jfree.ui.TextAnchor;
36  
37  import org.codehaus.mojo.dashboard.report.plugin.chart.AbstractChartRenderer;
38  import org.codehaus.mojo.dashboard.report.plugin.chart.IChartStrategy;
39  import org.codehaus.mojo.dashboard.report.plugin.utils.ChartUtils;
40  import org.codehaus.mojo.dashboard.report.plugin.utils.DateUtils;
41  import org.codehaus.mojo.dashboard.report.plugin.utils.TimePeriod;
42  
43  public class TimeChartRenderer extends AbstractChartRenderer
44  {
45  
46      public TimeChartRenderer( IChartStrategy strategy )
47      {
48          super( strategy );
49      }
50  
51      public TimeChartRenderer( IChartStrategy strategy, int width, int height )
52      {
53          super( strategy, width, height );
54      }
55  
56      public void createChart()
57      {
58          XYDataset dataset = (XYDataset) this.datasetStrategy.getDataset();
59          report = ChartFactory.createTimeSeriesChart( this.datasetStrategy.getTitle(), // title
60                                                       this.datasetStrategy.getXAxisLabel(), // x-axis label
61                                                       this.datasetStrategy.getYAxisLabel(), // y-axis label
62                                                       dataset, // data
63                                                       true, // create legend?
64                                                       true, // generate tooltips?
65                                                       false // generate URLs?
66          );
67  
68          // report.setBackgroundPaint( Color.lightGray );
69          XYPlot plot = report.getXYPlot();
70          plot.setBackgroundPaint( Color.white );
71          plot.setDomainGridlinePaint( Color.lightGray );
72          plot.setRangeGridlinePaint( Color.lightGray );
73          plot.setAxisOffset( new RectangleInsets( 5D, 5D, 5D, 5D ) );
74          XYItemRenderer xyitemrenderer = plot.getRenderer();
75          if ( xyitemrenderer instanceof XYLineAndShapeRenderer )
76          {
77              XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) xyitemrenderer;
78              renderer.setBaseShapesVisible( true );
79              renderer.setBaseShapesFilled( true );
80  
81              renderer.setShapesVisible( true );
82              renderer.setDrawOutlines( true );
83              renderer.setBaseItemLabelGenerator( ( (AbstractTimeChartStrategy) this.datasetStrategy ).getLabelGenerator() );
84              renderer.setItemLabelFont( new Font( "SansSerif", Font.BOLD, 10 ) );
85              renderer.setBaseItemLabelsVisible( true );
86              renderer.setPositiveItemLabelPosition( new ItemLabelPosition( ItemLabelAnchor.OUTSIDE10,
87                                                                            TextAnchor.BASELINE_RIGHT ) );
88          }
89  
90          Paint[] paints = this.datasetStrategy.getPaintColor();
91          for ( int i = 0; i < dataset.getSeriesCount() && i < paints.length; i++ )
92          {
93              xyitemrenderer.setSeriesPaint( i, paints[i] );
94              xyitemrenderer.setSeriesStroke( i, new BasicStroke( 2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND ) );
95          }
96          plot.setRangeAxis( ( (AbstractTimeChartStrategy) this.datasetStrategy ).getRangeAxis() );
97          DashDateAxis axisDate = new DashDateAxis();
98          axisDate.setDateFormatOverride( ( (AbstractTimeChartStrategy) this.datasetStrategy ).getTimePeriod().getDateFormat() );
99          axisDate.setLabel( this.datasetStrategy.getXAxisLabel() );
100         axisDate.setTickUnit( getTickUnit( ( (AbstractTimeChartStrategy) this.datasetStrategy ).getTimePeriod() ) );
101         axisDate.setUpperMargin( 0.0D );
102         axisDate.setDateTickLabelAngle( -0.6 );
103 
104         if ( ( (AbstractTimeChartStrategy) this.datasetStrategy ).getStartDate() != null
105                         && ( (AbstractTimeChartStrategy) this.datasetStrategy ).getEndDate() != null )
106         {
107             axisDate.setRangeWithMargins( new DateRange(
108                                                          ( (AbstractTimeChartStrategy) this.datasetStrategy ).getStartDate(),
109                                                          ( (AbstractTimeChartStrategy) this.datasetStrategy ).getEndDate() ) );
110 
111         }
112         plot.setDomainAxis( axisDate );
113 
114         Date[] dates =
115             DateUtils.getAllDates( ( (AbstractTimeChartStrategy) this.datasetStrategy ).getStartDate(),
116                                    ( (AbstractTimeChartStrategy) this.datasetStrategy ).getEndDate(),
117                                    ( (AbstractTimeChartStrategy) this.datasetStrategy ).getTimePeriod() );
118         int width = ( dates.length * ChartUtils.STANDARD_TIME_ENTRY_WIDTH ) + ChartUtils.STANDARD_TIME_ADDITIONAL_WIDTH;
119         if ( width > ChartUtils.MINIMUM_WIDTH )
120         {
121             this.setWidth( width );
122         }
123         else
124         {
125             this.setWidth( ChartUtils.MINIMUM_WIDTH );
126         }
127     }
128 
129     private DateTickUnit getTickUnit( TimePeriod timePeriod )
130     {
131         DateTickUnit tickUnit = null;
132         if ( timePeriod.equals( TimePeriod.MINUTE ) )
133         {
134             tickUnit = new DateTickUnit( DateTickUnit.MINUTE, 10 );
135         }
136         else if ( timePeriod.equals( TimePeriod.HOUR ) )
137         {
138             tickUnit = new DateTickUnit( DateTickUnit.HOUR, 1 );
139         }
140         else if ( timePeriod.equals( TimePeriod.DAY ) )
141         {
142             tickUnit = new DateTickUnit( DateTickUnit.DAY, 1 );
143         }
144         else if ( timePeriod.equals( TimePeriod.WEEK ) )
145         {
146             tickUnit = new DateTickUnit( DateTickUnit.DAY, 7 );
147         }
148         else if ( timePeriod.equals( TimePeriod.MONTH ) )
149         {
150             tickUnit = new DateTickUnit( DateTickUnit.MONTH, 1 );
151         }
152         else
153         {
154             tickUnit = new DateTickUnit( DateTickUnit.HOUR, 1 );
155         }
156         return tickUnit;
157     }
158 
159 }