View Javadoc

1   package org.codehaus.mojo.dashboard.report.plugin.chart;
2   
3   /*
4    * Copyright 2006 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.Color;
20  import java.awt.Paint;
21  import java.text.NumberFormat;
22  import java.util.Locale;
23  
24  import org.codehaus.mojo.dashboard.report.plugin.utils.ChartUtils;
25  import org.jfree.chart.ChartFactory;
26  import org.jfree.chart.axis.AxisLocation;
27  import org.jfree.chart.axis.CategoryAxis;
28  import org.jfree.chart.axis.NumberAxis;
29  import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
30  import org.jfree.chart.plot.CategoryPlot;
31  import org.jfree.chart.plot.PlotOrientation;
32  import org.jfree.chart.renderer.category.BarRenderer;
33  import org.jfree.data.category.CategoryDataset;
34  import org.jfree.ui.RectangleInsets;
35  
36  /**
37   * 
38   * @author <a href="dvicente72@gmail.com">David Vicente</a>
39   * 
40   */
41  public class BarChartRenderer extends AbstractChartRenderer
42  {
43  
44      private static final double NUMBER_AXIS_RANGE = 1.0D;
45  
46      public BarChartRenderer( IChartStrategy strategy )
47      {
48          super( strategy );
49      }
50  
51      public BarChartRenderer( IChartStrategy strategy, int width, int height )
52      {
53          super( strategy, width, height );
54      }
55  
56      public void createChart()
57      {
58          CategoryDataset categorydataset = (CategoryDataset) this.datasetStrategy.getDataset();
59          report =
60              ChartFactory.createBarChart( this.datasetStrategy.getTitle(), this.datasetStrategy.getYAxisLabel(),
61                                           this.datasetStrategy.getXAxisLabel(), categorydataset,
62                                           PlotOrientation.HORIZONTAL, true, true, false );
63          // report.setBackgroundPaint( Color.lightGray );
64          report.setPadding( new RectangleInsets( 5.0d, 5.0d, 5.0d, 5.0d ) );
65          CategoryPlot categoryplot = (CategoryPlot) report.getPlot();
66          categoryplot.setBackgroundPaint( Color.white );
67          categoryplot.setRangeGridlinePaint( Color.lightGray );
68          categoryplot.setDomainGridlinePaint( Color.lightGray );
69          categoryplot.setRangeAxisLocation( AxisLocation.BOTTOM_OR_LEFT );
70          NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
71          if ( datasetStrategy instanceof CoberturaBarChartStrategy || datasetStrategy instanceof CloverBarChartStrategy
72                          || datasetStrategy instanceof MultiCloverBarChartStrategy )
73          {
74              numberaxis.setRange( 0.0D, BarChartRenderer.NUMBER_AXIS_RANGE );
75              numberaxis.setNumberFormatOverride( NumberFormat.getPercentInstance() );
76          }
77          else
78          {
79              numberaxis.setStandardTickUnits( NumberAxis.createIntegerTickUnits() );
80          }
81          numberaxis.setLowerMargin( 0.0D );
82          CategoryAxis axis = categoryplot.getDomainAxis();
83          axis.setLowerMargin( 0.02 ); // two percent
84          axis.setCategoryMargin( 0.10 ); // ten percent
85          axis.setUpperMargin( 0.02 ); // two percent
86          BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
87          barrenderer.setItemMargin( 0.10 );
88          barrenderer.setDrawBarOutline( false );
89          barrenderer.setBaseItemLabelsVisible( true );
90          if ( datasetStrategy instanceof CoberturaBarChartStrategy || datasetStrategy instanceof CloverBarChartStrategy
91                          || datasetStrategy instanceof MultiCloverBarChartStrategy )
92          {
93              barrenderer.setBaseItemLabelGenerator( new StandardCategoryItemLabelGenerator(
94                                                                                             "{2}",
95                                                                                             NumberFormat.getPercentInstance( Locale.getDefault() ) ) );
96          }
97          else
98          {
99              barrenderer.setBaseItemLabelGenerator( new StandardCategoryItemLabelGenerator() );
100         }
101 
102         int height =
103             ( categorydataset.getColumnCount() * ChartUtils.STANDARD_BARCHART_ENTRY_HEIGHT * categorydataset.getRowCount() );
104         if ( height > ChartUtils.MINIMUM_HEIGHT )
105         {
106             super.setHeight( height );
107         }
108         else
109         {
110             super.setHeight( ChartUtils.MINIMUM_HEIGHT );
111         }
112 
113         Paint[] paints = this.datasetStrategy.getPaintColor();
114 
115         for ( int i = 0; i < categorydataset.getRowCount() && i < paints.length; i++ )
116         {
117             barrenderer.setSeriesPaint( i, paints[i] );
118         }
119 
120     }
121 
122 }