- Timestamp:
- 08/11/08 18:03:36 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/items/widgets/charts/AbstractChart.java
r205 r213 1 1 package org.expeditee.items.widgets.charts; 2 2 3 import java.awt.Color; 3 4 import java.awt.Font; 5 import java.awt.event.KeyEvent; 6 import java.awt.event.KeyListener; 7 import java.util.Collection; 4 8 5 9 import javax.swing.JPanel; 6 10 7 import org.expeditee.gui.AttributeUtils; 8 import org.expeditee.gui.AttributeValuePair; 11 import org.expeditee.gui.ColorUtils; 9 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; 10 17 import org.expeditee.items.Text; 11 import org.expeditee.items.widgets. InteractiveWidget;18 import org.expeditee.items.widgets.DataFrameWidget; 12 19 import org.jfree.chart.ChartPanel; 13 20 import org.jfree.chart.JFreeChart; 14 import org.jfree.chart.labels.PieSectionLabelGenerator;15 import org.jfree.chart.plot.PiePlot;16 21 import org.jfree.chart.plot.Plot; 22 import org.jfree.chart.plot.PlotOrientation; 23 import org.jfree.chart.plot.ValueAxisPlot2; 17 24 import org.jfree.chart.title.LegendTitle; 18 25 import org.jfree.chart.title.TextTitle; 19 import org.jfree.data.general.AbstractDataset; 20 import org.jfree.data.general.DefaultPieDataset; 21 22 public abstract class AbstractChart extends InteractiveWidget { 26 27 public abstract class AbstractChart extends DataFrameWidget { 23 28 24 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"; 25 34 26 35 private JFreeChart _chart = null; … … 51 60 ChartPanel cp = new ChartPanel(_chart); 52 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 }); 53 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 } 54 122 } 55 123 … … 61 129 // Get the data from the linked frame 62 130 Frame dataFrame = getDataFrame(); 63 131 64 132 _chart.clearSubtitles(); 65 133 … … 92 160 * @param dataFrame 93 161 */ 94 protected abstract void refreshPlot(Frame dataFrame, Plot plot); 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 } 95 183 96 184 /** 97 185 * @param dataFrame 98 186 */ 99 protected abstract void refreshData(Frame dataFrame); 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 } 100 247 101 248 @Override
Note:
See TracChangeset
for help on using the changeset viewer.