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.awt.geom.Line2D;
23  import java.util.Calendar;
24  import java.util.Date;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import org.jfree.chart.ChartColor;
29  import org.jfree.chart.LegendItem;
30  import org.jfree.chart.block.Block;
31  import org.jfree.chart.block.BlockContainer;
32  import org.jfree.chart.block.BorderArrangement;
33  import org.jfree.chart.block.CenterArrangement;
34  import org.jfree.chart.block.ColumnArrangement;
35  import org.jfree.chart.block.EmptyBlock;
36  import org.jfree.chart.block.FlowArrangement;
37  import org.jfree.chart.block.LabelBlock;
38  import org.jfree.chart.block.LineBorder;
39  import org.jfree.chart.plot.ValueMarker;
40  import org.jfree.chart.plot.XYPlot;
41  import org.jfree.chart.title.CompositeTitle;
42  import org.jfree.chart.title.LegendGraphic;
43  import org.jfree.chart.title.LegendItemBlockContainer;
44  import org.jfree.chart.title.LegendTitle;
45  import org.jfree.ui.HorizontalAlignment;
46  import org.jfree.ui.RectangleAnchor;
47  import org.jfree.ui.RectangleEdge;
48  import org.jfree.ui.RectangleInsets;
49  import org.jfree.ui.VerticalAlignment;
50  
51  import org.codehaus.mojo.dashboard.report.plugin.chart.AbstractChartDecorator;
52  import org.codehaus.mojo.dashboard.report.plugin.chart.IChartRenderer;
53  import org.codehaus.mojo.dashboard.report.plugin.utils.ChartUtils;
54  
55  /**
56   * 
57   * @author <a href="dvicente72@gmail.com">David Vicente</a>
58   * 
59   */
60  public class MarkerTimeChartDecorator extends AbstractChartDecorator
61  {
62  
63      public MarkerTimeChartDecorator( IChartRenderer chartToDecorate, List markersToPlot )
64      {
65          super( chartToDecorate, markersToPlot );
66      }
67  
68      public void createChart()
69      {
70  
71          XYPlot xyplot = (XYPlot) report.getPlot();
72          // if (this.decoratedChart instanceof TimeChartRenderer ) {
73  
74          if ( this.results != null && !this.results.isEmpty() )
75          {
76              Iterator iter1 = this.results.iterator();
77              ValueMarker valuemarker = null;
78              BlockContainer blockcontainerLabel = new BlockContainer( new ColumnArrangement() );
79              // blockcontainerLabel.setFrame( new LineBorder() );
80              int i = 0;
81              while ( iter1.hasNext() )
82              {
83                  Object[] item = (Object[]) iter1.next();
84                  Date date = (Date) item[1];
85                  Calendar cal = Calendar.getInstance();
86                  cal.setTime( date );
87                  cal.set( Calendar.HOUR_OF_DAY, 0 );
88                  cal.set( Calendar.MINUTE, 0 );
89                  cal.set( Calendar.SECOND, 0 );
90                  valuemarker =
91                      new ValueMarker( cal.getTimeInMillis(), ChartColor.createDefaultPaintArray()[i],
92                                       new BasicStroke( 2.0F ) );
93  
94                  xyplot.addDomainMarker( valuemarker );
95                  LegendItem legendLabel =
96                      new LegendItem( (String) item[0], null, null, null, new Line2D.Double( -7.0, 0.0, 7.0, 0.0 ),
97                                      valuemarker.getPaint(), valuemarker.getStroke(), valuemarker.getPaint() );
98  
99                  blockcontainerLabel.add( createLegendItemBlock( legendLabel, i ) );
100                 i++;
101             }
102             createLegendBlock( blockcontainerLabel );
103         }
104         // }
105     }
106 
107     public Color getBackgroundColor()
108     {
109         return (Color) ChartUtils.BLUE_STEEL2_LIGHT;
110     }
111 
112     /**
113      * Creates a legend item block.
114      * 
115      * @param item
116      *            the legend item.
117      * 
118      * @return The block.
119      */
120     protected void createLegendBlock( BlockContainer blockcontainerLabel )
121     {
122 
123         XYPlot xyplot = (XYPlot) report.getPlot();
124 
125         int nbRenderer = xyplot.getDatasetCount();
126         BlockContainer blockcontainer = new BlockContainer( new BorderArrangement() );
127         if ( nbRenderer > 1 )
128         {
129             BlockContainer oldLegendBlockContainer =
130                 new BlockContainer( new FlowArrangement( HorizontalAlignment.LEFT, VerticalAlignment.TOP, 2.0D, 2.0D ) );
131             for ( int i = 0; i < nbRenderer; i++ )
132             {
133                 LegendTitle legendtitle = new LegendTitle( xyplot.getRenderer( i ) );
134                 legendtitle.setMargin( new RectangleInsets( 1.0, 1.0, 1.0, 1.0 ) );
135                 legendtitle.setFrame( new LineBorder() );
136                 legendtitle.setBackgroundPaint( ChartColor.WHITE );
137                 oldLegendBlockContainer.add( legendtitle );
138             }
139 
140             blockcontainer.add( oldLegendBlockContainer, RectangleEdge.LEFT );
141         }
142         else
143         {
144             LegendTitle legendtitle = new LegendTitle( xyplot.getRenderer( 0 ) );
145             legendtitle.setMargin( new RectangleInsets( 1.0, 1.0, 1.0, 1.0 ) );
146             legendtitle.setFrame( new LineBorder() );
147             legendtitle.setBackgroundPaint( ChartColor.WHITE );
148             blockcontainer.add( legendtitle, RectangleEdge.LEFT );
149         }
150 
151         LegendTitle legendtitle1 = new LegendTitle( xyplot.getRenderer() );
152         legendtitle1.setMargin( new RectangleInsets( 1.0, 1.0, 1.0, 1.0 ) );
153         legendtitle1.setFrame( new LineBorder() );
154         legendtitle1.setBackgroundPaint( ChartColor.WHITE );
155         legendtitle1.setWrapper( blockcontainerLabel );
156 
157         blockcontainer.add( legendtitle1, RectangleEdge.RIGHT );
158         blockcontainer.add( new EmptyBlock( 1000D, 0.0D ) );
159 
160         CompositeTitle compositetitle = new CompositeTitle( blockcontainer );
161         compositetitle.setPosition( RectangleEdge.BOTTOM );
162         report.clearSubtitles();
163         report.addSubtitle( compositetitle );
164     }
165 
166     /**
167      * Creates a legend item block.
168      * 
169      * @param item
170      *            the legend item.
171      * 
172      * @return The block.
173      */
174     protected Block createLegendItemBlock( LegendItem item, int i )
175     {
176         BlockContainer result = null;
177         LegendGraphic lg = new LegendGraphic( item.getShape(), item.getFillPaint() );
178         lg.setFillPaintTransformer( item.getFillPaintTransformer() );
179         lg.setShapeFilled( true );
180         lg.setLine( item.getLine() );
181         lg.setLineStroke( item.getLineStroke() );
182         lg.setLinePaint( item.getFillPaint() );
183         lg.setLineVisible( true );
184         lg.setShapeVisible( true );
185         lg.setShapeOutlineVisible( true );
186         lg.setOutlinePaint( item.getFillPaint() );
187         lg.setOutlineStroke( item.getOutlineStroke() );
188         lg.setPadding( new RectangleInsets( 2.0, 2.0, 2.0, 2.0 ) );
189 
190         LegendItemBlockContainer legendItem = new LegendItemBlockContainer( new BorderArrangement(), 0, i );
191         lg.setShapeAnchor( RectangleAnchor.CENTER );
192         lg.setShapeLocation( RectangleAnchor.CENTER );
193         legendItem.add( lg, RectangleEdge.LEFT );
194 
195         LabelBlock labelBlock = new LabelBlock( item.getLabel(), new Font( "SansSerif", Font.BOLD, 10 ), Color.black );
196         labelBlock.setPadding( new RectangleInsets( 2.0, 2.0, 2.0, 2.0 ) );
197         legendItem.add( labelBlock );
198         legendItem.setToolTipText( item.getToolTipText() );
199         legendItem.setURLText( item.getURLText() );
200 
201         result = new BlockContainer( new CenterArrangement() );
202         result.add( legendItem );
203 
204         return result;
205     }
206 
207 }