View Javadoc

1   package org.codehaus.mojo.dashboard.report.plugin.chart.time;
2   
3   /*
4    * Copyright 2008 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.text.NumberFormat;
23  import java.util.Date;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Locale;
27  
28  import org.codehaus.mojo.dashboard.report.plugin.beans.SurefireReportBean;
29  import org.codehaus.mojo.dashboard.report.plugin.chart.AbstractChartDecorator;
30  import org.codehaus.mojo.dashboard.report.plugin.chart.IChartRenderer;
31  import org.codehaus.mojo.dashboard.report.plugin.utils.ChartUtils;
32  import org.codehaus.mojo.dashboard.report.plugin.utils.TimePeriod;
33  import org.jfree.chart.ChartColor;
34  import org.jfree.chart.axis.NumberAxis;
35  import org.jfree.chart.block.BlockBorder;
36  import org.jfree.chart.block.BlockContainer;
37  import org.jfree.chart.block.BorderArrangement;
38  import org.jfree.chart.block.EmptyBlock;
39  import org.jfree.chart.labels.ItemLabelAnchor;
40  import org.jfree.chart.labels.ItemLabelPosition;
41  import org.jfree.chart.labels.StandardXYItemLabelGenerator;
42  import org.jfree.chart.plot.XYPlot;
43  import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
44  import org.jfree.chart.title.CompositeTitle;
45  import org.jfree.chart.title.LegendTitle;
46  import org.jfree.data.time.Day;
47  import org.jfree.data.time.TimeSeries;
48  import org.jfree.data.time.TimeSeriesCollection;
49  import org.jfree.ui.RectangleEdge;
50  import org.jfree.ui.RectangleInsets;
51  import org.jfree.ui.TextAnchor;
52  
53  /**
54   * @author A315941
55   *
56   */
57  public class SurefirePercentAxisDecorator extends AbstractChartDecorator
58  {
59  
60      private static final double BLOCK_CONTAINER_WIDTH = 2000D;
61  
62      private static final int ITEM_LABEL_FONT_SIZE = 10;
63  
64      private static final double AXIS_UPPER_MARGIN = 0.099D;
65  
66      /**
67       *
68       */
69      private static final int PCENT = 100;
70  
71      /**
72       * Default constructor
73       *
74       * @param chartToDecorate
75       *            the chart to decorate with new Axis
76       * @param results
77       *            data used with new Axis
78       */
79      public SurefirePercentAxisDecorator( IChartRenderer chartToDecorate, List results )
80      {
81          super( chartToDecorate, results );
82      }
83  
84      /**
85       *
86       */
87      public void createChart()
88      {
89  
90          XYPlot xyplot = (XYPlot) report.getPlot();
91          if ( this.decoratedChart instanceof TimeChartRenderer && this.results != null && !this.results.isEmpty() )
92          {
93  
94              Iterator iter = this.results.iterator();
95              TimeSeriesCollection defaultdataset = new TimeSeriesCollection();
96              TimeSeries s1 = new TimeSeries( "% success", Day.class );
97  
98              while ( iter.hasNext() )
99              {
100                 SurefireReportBean surefire = (SurefireReportBean) iter.next();
101                 Date date = surefire.getDateGeneration();
102                 s1.addOrUpdate( new Day( TimePeriod.DAY.normalize( date ) ), surefire.getSucessRate() / PCENT );
103 
104             }
105 
106             defaultdataset.addSeries( s1 );
107 
108             XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
109             renderer.setBaseShapesVisible( true );
110             renderer.setBaseShapesFilled( true );
111             renderer.setSeriesPaint( 0, ChartColor.DARK_BLUE );
112             renderer.setShapesVisible( true );
113             renderer.setDrawOutlines( true );
114             StandardXYItemLabelGenerator labelgenerator =
115                 new StandardXYItemLabelGenerator( StandardXYItemLabelGenerator.DEFAULT_ITEM_LABEL_FORMAT,
116                                                   TimePeriod.DAY.getDateFormat(),
117                                                   NumberFormat.getPercentInstance( Locale.getDefault() ) );
118             renderer.setBaseItemLabelGenerator( labelgenerator );
119             renderer.setItemLabelFont( new Font( "SansSerif", Font.BOLD, ITEM_LABEL_FONT_SIZE ) );
120             renderer.setBaseItemLabelsVisible( true );
121             renderer.setPositiveItemLabelPosition( new ItemLabelPosition( ItemLabelAnchor.OUTSIDE10,
122                                                                           TextAnchor.BASELINE_RIGHT ) );
123 
124             renderer.setBaseStroke( new BasicStroke( 2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND ) );
125 
126             LegendTitle legendtitle = new LegendTitle( xyplot.getRenderer( 0 ) );
127             legendtitle.setMargin( new RectangleInsets( 2D, 2D, 2D, 2D ) );
128             legendtitle.setFrame( new BlockBorder() );
129             legendtitle.setBackgroundPaint( ChartColor.WHITE );
130 
131             LegendTitle legendtitle1 = new LegendTitle( renderer );
132             legendtitle1.setMargin( new RectangleInsets( 2D, 2D, 2D, 2D ) );
133             legendtitle1.setFrame( new BlockBorder() );
134             legendtitle1.setBackgroundPaint( ChartColor.WHITE );
135 
136             BlockContainer blockcontainer = new BlockContainer( new BorderArrangement() );
137             blockcontainer.add( legendtitle, RectangleEdge.LEFT );
138             blockcontainer.add( legendtitle1, RectangleEdge.RIGHT );
139             blockcontainer.add( new EmptyBlock( BLOCK_CONTAINER_WIDTH, 0.0D ) );
140 
141             CompositeTitle compositetitle = new CompositeTitle( blockcontainer );
142             compositetitle.setPosition( RectangleEdge.BOTTOM );
143 
144             report.clearSubtitles();
145             report.addSubtitle( compositetitle );
146 
147             xyplot.setDataset( 1, defaultdataset );
148 
149             NumberAxis valueaxis = new NumberAxis( "% success" );
150             valueaxis.setLowerMargin( 0.0D );
151             valueaxis.setUpperMargin( AXIS_UPPER_MARGIN );
152             valueaxis.setRangeWithMargins( 0.0D, 1.0D );
153             valueaxis.setNumberFormatOverride( NumberFormat.getPercentInstance() );
154             xyplot.setRangeAxis( 1, valueaxis );
155             xyplot.mapDatasetToRangeAxis( 1, 1 );
156             xyplot.setRenderer( 1, renderer );
157         }
158 
159     }
160 
161     /**
162      * get specific background color
163      *
164      * @return used color
165      */
166     public Color getBackgroundColor()
167     {
168         return (Color) ChartUtils.BLUE_STEEL2_LIGHT;
169     }
170 
171 }