Ignore:
Timestamp:
08/11/08 18:03:36 (16 years ago)
Author:
ra33
Message:

Added Charts

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/widgets/charts/AbstractChart.java

    r205 r213  
    11package org.expeditee.items.widgets.charts;
    22
     3import java.awt.Color;
    34import java.awt.Font;
     5import java.awt.event.KeyEvent;
     6import java.awt.event.KeyListener;
     7import java.util.Collection;
    48
    59import javax.swing.JPanel;
    610
    7 import org.expeditee.gui.AttributeUtils;
    8 import org.expeditee.gui.AttributeValuePair;
     11import org.expeditee.gui.ColorUtils;
    912import org.expeditee.gui.Frame;
     13import org.expeditee.gui.FrameIO;
     14import org.expeditee.gui.FrameKeyboardActions;
     15import org.expeditee.gui.FunctionKey;
     16import org.expeditee.items.Item;
    1017import org.expeditee.items.Text;
    11 import org.expeditee.items.widgets.InteractiveWidget;
     18import org.expeditee.items.widgets.DataFrameWidget;
    1219import org.jfree.chart.ChartPanel;
    1320import org.jfree.chart.JFreeChart;
    14 import org.jfree.chart.labels.PieSectionLabelGenerator;
    15 import org.jfree.chart.plot.PiePlot;
    1621import org.jfree.chart.plot.Plot;
     22import org.jfree.chart.plot.PlotOrientation;
     23import org.jfree.chart.plot.ValueAxisPlot2;
    1724import org.jfree.chart.title.LegendTitle;
    1825import 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
     27public abstract class AbstractChart extends DataFrameWidget {
    2328
    2429        protected static final String DEFAULT_TITLE = "";
     30
     31        protected static final String DEFAULT_XAXIS = "X";
     32
     33        protected static final String DEFAULT_YAXIS = "Y";
    2534
    2635        private JFreeChart _chart = null;
     
    5160                ChartPanel cp = new ChartPanel(_chart);
    5261                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                });
    53113                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                }
    54122        }
    55123
     
    61129                // Get the data from the linked frame
    62130                Frame dataFrame = getDataFrame();
    63                
     131
    64132                _chart.clearSubtitles();
    65133
     
    92160         * @param dataFrame
    93161         */
    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        }
    95183
    96184        /**
    97185         * @param dataFrame
    98186         */
    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        }
    100247
    101248        @Override
Note: See TracChangeset for help on using the changeset viewer.