1 | |
|
2 | |
|
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.Date; |
11 | |
import java.util.HashMap; |
12 | |
import java.util.LinkedList; |
13 | |
import java.util.List; |
14 | |
import java.util.Map; |
15 | |
import java.util.logging.Logger; |
16 | |
|
17 | |
import org.homeunix.thecave.buddi.i18n.BuddiKeys; |
18 | |
import org.homeunix.thecave.buddi.i18n.keys.PluginReportDateRangeChoices; |
19 | |
import org.homeunix.thecave.buddi.plugin.api.BuddiReportPlugin; |
20 | |
import org.homeunix.thecave.buddi.plugin.api.model.ImmutableDocument; |
21 | |
import org.homeunix.thecave.buddi.plugin.api.util.HtmlHelper; |
22 | |
import org.homeunix.thecave.buddi.plugin.api.util.HtmlPage; |
23 | |
import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter; |
24 | |
import org.homeunix.thecave.buddi.util.Formatter; |
25 | |
import org.jfree.chart.ChartFactory; |
26 | |
import org.jfree.chart.JFreeChart; |
27 | |
import org.jfree.chart.plot.PlotOrientation; |
28 | |
import org.jfree.data.category.DefaultCategoryDataset; |
29 | |
|
30 | |
import ca.digitalcave.moss.common.DateUtil; |
31 | |
import ca.digitalcave.moss.common.Version; |
32 | |
import ca.digitalcave.moss.swing.MossDocumentFrame; |
33 | |
|
34 | 2977 | public class NetWorthOverTime extends BuddiReportPlugin { |
35 | |
|
36 | |
public static final long serialVersionUID = 0; |
37 | |
|
38 | |
@Override |
39 | |
public HtmlPage getReport(ImmutableDocument model, MossDocumentFrame frame, Date startDate, Date endDate) { |
40 | 0 | final int NUM_SAMPLES = 12; |
41 | |
|
42 | 0 | DefaultCategoryDataset barData = new DefaultCategoryDataset(); |
43 | |
|
44 | 0 | List<Date> dates = new LinkedList<Date>(); |
45 | 0 | Date date = startDate; |
46 | |
|
47 | 0 | int numberOfDaysBetween = DateUtil.getDaysBetween(startDate, new Date(), false); |
48 | 0 | int daysBetweenReport = numberOfDaysBetween / NUM_SAMPLES; |
49 | |
|
50 | 0 | for (int i = 0; i < NUM_SAMPLES; i++){ |
51 | 0 | date = DateUtil.addDays(startDate, i * daysBetweenReport); |
52 | 0 | if (date.before(new Date())) |
53 | 0 | dates.add(date); |
54 | 0 | Logger.getLogger(this.getClass().getName()).finest("Added date: " + date); |
55 | |
} |
56 | |
|
57 | 0 | dates.add(new Date()); |
58 | |
|
59 | 0 | for (Date d : dates) { |
60 | 0 | long netWorth = model.getNetWorth(d); |
61 | |
|
62 | |
|
63 | 0 | barData.addValue((Number) new Double(netWorth / 100.0), |
64 | |
TextFormatter.getTranslation(BuddiKeys.NET_WORTH), |
65 | |
(Formatter.getDateFormat("MM/dd").format(d))); |
66 | 0 | } |
67 | |
|
68 | |
|
69 | |
|
70 | 0 | JFreeChart chart = ChartFactory.createLineChart( |
71 | |
"", |
72 | |
"", |
73 | |
"", |
74 | |
barData, |
75 | |
PlotOrientation.VERTICAL, |
76 | |
true, |
77 | |
true, |
78 | |
false |
79 | |
); |
80 | |
|
81 | 0 | chart.setBackgroundPaint(Color.WHITE); |
82 | 0 | chart.setBorderStroke(new BasicStroke(0)); |
83 | 0 | chart.getCategoryPlot().setBackgroundPaint(Color.WHITE); |
84 | 0 | chart.getCategoryPlot().setDomainGridlinePaint(Color.BLACK); |
85 | 0 | chart.getCategoryPlot().setRangeGridlinePaint(Color.BLACK); |
86 | |
|
87 | 0 | StringBuilder sb = HtmlHelper.getHtmlHeader( |
88 | |
TextFormatter.getTranslation(BuddiKeys.GRAPH_TITLE_NET_WORTH_OVER_TIME), |
89 | |
null, |
90 | |
startDate, |
91 | |
new Date()); |
92 | |
|
93 | 0 | sb.append("<img class='center_img' src='graph.png' />"); |
94 | 0 | sb.append(HtmlHelper.getHtmlFooter()); |
95 | |
|
96 | 0 | Map<String, BufferedImage> images = new HashMap<String, BufferedImage>(); |
97 | 0 | images.put("graph.png", |
98 | |
chart.createBufferedImage( |
99 | |
Math.min(Toolkit.getDefaultToolkit().getScreenSize().width - 200, 1000), |
100 | |
Toolkit.getDefaultToolkit().getScreenSize().height - 100)); |
101 | |
|
102 | 0 | return new HtmlPage(sb.toString(), images); |
103 | |
} |
104 | |
|
105 | |
public String getName() { |
106 | 0 | return BuddiKeys.GRAPH_TITLE_NET_WORTH_OVER_TIME.toString(); |
107 | |
} |
108 | |
|
109 | |
@Override |
110 | |
public PluginReportDateRangeChoices getDateRangeChoice() { |
111 | 6050 | return PluginReportDateRangeChoices.START_ONLY; |
112 | |
} |
113 | |
|
114 | |
public String getDescription() { |
115 | 3025 | return BuddiKeys.NETWORTH_LINE_GRAPH.toString(); |
116 | |
} |
117 | |
|
118 | |
public boolean isPluginActive() { |
119 | 3025 | return true; |
120 | |
} |
121 | |
|
122 | |
public Version getMaximumVersion() { |
123 | 0 | return null; |
124 | |
} |
125 | |
|
126 | |
public Version getMinimumVersion() { |
127 | 0 | return null; |
128 | |
} |
129 | |
} |