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.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
37
38
39 public class StackedBarChartRenderer extends AbstractChartRenderer
40 {
41 private static final double NUMBER_AXIS_RANGE = 1.0D;
42
43
44
45
46
47
48
49 public StackedBarChartRenderer( IChartStrategy strategy )
50 {
51 super( strategy );
52 }
53
54
55
56
57
58
59
60
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
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 }