1 | package org.expeditee.items.widgets.charts;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Font;
|
---|
5 | import java.awt.event.KeyEvent;
|
---|
6 | import java.awt.event.KeyListener;
|
---|
7 | import java.util.Collection;
|
---|
8 |
|
---|
9 | import javax.swing.JPanel;
|
---|
10 |
|
---|
11 | import org.expeditee.gui.ColorUtils;
|
---|
12 | import org.expeditee.gui.Frame;
|
---|
13 | import org.expeditee.gui.FrameIO;
|
---|
14 | import org.expeditee.gui.FrameKeyboardActions;
|
---|
15 | import org.expeditee.gui.FunctionKey;
|
---|
16 | import org.expeditee.items.Item;
|
---|
17 | import org.expeditee.items.Text;
|
---|
18 | import org.expeditee.items.widgets.DataFrameWidget;
|
---|
19 | import org.jfree.chart.ChartPanel;
|
---|
20 | import org.jfree.chart.JFreeChart;
|
---|
21 | import org.jfree.chart.plot.Plot;
|
---|
22 | import org.jfree.chart.plot.PlotOrientation;
|
---|
23 | import org.jfree.chart.plot.ValueAxisPlot2;
|
---|
24 | import org.jfree.chart.title.LegendTitle;
|
---|
25 | import org.jfree.chart.title.TextTitle;
|
---|
26 |
|
---|
27 | public abstract class AbstractChart extends DataFrameWidget {
|
---|
28 |
|
---|
29 | protected static final String DEFAULT_TITLE = "";
|
---|
30 |
|
---|
31 | protected static final String DEFAULT_XAXIS = "X";
|
---|
32 |
|
---|
33 | protected static final String DEFAULT_YAXIS = "Y";
|
---|
34 |
|
---|
35 | private JFreeChart _chart = null;
|
---|
36 |
|
---|
37 | private LegendTitle _legend = null;
|
---|
38 |
|
---|
39 | protected JFreeChart getChart() {
|
---|
40 | return _chart;
|
---|
41 | }
|
---|
42 |
|
---|
43 | public AbstractChart(Text source, String[] args) {
|
---|
44 | super(source, new JPanel(), 100, 300, -1, 100, 300, -1);
|
---|
45 |
|
---|
46 | init();
|
---|
47 | refresh();
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | protected String[] getArgs() {
|
---|
52 | return null;
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected void init() {
|
---|
56 | // create a chart...
|
---|
57 | _chart = createNewChart();
|
---|
58 | _chart.getPlot().setNoDataMessage("Add link to data frame");
|
---|
59 | _legend = _chart.getLegend();
|
---|
60 | ChartPanel cp = new ChartPanel(_chart);
|
---|
61 | cp.setPopupMenu(null);
|
---|
62 | final Item firstItem = getItems().get(0);
|
---|
63 | cp.addKeyListener(new KeyListener() {
|
---|
64 | public void keyPressed(KeyEvent e) {
|
---|
65 | int index = e.getKeyCode() - KeyEvent.VK_F1 + 1;
|
---|
66 | // Make sure the key is within range
|
---|
67 | if (index < 0 || index >= FunctionKey.values().length)
|
---|
68 | return;
|
---|
69 |
|
---|
70 | FunctionKey key = FunctionKey.values()[index];
|
---|
71 | switch (key) {
|
---|
72 | case SizeUp:
|
---|
73 | FrameKeyboardActions.SetSize(firstItem, 1, false, true);
|
---|
74 | break;
|
---|
75 | case SizeDown:
|
---|
76 | FrameKeyboardActions.SetSize(firstItem, -1, false, true);
|
---|
77 | break;
|
---|
78 | case ChangeColor:
|
---|
79 | if (e.isControlDown()) {
|
---|
80 | Color newColor = ColorUtils.getNextColor(getSource()
|
---|
81 | .getFillColor(), Frame.COLOR_WHEEL, null);
|
---|
82 | if (e.isShiftDown()) {
|
---|
83 | newColor = null;
|
---|
84 | _chart.getPlot().setBackgroundPaint(
|
---|
85 | Plot.DEFAULT_BACKGROUND_PAINT);
|
---|
86 | } else {
|
---|
87 | _chart.getPlot().setBackgroundPaint(newColor);
|
---|
88 | }
|
---|
89 | getSource().setFillColor(newColor);
|
---|
90 | } else {
|
---|
91 | Color newColor = ColorUtils.getNextColor(getSource()
|
---|
92 | .getBackgroundColor(), Frame.COLOR_WHEEL, null);
|
---|
93 | if (e.isShiftDown()) {
|
---|
94 | newColor = null;
|
---|
95 | _chart
|
---|
96 | .setBackgroundPaint(JFreeChart.DEFAULT_BACKGROUND_PAINT);
|
---|
97 | } else {
|
---|
98 | _chart.setBackgroundPaint(newColor);
|
---|
99 | }
|
---|
100 | getSource().setBackgroundColor(newColor);
|
---|
101 |
|
---|
102 | }
|
---|
103 | break;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | public void keyReleased(KeyEvent e) {
|
---|
108 | }
|
---|
109 |
|
---|
110 | public void keyTyped(KeyEvent e) {
|
---|
111 | }
|
---|
112 | });
|
---|
113 | super._swingComponent = cp;
|
---|
114 | Color c = getSource().getFillColor();
|
---|
115 | if (c != null) {
|
---|
116 | _chart.getPlot().setBackgroundPaint(c);
|
---|
117 | }
|
---|
118 | c = getSource().getBackgroundColor();
|
---|
119 | if (c != null) {
|
---|
120 | _chart.setBackgroundPaint(c);
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | protected abstract JFreeChart createNewChart();
|
---|
125 |
|
---|
126 | @Override
|
---|
127 | public void refresh() {
|
---|
128 | super.refresh();
|
---|
129 | // Get the data from the linked frame
|
---|
130 | Frame dataFrame = getDataFrame();
|
---|
131 |
|
---|
132 | _chart.clearSubtitles();
|
---|
133 |
|
---|
134 | if (dataFrame != null) {
|
---|
135 | _chart.setTitle(dataFrame.getTitle());
|
---|
136 |
|
---|
137 | refreshData(dataFrame);
|
---|
138 |
|
---|
139 | if (dataFrame.hasAnnotation("legend")) {
|
---|
140 | _chart.addLegend(_legend);
|
---|
141 | }
|
---|
142 | if (dataFrame.hasAnnotation("subtitle")) {
|
---|
143 | getChart().addSubtitle(
|
---|
144 | new TextTitle(dataFrame.getAnnotationValue("subtitle"),
|
---|
145 | JFreeChart.DEFAULT_TITLE_FONT.deriveFont(
|
---|
146 | Font.ITALIC,
|
---|
147 | JFreeChart.DEFAULT_TITLE_FONT
|
---|
148 | .getSize2D() * .7F)));
|
---|
149 | }
|
---|
150 |
|
---|
151 | refreshPlot(dataFrame, _chart.getPlot());
|
---|
152 |
|
---|
153 | } else {
|
---|
154 | _chart.setTitle(this.getClass().getSimpleName() + " Chart");
|
---|
155 | }
|
---|
156 | super._swingComponent.invalidate();
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * @param dataFrame
|
---|
161 | */
|
---|
162 | protected void refreshPlot(Frame dataFrame, Plot simplePlot) {
|
---|
163 | if (this.fixedAxis())
|
---|
164 | return;
|
---|
165 | ValueAxisPlot2 plot = (ValueAxisPlot2) simplePlot;
|
---|
166 | if (dataFrame.hasAnnotation("xaxis")) {
|
---|
167 | plot.getDomainAxis()
|
---|
168 | .setLabel(dataFrame.getAnnotationValue("xaxis"));
|
---|
169 | }
|
---|
170 | if (dataFrame.hasAnnotation("yaxis")) {
|
---|
171 | plot.getRangeAxis().setLabel(dataFrame.getAnnotationValue("yaxis"));
|
---|
172 | }
|
---|
173 | if (dataFrame.hasAnnotation("horizontal")) {
|
---|
174 | plot.setOrientation(PlotOrientation.HORIZONTAL);
|
---|
175 | } else {
|
---|
176 | plot.setOrientation(PlotOrientation.VERTICAL);
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | private boolean fixedAxis() {
|
---|
181 | return !(this instanceof ValueAxisPlot2);
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * @param dataFrame
|
---|
186 | */
|
---|
187 | protected void refreshData(Frame dataFrame) {
|
---|
188 | clearSubjects();
|
---|
189 | boolean swap = false;
|
---|
190 | if (dataFrame.hasAnnotation("swap")) {
|
---|
191 | swap = true;
|
---|
192 | }
|
---|
193 | boolean foundData = false;
|
---|
194 | // First do a pass to see if there are any line ends
|
---|
195 | Collection<Text> textItems = dataFrame.getNonAnnotationText(true);
|
---|
196 | textItems.remove(dataFrame.getTitleItem());
|
---|
197 |
|
---|
198 | // Search for line ends
|
---|
199 | for (Text text : textItems) {
|
---|
200 | if (!text.isLineEnd()) {
|
---|
201 | continue;
|
---|
202 | }
|
---|
203 | foundData |= addCategoryData(text.getText(), text
|
---|
204 | .getEnclosedNonAnnotationText(), swap);
|
---|
205 | }
|
---|
206 |
|
---|
207 | // As a second option look for linked items on the frame
|
---|
208 | if (!foundData) {
|
---|
209 | for (Text category : textItems) {
|
---|
210 | if (category.isLineEnd() || category.getLink() == null)
|
---|
211 | continue;
|
---|
212 | Frame linkFrame = FrameIO.LoadFrame(category.getAbsoluteLink());
|
---|
213 | if (linkFrame == null)
|
---|
214 | continue;
|
---|
215 |
|
---|
216 | if (foundData |= addCategoryData(category.getText(), linkFrame
|
---|
217 | .getNonAnnotationText(true), swap)) {
|
---|
218 | addSubject(linkFrame);
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | // If there were no groupings in boxes then just get all the values on
|
---|
224 | // the frame
|
---|
225 | if (!foundData) {
|
---|
226 | foundData |= addCategoryData("", textItems, swap);
|
---|
227 | }
|
---|
228 | if (foundData) {
|
---|
229 | addSubject(dataFrame);
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | protected boolean addCategoryData(String categoryName,
|
---|
234 | Collection<Text> items, boolean swap) {
|
---|
235 | return true;
|
---|
236 | }
|
---|
237 |
|
---|
238 | @Override
|
---|
239 | public void setLink(String link) {
|
---|
240 | super.setLink(link);
|
---|
241 |
|
---|
242 | Frame oldDataFrame = getDataFrame();
|
---|
243 | if (oldDataFrame != null)
|
---|
244 | clearSubjects();
|
---|
245 | setDataFrame(null);
|
---|
246 | }
|
---|
247 |
|
---|
248 | @Override
|
---|
249 | public float getMinimumBorderThickness() {
|
---|
250 | return 1.0F;
|
---|
251 | }
|
---|
252 | }
|
---|