Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libUIComponent / src / org / gvsig / gui / beans / graphic / GraphicChartPanel.java @ 22758

History | View | Annotate | Download (6.21 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.gui.beans.graphic;
20

    
21
import java.awt.BasicStroke;
22
import java.awt.BorderLayout;
23
import java.awt.Color;
24
import java.awt.Image;
25

    
26
import javax.swing.BorderFactory;
27
import javax.swing.ImageIcon;
28
import javax.swing.JPanel;
29

    
30
import org.jfree.chart.ChartFactory;
31
import org.jfree.chart.ChartPanel;
32
import org.jfree.chart.JFreeChart;
33
import org.jfree.chart.plot.Plot;
34
import org.jfree.chart.plot.PlotOrientation;
35
import org.jfree.chart.plot.XYPlot;
36
import org.jfree.data.xy.XYSeries;
37
import org.jfree.data.xy.XYSeriesCollection;
38
/**
39
 * Componente gr?fico de JFree
40
 * 
41
 * @version 05/04/2007
42
 * @author Nacho Brodin (brodin_ign@gva.es)
43
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
44
 */
45
public class GraphicChartPanel extends JPanel {
46
        private static final long serialVersionUID = 7328137487119964665L;
47
        private JFreeChart         chart       = null;
48
        private ChartPanel         jPanelChart = null;
49
        private XYSeries           series[]    = new XYSeries[0];
50
        private XYSeriesCollection dataset     = null;
51

    
52
        private int                viewType    = 0;
53

    
54
        public GraphicChartPanel() {
55
                dataset = new XYSeriesCollection();
56

    
57
                createChart();
58

    
59
                initialize();
60

    
61
                Plot plot = this.jPanelChart.getChart().getPlot();
62
                plot.setOutlineStroke(new BasicStroke(1));
63
                plot.setOutlinePaint(Color.black);
64

    
65
                chart.setBackgroundPaint(Color.white);
66
                plot.setBackgroundPaint(new Color(245, 245, 245));
67
        }
68

    
69
        /**
70
         * This method initializes this
71
         */
72
        private void initialize() {
73
                this.setLayout(new BorderLayout());
74
                this.add(getChart(), BorderLayout.CENTER);
75
        }
76

    
77
        /**
78
         * @return
79
         */
80
        public ChartPanel getChart() {
81
                if (jPanelChart == null) {
82
                        jPanelChart = new ChartPanel(chart);
83
                        jPanelChart.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
84
                }
85
                return jPanelChart;
86
        }
87

    
88
        /**
89
         * Creates a chart.
90
         * @param dataset the dataset.
91
         * @return A chart.
92
         */
93
        private void createChart() {
94
                chart = ChartFactory.createXYLineChart(null, null, null, dataset,
95
                                                        PlotOrientation.VERTICAL, false, true, true);
96

    
97
                // Definir la lista de colores
98
                XYPlot plot = chart.getXYPlot();
99
                plot.getRenderer().setSeriesPaint(0, Color.red);
100
                plot.getRenderer().setSeriesPaint(1, Color.green);
101
                plot.getRenderer().setSeriesPaint(2, Color.blue);
102
                plot.getRenderer().setSeriesPaint(3, Color.cyan);
103
                plot.getRenderer().setSeriesPaint(4, Color.black);
104
                plot.getRenderer().setSeriesPaint(5, Color.darkGray);
105
                plot.getRenderer().setSeriesPaint(6, Color.gray);
106
                plot.getRenderer().setSeriesPaint(7, Color.magenta);
107
                plot.getRenderer().setSeriesPaint(8, Color.yellow);
108
                plot.getRenderer().setSeriesPaint(9, Color.orange);
109

    
110
                Image img = new ImageIcon(getClass().getResource("images/splash.png")).getImage();
111
                plot.setBackgroundPaint(null);
112
                plot.setBackgroundImageAlpha(0.18f);
113
                plot.setBackgroundImage(img);
114
        }
115

    
116
        /**
117
         * Asigna nuevos valores para la gr?fica
118
         * @param values Matriz de [n?mero de gr?ficas][Valores por gr?fica]
119
         */
120
        public void setNewChart(int[][] values, String names[]) {
121
                series = new XYSeries[values.length];
122
                for (int iGraf = 0; iGraf < values.length; iGraf++) {
123
                        series[iGraf] = new XYSeries(names[iGraf]);
124
                        for (int i = 0; i < values[iGraf].length; i++) {
125
                                series[iGraf].add(new Integer(i), new Integer(values[iGraf][i]));
126
                        }
127
                }
128
                reloadGraphic();
129
        }
130

    
131
        /**
132
         * Asigna nuevos valores para la gr?fica
133
         * @param values Matriz de [n?mero de gr?ficas][Valores por gr?fica]
134
         */
135
        public void setNewChart(double[][][] values, String names[]) {
136
                series = new XYSeries[values.length];
137
                for (int iGraf = 0; iGraf < values.length; iGraf++) {
138
                        series[iGraf] = new XYSeries(names[iGraf]);
139
                        for (int i = 0; i < values[iGraf].length; i++) {
140
                                series[iGraf].add(values[iGraf][i][0], values[iGraf][i][1]);
141
                        }
142
                }
143
                reloadGraphic();
144
        }
145

    
146
        /**
147
         * Define el tipo de visualizacion para la grafica.
148
         * Valores: 0 - Normal, 1 - Acumulado, 2 - Logaritmico
149
         * @param type
150
         */
151
        public void setViewType(int type) {
152
                this.viewType = type;
153
                reloadGraphic();
154
        }
155

    
156
        /**
157
         * Recarga los datos de la grafica dependiendo del tipo de visualizacion
158
         */
159
        private void reloadGraphic() {
160
                dataset.removeAllSeries();
161
                switch (viewType) {
162
                        case 0: // Normal
163
                                for (int i = 0; i < series.length; i++)
164
                                        dataset.addSeries(series[i]);
165
                                break;
166
                        case 1: // Acumulado
167
                                XYSeries[] seriesAcum = new XYSeries[series.length];
168
                                for (int i = 0; i < series.length; i++) {
169
                                        seriesAcum[i] = new XYSeries(series[i].getKey());
170
                                        double total = 0;
171
                                        for (int j = 0; j < series[i].getItemCount(); j++) {
172
                                                total += series[i].getY(j).doubleValue();
173
                                                seriesAcum[i].add(series[i].getX(j), total);
174
                                        }
175
                                        dataset.addSeries(seriesAcum[i]);
176
                                }
177
                                break;
178
                        case 2: // Logaritmico
179
                                XYSeries[] seriesLog = new XYSeries[series.length];
180

    
181
                                double minim = Double.MAX_VALUE;
182
                                for (int i = 0; i < series.length; i++)
183
                                        for (int j = 0; j < series[i].getItemCount(); j++)
184
                                                if (minim > series[i].getY(j).doubleValue())
185
                                                        minim = series[i].getY(j).doubleValue();
186
                                
187
                                for (int i = 0; i < series.length; i++) {
188
                                        seriesLog[i] = new XYSeries(series[i].getKey());
189
                                        for (int j = 0; j < series[i].getItemCount(); j++)
190
                                                seriesLog[i].add(series[i].getX(j), java.lang.Math.log(series[i].getY(j).doubleValue() - minim + 1.0));
191
                                        dataset.addSeries(seriesLog[i]);
192
                                }
193
                                break;
194
                }
195
                jPanelChart.repaint();
196
        }
197

    
198
        /**
199
         * Limpia la gr?fica
200
         */
201
        public void cleanChart() {
202
                series = new XYSeries[0];
203
                reloadGraphic();
204
        }
205
}