001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.jfreechart;
025    
026    import java.awt.BasicStroke;
027    import java.awt.Color;
028    
029    import org.jfree.chart.ChartFactory;
030    import org.jfree.chart.axis.LogarithmicAxis;
031    import org.jfree.chart.axis.NumberAxis;
032    import org.jfree.chart.plot.PlotOrientation;
033    import org.jfree.chart.plot.XYPlot;
034    import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
035    import org.jfree.data.general.Dataset;
036    import org.jfree.data.xy.XYDataset;
037    import org.ujmp.core.Matrix;
038    import org.ujmp.core.util.StringUtil;
039    import org.ujmp.gui.MatrixGUIObject;
040    
041    public class MatrixChartPanel extends AbstractChartPanel {
042            private static final long serialVersionUID = 3661988250162505586L;
043    
044            public MatrixChartPanel(Matrix m) {
045                    this((MatrixGUIObject) m.getGUIObject());
046            }
047    
048            public MatrixChartPanel(MatrixGUIObject guiObject) {
049                    this(guiObject, new ChartConfiguration());
050            }
051    
052            public MatrixChartPanel(Matrix m, ChartConfiguration config) {
053                    this((MatrixGUIObject) m.getGUIObject(), config);
054            }
055    
056            public MatrixChartPanel(MatrixGUIObject matrix, ChartConfiguration config) {
057                    super(matrix, config);
058            }
059    
060            public synchronized void redraw() {
061                    Dataset dataset = null;
062                    dataset = new XYSeriesCollectionWrapper(getMatrix());
063                    // dataset = new CategoryDatasetWrapper(getMatrix());
064    
065                    String title = getMatrix().getLabel();
066                    String xLabel = StringUtil.format(getMatrix().getMatrix()
067                                    .getAxisAnnotation(Matrix.ROW));
068                    String yLabel = null;
069    
070                    // setChart(ChartFactory.createLineChart(title, xLabel, yLabel,
071                    // (CategoryDataset) dataset, PlotOrientation.VERTICAL, true,
072                    // true, false));
073                    setChart(ChartFactory.createXYLineChart(title, xLabel, yLabel,
074                                    (XYDataset) dataset, PlotOrientation.VERTICAL, true, true,
075                                    false));
076    
077                    XYPlot plot = getChart().getXYPlot();
078    
079                    if (getConfig().isLogScaleDomain()) {
080                            try {
081                                    NumberAxis axis = new LogarithmicAxis(null);
082                                    plot.setDomainAxis(axis);
083                            } catch (Exception e) {
084                                    NumberAxis axis = new NumberAxis();
085                                    plot.setDomainAxis(axis);
086                            }
087                    } else {
088                            NumberAxis axis = new NumberAxis();
089                            plot.setDomainAxis(axis);
090                    }
091    
092                    if (getConfig().isLogScaleRange()) {
093                            try {
094                                    NumberAxis axis = new LogarithmicAxis(null);
095                                    plot.setRangeAxis(axis);
096                            } catch (Exception e) {
097                                    NumberAxis axis = new NumberAxis();
098                                    plot.setRangeAxis(axis);
099                            }
100                    } else {
101                            NumberAxis axis = new NumberAxis();
102                            plot.setRangeAxis(axis);
103                    }
104    
105                    getChart().setTitle((String) null);
106    
107                    getChart().setBackgroundPaint(Color.WHITE);
108    
109                    plot.setDomainGridlinesVisible(false);
110                    plot.setRangeGridlinesVisible(false);
111    
112                    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
113    
114                    renderer.setBaseShapesVisible(false);
115                    renderer.setDrawSeriesLineAsPath(true);
116                    for (int i = 0; i < getMatrix().getColumnCount(); i++) {
117                            renderer.setSeriesStroke(i, new BasicStroke(3));
118                            plot.setRenderer(i, renderer);
119                    }
120    
121                    plot.setBackgroundPaint(Color.white);
122                    plot.setDomainGridlinePaint(Color.white);
123                    plot.setRangeGridlinePaint(Color.white);
124                    plot.setDomainCrosshairVisible(false);
125                    plot.setRangeCrosshairVisible(false);
126    
127                    plot.getRangeAxis().setAutoRange(true);
128                    plot.getDomainAxis().setAutoRange(true);
129                    plot.getDomainAxis().setUpperMargin(0);
130    
131                    setMouseZoomable(false);
132            }
133    
134    }