Coverage Report - org.homeunix.thecave.buddi.plugin.builtin.report.IncomePieGraph
 
Classes in this File Line Coverage Branch Coverage Complexity
IncomePieGraph
5%
4/69
0%
0/38
3.111
IncomePieGraph$1
0%
0/2
N/A
3.111
 
 1  
 /*
 2  
  * Created on Sep 14, 2006 by wyatt
 3  
  */
 4  
 package org.homeunix.thecave.buddi.plugin.builtin.report;
 5  
 
 6  
 import java.awt.BasicStroke;
 7  
 import java.awt.Color;
 8  
 import java.awt.Toolkit;
 9  
 import java.awt.image.BufferedImage;
 10  
 import java.util.Collections;
 11  
 import java.util.Comparator;
 12  
 import java.util.Date;
 13  
 import java.util.HashMap;
 14  
 import java.util.LinkedList;
 15  
 import java.util.List;
 16  
 import java.util.Map;
 17  
 
 18  
 import org.homeunix.thecave.buddi.i18n.BuddiKeys;
 19  
 import org.homeunix.thecave.buddi.i18n.keys.PluginReportDateRangeChoices;
 20  
 import org.homeunix.thecave.buddi.plugin.api.BuddiReportPlugin;
 21  
 import org.homeunix.thecave.buddi.plugin.api.model.ImmutableBudgetCategory;
 22  
 import org.homeunix.thecave.buddi.plugin.api.model.ImmutableDocument;
 23  
 import org.homeunix.thecave.buddi.plugin.api.model.ImmutableSplit;
 24  
 import org.homeunix.thecave.buddi.plugin.api.model.ImmutableTransaction;
 25  
 import org.homeunix.thecave.buddi.plugin.api.model.ImmutableTransactionSplit;
 26  
 import org.homeunix.thecave.buddi.plugin.api.util.HtmlHelper;
 27  
 import org.homeunix.thecave.buddi.plugin.api.util.HtmlPage;
 28  
 import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter;
 29  
 import org.jfree.chart.ChartFactory;
 30  
 import org.jfree.chart.JFreeChart;
 31  
 import org.jfree.chart.plot.PiePlot;
 32  
 import org.jfree.data.general.DefaultPieDataset;
 33  
 
 34  
 import ca.digitalcave.moss.common.Version;
 35  
 import ca.digitalcave.moss.swing.MossDocumentFrame;
 36  
 
 37  2977
 public class IncomePieGraph extends BuddiReportPlugin {
 38  
         
 39  
         public static final long serialVersionUID = 0;
 40  
         
 41  
         @Override
 42  
         public HtmlPage getReport(ImmutableDocument model, MossDocumentFrame frame, Date startDate, Date endDate) {
 43  0
                 DefaultPieDataset pieData = new DefaultPieDataset();
 44  
                 
 45  0
                 final Map<ImmutableBudgetCategory, Long> categories = getIncomeBetween(model, startDate, endDate);
 46  
                 
 47  0
                 List<ImmutableBudgetCategory> cats = new LinkedList<ImmutableBudgetCategory>(categories.keySet());
 48  0
                 Collections.sort(cats);
 49  
                 
 50  0
                 long totalIncome = 0;
 51  
                 
 52  0
                 for (ImmutableBudgetCategory c : cats) {
 53  0
                         totalIncome += categories.get(c);
 54  
                 }
 55  
                 
 56  0
                 Collections.sort(cats, new Comparator<ImmutableBudgetCategory>(){
 57  
                         public int compare(ImmutableBudgetCategory o1,
 58  
                                         ImmutableBudgetCategory o2) {
 59  0
                                 return categories.get(o1).compareTo(categories.get(o2));
 60  
                         }
 61  
                 });
 62  
                 
 63  0
                 for (ImmutableBudgetCategory c : cats) {
 64  0
                         if (categories.get(c) > 0)
 65  0
                                 pieData.setValue(TextFormatter.getTranslation(c.toString()) + " (" + ((10000 * categories.get(c)) / totalIncome) / 100.0 + "%)", (double) categories.get(c));
 66  
                 }
 67  
                 
 68  
                                 
 69  0
                 JFreeChart chart = ChartFactory.createPieChart(
 70  
                                 "",
 71  
                                 pieData,             // data
 72  
                                 true,               // include legend
 73  
                                 true,
 74  
                                 false
 75  
                 );
 76  
                 
 77  0
                 chart.setBackgroundPaint(Color.WHITE);
 78  0
                 chart.setBorderStroke(new BasicStroke(0));
 79  0
                 chart.getPlot().setBackgroundPaint(Color.WHITE);
 80  0
                 chart.getPlot().setOutlinePaint(Color.WHITE);
 81  
 
 82  0
                 ((PiePlot) chart.getPlot()).setLabelGenerator(new BuddiPieSectionLabelGenerator());
 83  
                                 
 84  0
                 StringBuilder sb = HtmlHelper.getHtmlHeader(
 85  
                                 TextFormatter.getTranslation(BuddiKeys.GRAPH_TITLE_INCOME_PIE_GRAPH), 
 86  
                                 TextFormatter.getFormattedCurrency(totalIncome), 
 87  
                                 startDate, 
 88  
                                 endDate);
 89  
 
 90  0
                 sb.append("<img class='center_img' src='graph.png' />");
 91  0
                 sb.append(HtmlHelper.getHtmlFooter());
 92  
                 
 93  0
                 Map<String, BufferedImage> images = new HashMap<String, BufferedImage>();
 94  0
                 images.put("graph.png", 
 95  
                                 chart.createBufferedImage(
 96  
                                                 Math.min(Toolkit.getDefaultToolkit().getScreenSize().width - 200, 1000), 
 97  
                                                 Toolkit.getDefaultToolkit().getScreenSize().height - 100));
 98  
                 
 99  0
                 return new HtmlPage(sb.toString(), images);
 100  
         }
 101  
                 
 102  
         private Map<ImmutableBudgetCategory, Long> getIncomeBetween(ImmutableDocument model, Date startDate, Date endDate){
 103  0
                 List<ImmutableTransaction> transactions = model.getImmutableTransactions(startDate, endDate);
 104  0
                 Map<ImmutableBudgetCategory, Long> categories = new HashMap<ImmutableBudgetCategory, Long>();
 105  
                 
 106  
                 //This map is where we store the totals for this time period.
 107  0
                 for (ImmutableBudgetCategory category : model.getImmutableBudgetCategories()) {
 108  0
                         if (category.isIncome())
 109  0
                                 categories.put(category, Long.valueOf(0));
 110  
                 }
 111  
                 
 112  0
                 for (ImmutableTransaction transaction : transactions) {
 113  0
                         if (!transaction.isDeleted()){
 114  
                                 //Sum up the amounts for each category.
 115  0
                                 if (transaction.getFrom() instanceof ImmutableBudgetCategory){
 116  0
                                         ImmutableBudgetCategory c = (ImmutableBudgetCategory) transaction.getFrom();
 117  0
                                         if (c.isIncome()){
 118  0
                                                 Long l = categories.get(c);
 119  0
                                                 l += transaction.getAmount();
 120  0
                                                 categories.put(c, l);
 121  
                                         }
 122  0
                                 }
 123  0
                                 else if (transaction.getTo() instanceof ImmutableBudgetCategory){
 124  0
                                         ImmutableBudgetCategory c = (ImmutableBudgetCategory) transaction.getTo();
 125  0
                                         if (c.isIncome()){
 126  0
                                                 Long l = categories.get(c);
 127  0
                                                 l += transaction.getAmount();
 128  0
                                                 categories.put(c, l);
 129  
                                         }
 130  
                                 }
 131  
                                 
 132  
                                 //Check for splits as well
 133  0
                                 if (transaction.getTo() instanceof ImmutableSplit){
 134  0
                                         for (ImmutableTransactionSplit split : transaction.getImmutableToSplits()) {
 135  0
                                                 if (split.getSource() instanceof ImmutableBudgetCategory){
 136  0
                                                         ImmutableBudgetCategory c = (ImmutableBudgetCategory) split.getSource();
 137  0
                                                         if (c.isIncome()){
 138  0
                                                                 Long l = categories.get(c);
 139  0
                                                                 l += split.getAmount();
 140  0
                                                                 categories.put(c, l);
 141  
                                                         }
 142  0
                                                 }
 143  
                                         }
 144  
                                 }
 145  0
                                 if (transaction.getFrom() instanceof ImmutableSplit){
 146  0
                                         for (ImmutableTransactionSplit split : transaction.getImmutableFromSplits()) {
 147  0
                                                 if (split.getSource() instanceof ImmutableBudgetCategory){
 148  0
                                                         ImmutableBudgetCategory c = (ImmutableBudgetCategory) split.getSource();
 149  0
                                                         if (c.isIncome()){
 150  0
                                                                 Long l = categories.get(c);
 151  0
                                                                 l += split.getAmount();
 152  0
                                                                 categories.put(c, l);
 153  
                                                         }
 154  0
                                                 }
 155  
                                         }
 156  
                                 }
 157  
                         }
 158  
                 }
 159  
                                 
 160  0
                 return categories;
 161  
         }
 162  
 
 163  
         @Override
 164  
         public PluginReportDateRangeChoices getDateRangeChoice() {
 165  3025
                 return PluginReportDateRangeChoices.INTERVAL;
 166  
         }
 167  
         
 168  
         public String getDescription() {
 169  3025
                 return BuddiKeys.GRAPH_DESCRIPTION_INCOME_PIE_GRAPH.toString();
 170  
         }
 171  
 
 172  
         public String getName() {
 173  0
                 return BuddiKeys.GRAPH_TITLE_INCOME_PIE_GRAPH.toString();
 174  
         }
 175  
 
 176  
         public boolean isPluginActive() {
 177  3025
                 return true;
 178  
         }
 179  
 
 180  
         public Version getMaximumVersion() {
 181  0
                 return null;
 182  
         }
 183  
         
 184  
         public Version getMinimumVersion() {
 185  0
                 return null;
 186  
         }
 187  
 }