1 package org.codehaus.mojo.dashboard.report.plugin.chart;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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 );
84 axis.setCategoryMargin( 0.10 );
85 axis.setUpperMargin( 0.02 );
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 }