Ignore:
Timestamp:
08/13/08 16:21:37 (16 years ago)
Author:
ra33
Message:
 
File:
1 edited

Legend:

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

    r213 r220  
    11package org.expeditee.items.widgets.charts;
    22
     3import java.awt.BasicStroke;
    34import java.awt.Color;
    45import java.awt.Font;
     6import java.awt.Paint;
    57import java.awt.event.KeyEvent;
    68import java.awt.event.KeyListener;
    79import java.util.Collection;
     10import java.util.LinkedHashMap;
     11import java.util.Map;
    812
    913import javax.swing.JPanel;
     
    1923import org.jfree.chart.ChartPanel;
    2024import org.jfree.chart.JFreeChart;
     25import org.jfree.chart.block.BlockBorder;
    2126import org.jfree.chart.plot.Plot;
    22 import org.jfree.chart.plot.PlotOrientation;
    23 import org.jfree.chart.plot.ValueAxisPlot2;
    2427import org.jfree.chart.title.LegendTitle;
    2528import org.jfree.chart.title.TextTitle;
     29import org.jfree.chart.title.Title;
     30import org.jfree.ui.RectangleInsets;
    2631
    2732public abstract class AbstractChart extends DataFrameWidget {
     
    3641
    3742        private LegendTitle _legend = null;
     43
     44        protected Map<String, Paint> _paints;
    3845
    3946        protected JFreeChart getChart() {
     
    4350        public AbstractChart(Text source, String[] args) {
    4451                super(source, new JPanel(), 100, 300, -1, 100, 300, -1);
    45 
     52                _paints = new LinkedHashMap<String, Paint>();
    4653                init();
    4754                refresh();
     
    7178                                switch (key) {
    7279                                case SizeUp:
    73                                         FrameKeyboardActions.SetSize(firstItem, 1, false, true);
     80                                        // FrameKeyboardActions.SetSize(firstItem, 1, false, true);
    7481                                        break;
    7582                                case SizeDown:
    76                                         FrameKeyboardActions.SetSize(firstItem, -1, false, true);
     83                                        // FrameKeyboardActions.SetSize(firstItem, -1, false, true);
    7784                                        break;
    7885                                case ChangeColor:
    7986                                        if (e.isControlDown()) {
    80                                                 Color newColor = ColorUtils.getNextColor(getSource()
    81                                                                 .getFillColor(), Frame.COLOR_WHEEL, null);
     87
    8288                                                if (e.isShiftDown()) {
    83                                                         newColor = null;
    84                                                         _chart.getPlot().setBackgroundPaint(
    85                                                                         Plot.DEFAULT_BACKGROUND_PAINT);
     89                                                        setFillColor(null);
    8690                                                } else {
    87                                                         _chart.getPlot().setBackgroundPaint(newColor);
     91                                                        Color newColor = ColorUtils.getNextColor(
     92                                                                        getSource().getFillColor(),
     93                                                                        Frame.COLOR_WHEEL, null);
     94                                                        setFillColor(newColor);
    8895                                                }
    89                                                 getSource().setFillColor(newColor);
    9096                                        } else {
    91                                                 Color newColor = ColorUtils.getNextColor(getSource()
    92                                                                 .getBackgroundColor(), Frame.COLOR_WHEEL, null);
    9397                                                if (e.isShiftDown()) {
    94                                                         newColor = null;
    95                                                         _chart
    96                                                                         .setBackgroundPaint(JFreeChart.DEFAULT_BACKGROUND_PAINT);
     98                                                        setBackgroundColor(null);
    9799                                                } else {
    98                                                         _chart.setBackgroundPaint(newColor);
     100                                                        Color newColor = ColorUtils.getNextColor(
     101                                                                        getSource().getColor(), Item.COLOR_WHEEL,
     102                                                                        null);
     103                                                        setColor(newColor);
    99104                                                }
    100                                                 getSource().setBackgroundColor(newColor);
    101 
    102105                                        }
    103106                                        break;
     
    112115                });
    113116                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);
    122123        }
    123124
     
    125126
    126127        @Override
    127         public void refresh() {
     128        public final void refresh() {
    128129                super.refresh();
     130                clearData();
    129131                // Get the data from the linked frame
    130132                Frame dataFrame = getDataFrame();
    131 
     133                _paints.clear();
    132134                _chart.clearSubtitles();
    133135
     
    154156                        _chart.setTitle(this.getClass().getSimpleName() + " Chart");
    155157                }
     158                _chart.getPlot().setDataPaints(_paints);
    156159                super._swingComponent.invalidate();
    157160        }
     
    160163         * @param dataFrame
    161164         */
    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);
    183166
    184167        /**
     
    214197                                        continue;
    215198
    216                                 if (foundData |= addCategoryData(category.getText(), linkFrame
     199                                String categoryName = category.getText();
     200                                if (foundData |= addCategoryData(categoryName, linkFrame
    217201                                                .getNonAnnotationText(true), swap)) {
     202                                        Color backgroundColor = category.getBackgroundColor();
     203                                        if (backgroundColor != null)
     204                                                _paints.put(categoryName, backgroundColor);
    218205                                        addSubject(linkFrame);
    219206                                }
     
    226213                        foundData |= addCategoryData("", textItems, swap);
    227214                }
    228                 if (foundData) {
     215                /* if (foundData) */{
    229216                        addSubject(dataFrame);
    230217                }
     
    237224
    238225        @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
    249226        public float getMinimumBorderThickness() {
    250227                return 1.0F;
    251228        }
     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        }
    252341}
Note: See TracChangeset for help on using the changeset viewer.