1 package net.sf.statcvs.charts;
2
3 import java.awt.Color;
4 import java.awt.Dimension;
5 import java.text.NumberFormat;
6 import java.util.ArrayList;
7 import java.util.Iterator;
8 import java.util.List;
9
10 import net.sf.statcvs.model.Module;
11 import net.sf.statcvs.output.ReportConfig;
12 import net.sf.statcvs.util.IntegerMap;
13
14 import org.jfree.chart.ChartFactory;
15 import org.jfree.chart.JFreeChart;
16 import org.jfree.chart.plot.PiePlot;
17 import org.jfree.chart.title.TextTitle;
18 import org.jfree.data.general.DefaultPieDataset;
19
20 /**
21 * Class for producing directory pie charts
22 * @author jentzsch
23 * @author Richard Cyganiak (richard@cyganiak.de)
24 * @version $Id: ModuleSizePieChartMaker.java,v 1.2 2009/08/31 19:16:35 benoitx Exp $
25 */
26 public class ModuleSizePieChartMaker {
27 private static final int SLICE_MIN_PERCENT = 5;
28
29 private final ReportConfig config;
30 private final String title;
31 private final String subTitle;
32 private final String fileName;
33 private final List files = new ArrayList();
34 private final String chartName;
35
36 /**
37 * Creates a new PieChartMaker
38 * @param config The report configuration to use
39 * @param directories The set of directories to consider
40 * @param title The chart title
41 * @param fileName The file name for chart
42 */
43 public ModuleSizePieChartMaker(final String chartName, final ReportConfig config, final String title, final String subTitle, final String fileName) {
44 this.chartName = chartName;
45 this.config = config;
46 this.title = title;
47 this.subTitle = subTitle;
48 this.fileName = fileName;
49 }
50
51 public ChartImage toFile() {
52 final DefaultPieDataset data = new DefaultPieDataset();
53
54 final IntegerMap dirSizes = new IntegerMap();
55 Iterator it = config.getRepository().getModules().values().iterator();
56 int total = 0;
57 while (it.hasNext()) {
58 final Module module = (Module) it.next();
59
60 dirSizes.addInt(module.getName(), module.getCurrentLinesOfCode());
61 total += module.getCurrentLinesOfCode();
62 }
63 it = dirSizes.iteratorSortedByValue();
64 final NumberFormat nf = NumberFormat.getNumberInstance();
65 final NumberFormat nf2 = NumberFormat.getPercentInstance();
66 while (it.hasNext()) {
67 final String modName = (String) it.next();
68 final Integer loc = dirSizes.getInteger(modName);
69 final double percent = (double) loc.intValue() / (double) total;
70 final String dirName = modName + " = " + nf.format(loc) + " (" + nf2.format(percent) + ")";
71 data.setValue(dirName, loc);
72 }
73
74 final JFreeChart chart = ChartFactory.createPieChart(this.config.getProjectName() + ": " + title, data, false, false, false);
75 final ArrayList arrayList = new ArrayList();
76 arrayList.add(new TextTitle(subTitle));
77 chart.setSubtitles(arrayList);
78 final PiePlot plot = (PiePlot) chart.getPlot();
79 plot.setShadowPaint(null);
80 plot.setLabelShadowPaint(null);
81 plot.setLabelOutlinePaint(Color.LIGHT_GRAY);
82 plot.setForegroundAlpha(0.8f);
83 plot.setSectionOutlinePaint(Color.BLACK);
84
85 plot.setBackgroundPaint(ChartConfigUtil.getPlotColor(chartName));
86 chart.setBackgroundPaint(ChartConfigUtil.getBackgroundColor(chartName));
87 ChartConfigUtil.configureCopyrightNotice(chartName, chart);
88 ChartConfigUtil.configureChartBackgroungImage(chartName, chart);
89 ChartConfigUtil.configurePlotImage(chartName, chart);
90
91 final Dimension dim = ChartConfigUtil.getDimension(chartName, config.getLargeChartSize());
92
93 return this.config.createChartImage(this.fileName, this.title, chart, dim);
94 }
95 }