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