- Timestamp:
- 08/13/08 16:21:37 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/items/widgets/charts/AbstractChart.java
r213 r220 1 1 package org.expeditee.items.widgets.charts; 2 2 3 import java.awt.BasicStroke; 3 4 import java.awt.Color; 4 5 import java.awt.Font; 6 import java.awt.Paint; 5 7 import java.awt.event.KeyEvent; 6 8 import java.awt.event.KeyListener; 7 9 import java.util.Collection; 10 import java.util.LinkedHashMap; 11 import java.util.Map; 8 12 9 13 import javax.swing.JPanel; … … 19 23 import org.jfree.chart.ChartPanel; 20 24 import org.jfree.chart.JFreeChart; 25 import org.jfree.chart.block.BlockBorder; 21 26 import org.jfree.chart.plot.Plot; 22 import org.jfree.chart.plot.PlotOrientation;23 import org.jfree.chart.plot.ValueAxisPlot2;24 27 import org.jfree.chart.title.LegendTitle; 25 28 import org.jfree.chart.title.TextTitle; 29 import org.jfree.chart.title.Title; 30 import org.jfree.ui.RectangleInsets; 26 31 27 32 public abstract class AbstractChart extends DataFrameWidget { … … 36 41 37 42 private LegendTitle _legend = null; 43 44 protected Map<String, Paint> _paints; 38 45 39 46 protected JFreeChart getChart() { … … 43 50 public AbstractChart(Text source, String[] args) { 44 51 super(source, new JPanel(), 100, 300, -1, 100, 300, -1); 45 52 _paints = new LinkedHashMap<String, Paint>(); 46 53 init(); 47 54 refresh(); … … 71 78 switch (key) { 72 79 case SizeUp: 73 FrameKeyboardActions.SetSize(firstItem, 1, false, true);80 // FrameKeyboardActions.SetSize(firstItem, 1, false, true); 74 81 break; 75 82 case SizeDown: 76 FrameKeyboardActions.SetSize(firstItem, -1, false, true);83 // FrameKeyboardActions.SetSize(firstItem, -1, false, true); 77 84 break; 78 85 case ChangeColor: 79 86 if (e.isControlDown()) { 80 Color newColor = ColorUtils.getNextColor(getSource() 81 .getFillColor(), Frame.COLOR_WHEEL, null); 87 82 88 if (e.isShiftDown()) { 83 newColor = null; 84 _chart.getPlot().setBackgroundPaint( 85 Plot.DEFAULT_BACKGROUND_PAINT); 89 setFillColor(null); 86 90 } else { 87 _chart.getPlot().setBackgroundPaint(newColor); 91 Color newColor = ColorUtils.getNextColor( 92 getSource().getFillColor(), 93 Frame.COLOR_WHEEL, null); 94 setFillColor(newColor); 88 95 } 89 getSource().setFillColor(newColor);90 96 } else { 91 Color newColor = ColorUtils.getNextColor(getSource()92 .getBackgroundColor(), Frame.COLOR_WHEEL, null);93 97 if (e.isShiftDown()) { 94 newColor = null; 95 _chart 96 .setBackgroundPaint(JFreeChart.DEFAULT_BACKGROUND_PAINT); 98 setBackgroundColor(null); 97 99 } else { 98 _chart.setBackgroundPaint(newColor); 100 Color newColor = ColorUtils.getNextColor( 101 getSource().getColor(), Item.COLOR_WHEEL, 102 null); 103 setColor(newColor); 99 104 } 100 getSource().setBackgroundColor(newColor);101 102 105 } 103 106 break; … … 112 115 }); 113 116 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 } 117 118 setColor(getSource().getColor()); 119 setBorderColor(getSource().getBorderColor()); 120 setFillColor(getSource().getFillColor()); 121 setBackgroundColor(getSource().getBackgroundColor()); 122 setThickness(getSource().getThickness(), false); 122 123 } 123 124 … … 125 126 126 127 @Override 127 public void refresh() {128 public final void refresh() { 128 129 super.refresh(); 130 clearData(); 129 131 // Get the data from the linked frame 130 132 Frame dataFrame = getDataFrame(); 131 133 _paints.clear(); 132 134 _chart.clearSubtitles(); 133 135 … … 154 156 _chart.setTitle(this.getClass().getSimpleName() + " Chart"); 155 157 } 158 _chart.getPlot().setDataPaints(_paints); 156 159 super._swingComponent.invalidate(); 157 160 } … … 160 163 * @param dataFrame 161 164 */ 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 } 165 protected abstract void refreshPlot(Frame dataFrame, Plot simplePlot); 183 166 184 167 /** … … 214 197 continue; 215 198 216 if (foundData |= addCategoryData(category.getText(), linkFrame 199 String categoryName = category.getText(); 200 if (foundData |= addCategoryData(categoryName, linkFrame 217 201 .getNonAnnotationText(true), swap)) { 202 Color backgroundColor = category.getBackgroundColor(); 203 if (backgroundColor != null) 204 _paints.put(categoryName, backgroundColor); 218 205 addSubject(linkFrame); 219 206 } … … 226 213 foundData |= addCategoryData("", textItems, swap); 227 214 } 228 if (foundData){215 /* if (foundData) */{ 229 216 addSubject(dataFrame); 230 217 } … … 237 224 238 225 @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 @Override249 226 public float getMinimumBorderThickness() { 250 227 return 1.0F; 251 228 } 229 230 protected abstract void clearData(); 231 232 @Override 233 public void setBackgroundColor(Color c) { 234 super.setBackgroundColor(c); 235 if (_chart == null) 236 return; 237 if (c == null) { 238 _chart.setBackgroundPaint(JFreeChart.DEFAULT_BACKGROUND_PAINT); 239 } else { 240 _chart.setBackgroundPaint(c); 241 } 242 } 243 244 @Override 245 public void setColor(Color c) { 246 super.setColor(c); 247 if (_chart == null) 248 return; 249 if (c == null) { 250 _chart.getTitle().setPaint(TextTitle.DEFAULT_TEXT_PAINT); 251 } else { 252 _chart.getTitle().setPaint(c); 253 } 254 for (Title t : _chart.getSubtitles()) { 255 if (c == null) { 256 t.setPaint(TextTitle.DEFAULT_TEXT_PAINT); 257 } else { 258 t.setPaint(c); 259 } 260 } 261 LegendTitle legend = _chart.getLegend(); 262 if (legend != null) { 263 if (c == null) { 264 legend.setFrame(new BlockBorder(Color.black)); 265 } else { 266 legend.setFrame(new BlockBorder(c)); 267 } 268 } 269 } 270 271 @Override 272 public void setBorderColor(Color c) { 273 super.setBorderColor(c); 274 if (_chart == null) 275 return; 276 if (c == null) { 277 _chart.getPlot().setOutlinePaint(Plot.DEFAULT_OUTLINE_PAINT); 278 } else { 279 _chart.getPlot().setOutlinePaint(c); 280 } 281 } 282 283 @Override 284 public void setFillColor(Color c) { 285 super.setFillColor(c); 286 if (_chart == null) 287 return; 288 LegendTitle legend = _chart.getLegend(); 289 if (c == null) { 290 _chart.getPlot().setBackgroundPaint(Plot.DEFAULT_BACKGROUND_PAINT); 291 if (legend != null) 292 legend.setBackgroundPaint(null); 293 } else { 294 _chart.getPlot().setBackgroundPaint(c); 295 if (legend != null) 296 legend.setBackgroundPaint(c); 297 } 298 } 299 300 @Override 301 public void setThickness(float newThickness, boolean setConnected) { 302 super.setThickness(newThickness, setConnected); 303 if (_chart == null) 304 return; 305 _chart.setBorderStroke(new BasicStroke(newThickness)); 306 _chart.getPlot().setOutlineStroke(new BasicStroke(newThickness)); 307 308 float fontSize = getFontSize(newThickness, TextTitle.DEFAULT_FONT 309 .getSize2D()); 310 LegendTitle legend = _chart.getLegend(); 311 if (legend != null) { 312 legend.setFrame(new BlockBorder(newThickness, newThickness, 313 newThickness, newThickness, getSource().getColor())); 314 legend.setItemFont(legend.getItemFont().deriveFont(fontSize)); 315 float pad = newThickness + 1; 316 float pad2 = pad; 317 legend.setItemLabelPadding(new RectangleInsets(pad, pad, pad, pad)); 318 legend.setLegendItemGraphicPadding(new RectangleInsets(pad2, pad2, 319 pad2, pad2)); 320 legend.setMargin(newThickness,newThickness,newThickness,newThickness); 321 } 322 float pad0 = newThickness - 1; 323 _chart.setPadding(new RectangleInsets(pad0, pad0, pad0, pad0)); 324 TextTitle title = _chart.getTitle(); 325 title.setFont(title.getFont().deriveFont( 326 getFontSize(newThickness, JFreeChart.DEFAULT_TITLE_FONT 327 .getSize2D()))); 328 329 for (Title t : _chart.getSubtitles()) { 330 if (t instanceof TextTitle) { 331 ((TextTitle) t).setFont(legend.getItemFont().deriveFont( 332 fontSize)); 333 } 334 t.setPadding(pad0, pad0, pad0, pad0); 335 } 336 } 337 338 static protected float getFontSize(float newThickness, float oldFontSize) { 339 return (newThickness + 5) * oldFontSize / 6; 340 } 252 341 }
Note:
See TracChangeset
for help on using the changeset viewer.