| 1 | |
package net.sf.statcvs.charts; |
| 2 | |
|
| 3 | |
import java.awt.Color; |
| 4 | |
import java.awt.Dimension; |
| 5 | |
import java.awt.Paint; |
| 6 | |
import java.util.ArrayList; |
| 7 | |
import java.util.Collections; |
| 8 | |
import java.util.HashMap; |
| 9 | |
import java.util.Iterator; |
| 10 | |
import java.util.List; |
| 11 | |
import java.util.Map; |
| 12 | |
import java.util.SortedSet; |
| 13 | |
import java.util.Map.Entry; |
| 14 | |
|
| 15 | |
import net.sf.statcvs.Messages; |
| 16 | |
import net.sf.statcvs.model.Author; |
| 17 | |
import net.sf.statcvs.model.Directory; |
| 18 | |
import net.sf.statcvs.model.Revision; |
| 19 | |
import net.sf.statcvs.model.VersionedFile; |
| 20 | |
import net.sf.statcvs.output.ReportConfig; |
| 21 | |
import net.sf.statcvs.pages.HTML; |
| 22 | |
import net.sf.statcvs.util.IntegerMap; |
| 23 | |
|
| 24 | |
import org.jfree.chart.ChartFactory; |
| 25 | |
import org.jfree.chart.JFreeChart; |
| 26 | |
import org.jfree.chart.plot.PiePlot; |
| 27 | |
import org.jfree.data.general.DefaultPieDataset; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
public abstract class DirectoryPieChartMaker { |
| 36 | |
private static final int SLICE_MIN_PERCENT = 5; |
| 37 | |
|
| 38 | |
private final ReportConfig config; |
| 39 | |
private final String title; |
| 40 | 0 | private final String fileName; |
| 41 | 0 | private List directories = new ArrayList(); |
| 42 | |
private final String chartName; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | 0 | |
| 50 | 0 | |
| 51 | 0 | public DirectoryPieChartMaker(final String chartName, final ReportConfig config, final SortedSet directories, final String title, final String fileName) { |
| 52 | 0 | this.chartName = chartName; |
| 53 | 0 | this.config = config; |
| 54 | 0 | this.title = title; |
| 55 | 0 | this.fileName = fileName; |
| 56 | 0 | this.directories = new ArrayList(directories); |
| 57 | 0 | } |
| 58 | |
|
| 59 | 0 | public ChartImage toFile() { |
| 60 | 0 | final DefaultPieDataset data = new DefaultPieDataset(); |
| 61 | 0 | |
| 62 | 0 | final IntegerMap dirSizes = new IntegerMap(); |
| 63 | 0 | Collections.sort(directories); |
| 64 | 0 | Iterator it = directories.iterator(); |
| 65 | 0 | while (it.hasNext()) { |
| 66 | 0 | final Directory dir = (Directory) it.next(); |
| 67 | 0 | dirSizes.addInt(dir, calculateValue(dir)); |
| 68 | 0 | } |
| 69 | 0 | |
| 70 | 0 | int otherSum = 0; |
| 71 | 0 | final Map colors = new HashMap(); |
| 72 | 0 | it = dirSizes.iteratorSortedByValue(); |
| 73 | 0 | while (it.hasNext()) { |
| 74 | 0 | final Directory dir = (Directory) it.next(); |
| 75 | 0 | if (dirSizes.getPercent(dir) >= SLICE_MIN_PERCENT) { |
| 76 | 0 | final String dirName = dir.isRoot() ? "/" : dir.getPath(); |
| 77 | 0 | data.setValue(dirName, dirSizes.getInteger(dir)); |
| 78 | 0 | colors.put(dirName, ChartUtils.getStringColor(dirName)); |
| 79 | 0 | } else { |
| 80 | 0 | otherSum += dirSizes.get(dir); |
| 81 | 0 | } |
| 82 | 0 | } |
| 83 | 0 | data.setValue(Messages.getString("PIE_MODSIZE_OTHER"), new Integer(otherSum)); |
| 84 | 0 | colors.put(Messages.getString("PIE_MODSIZE_OTHER"), Color.GRAY); |
| 85 | 0 | |
| 86 | 0 | final JFreeChart chart = ChartFactory.createPieChart(this.config.getProjectName() + ": " + title, data, false, false, false); |
| 87 | 0 | |
| 88 | 0 | final PiePlot plot = (PiePlot) chart.getPlot(); |
| 89 | 0 | plot.setShadowPaint(null); |
| 90 | 0 | plot.setLabelShadowPaint(null); |
| 91 | 0 | plot.setLabelOutlinePaint(Color.LIGHT_GRAY); |
| 92 | 0 | plot.setForegroundAlpha(0.8f); |
| 93 | 0 | plot.setSectionOutlinePaint(Color.BLACK); |
| 94 | 0 | it = colors.entrySet().iterator(); |
| 95 | 0 | while (it.hasNext()) { |
| 96 | 0 | final Entry entry = (Entry) it.next(); |
| 97 | 0 | plot.setSectionPaint((String) entry.getKey(), (Paint) entry.getValue()); |
| 98 | 0 | } |
| 99 | |
|
| 100 | 0 | plot.setBackgroundPaint(ChartConfigUtil.getPlotColor(chartName)); |
| 101 | 0 | chart.setBackgroundPaint(ChartConfigUtil.getBackgroundColor(chartName)); |
| 102 | 0 | ChartConfigUtil.configureCopyrightNotice(chartName, chart); |
| 103 | 0 | ChartConfigUtil.configureChartBackgroungImage(chartName, chart); |
| 104 | 0 | ChartConfigUtil.configurePlotImage(chartName, chart); |
| 105 | 0 | |
| 106 | 0 | final Dimension dim = ChartConfigUtil.getDimension(chartName, config.getLargeChartSize()); |
| 107 | 0 | |
| 108 | 0 | return this.config.createChartImage(this.fileName, this.title, chart, dim); |
| 109 | 0 | } |
| 110 | 0 | |
| 111 | 0 | protected abstract int calculateValue(Directory directory); |
| 112 | 0 | |
| 113 | 0 | public static class DirectorySizesChartMaker extends DirectoryPieChartMaker { |
| 114 | |
public DirectorySizesChartMaker(final ReportConfig config) { |
| 115 | 0 | super("directory_sizes", config, config.getRepository().getDirectories(), Messages.getString("PIE_DIRSIZE_SUBTITLE"), "directory_sizes.png"); |
| 116 | 0 | } |
| 117 | 0 | |
| 118 | 0 | protected int calculateValue(final Directory directory) { |
| 119 | 0 | int result = 0; |
| 120 | 0 | final Iterator fileIt = directory.getFiles().iterator(); |
| 121 | 0 | while (fileIt.hasNext()) { |
| 122 | 0 | final VersionedFile element = (VersionedFile) fileIt.next(); |
| 123 | 0 | result += element.getCurrentLinesOfCode(); |
| 124 | 0 | } |
| 125 | 0 | return result; |
| 126 | 0 | } |
| 127 | 0 | } |
| 128 | |
|
| 129 | |
public static class CodeDistributionChartMaker extends DirectoryPieChartMaker { |
| 130 | 0 | private static String getFileName(final Author author) { |
| 131 | 0 | return "directory_sizes_" + HTML.escapeAuthorName(author.getName()) + ".png"; |
| 132 | 0 | } |
| 133 | 0 | |
| 134 | 0 | private final Author author; |
| 135 | 0 | |
| 136 | 0 | public CodeDistributionChartMaker(final ReportConfig config, final Author author) { |
| 137 | 0 | super("directory_sizes", config, author.getDirectories(), Messages.getString("PIE_CODEDISTRIBUTION_SUBTITLE") + " " + author.getRealName(), |
| 138 | |
getFileName(author)); |
| 139 | 0 | this.author = author; |
| 140 | 0 | } |
| 141 | 0 | |
| 142 | 0 | public ChartImage toFile() { |
| 143 | 0 | final Iterator it = this.author.getRevisions().iterator(); |
| 144 | 0 | int totalLinesOfCode = 0; |
| 145 | 0 | while (it.hasNext()) { |
| 146 | 0 | final Revision rev = (Revision) it.next(); |
| 147 | 0 | totalLinesOfCode += rev.getNewLines(); |
| 148 | 0 | } |
| 149 | 0 | if (totalLinesOfCode == 0) { |
| 150 | 0 | return null; |
| 151 | 0 | } |
| 152 | 0 | return super.toFile(); |
| 153 | |
} |
| 154 | 0 | |
| 155 | |
protected int calculateValue(final Directory directory) { |
| 156 | 0 | int result = 0; |
| 157 | 0 | final Iterator it = directory.getRevisions().iterator(); |
| 158 | 0 | while (it.hasNext()) { |
| 159 | 0 | final Revision rev = (Revision) it.next(); |
| 160 | 0 | if (!this.author.equals(rev.getAuthor())) { |
| 161 | 0 | continue; |
| 162 | 0 | } |
| 163 | 0 | result += rev.getNewLines(); |
| 164 | 0 | } |
| 165 | 0 | return result; |
| 166 | 0 | } |
| 167 | 0 | } |
| 168 | |
} |