Statistics
| Revision:

root / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / scatterplot / gui / ChartScaterPlotPanel.java @ 17596

History | View | Annotate | Download (8.62 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2007 Instituto de Desarrollo Regional 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
* For more information, contact:
20
*
21
*  Generalitat Valenciana
22
*   Conselleria d'Infraestructures i Transport
23
*   Av. Blasco Ib??ez, 50
24
*   46010 VALENCIA
25
*   SPAIN
26
*
27
*      +34 963862235
28
*   gvsig@gva.es
29
*      www.gvsig.gva.es
30
*
31
*    or
32
*
33
*   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
*   Campus Universitario s/n
35
*   02071 Alabacete
36
*   Spain
37
*
38
*   +34 967 599 200
39
*/
40

    
41
package org.gvsig.remotesensing.scatterplot.gui;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Color;
45

    
46
import javax.swing.JPanel;
47

    
48
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
49
import org.gvsig.raster.buffer.BufferFactory;
50
import org.gvsig.raster.buffer.RasterBufferInvalidException;
51
import org.gvsig.raster.dataset.IBuffer;
52
import org.gvsig.raster.grid.Grid;
53
import org.gvsig.remotesensing.scatterplot.chart.ScatterPlotChart;
54
import org.gvsig.remotesensing.scatterplot.chart.ScatterPlotDiagram;
55
import org.jfree.chart.JFreeChart;
56
import org.jfree.chart.axis.NumberAxis;
57

    
58
import com.iver.andami.PluginServices;
59
import com.iver.cit.gvsig.fmap.layers.FLayer;
60

    
61
/**
62
 * Clase que define el panel donde aparece el grafico de dispersion para dos bandas, determinadas por
63
 * las variables bandaX y bandaY. El grafico es un FastScatterPlot de la libreria jfreeChart (personalizada para este
64
 * apartado). 
65
 * 
66
 * @author Alejandro Mu?oz Sanchez (alejandro.munoz@uclm.es)  
67
 * @version 11/12/2007
68
 */
69

    
70
public class ChartScaterPlotPanel extends JPanel {
71

    
72
        private static final long                 serialVersionUID = 1L;
73
        private JFreeChart                                 chart;
74
        private ScatterPlotDiagram                                 jPanelChart         = null;
75
        private  ScatterPlotChart                plot                         = null;
76
        private int                                         bandaX                        = 0;
77
        private int                                         bandaY                        = 1;
78
        private Grid                                         grid                        = null;
79
        private float                                         data[][]                = null;
80
        FLayer                                                         fLayer                        = null;
81
        
82
        
83
        /**
84
         *   Constructor
85
         *   @param flayer capa 
86
         *   @param band1        banda que se representa en el eje x.
87
         *   @param band2        banda que se representa en el eje y.
88
         * */
89
        public ChartScaterPlotPanel(FLayer fLayer, int band1, int band2){
90
                this.fLayer= fLayer;
91
                bandaX= band1;
92
                bandaY= band2;
93
                createChart();
94
                initialize();
95
        }
96
        
97
        
98
        /**
99
         *  Inicializacion del panel que contiene el grafico
100
         * */
101
        private void initialize() {
102
                this.setLayout(new BorderLayout());
103
                this.add(getChart(), BorderLayout.CENTER);
104
        }
105

    
106
        
107
        /**
108
         *         @return grafico actual
109
         * */
110
        public ScatterPlotDiagram getChart(){
111
                if(jPanelChart == null){
112
                        jPanelChart = new ScatterPlotDiagram(chart);
113
                        chart=null;
114
                        jPanelChart.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,1));
115
                }
116
                return         jPanelChart;
117
        }
118

    
119

    
120

    
121
        /**
122
         *         Metodo que construye el grafico inicialmente. Antes de construirlo, llama a
123
         *         setDataChart() para cargar los datos a representar 
124
         *         
125
         * */
126
        private void createChart() {
127
                
128
                 NumberAxis domainAxis = new NumberAxis(PluginServices.getText(this,"banda")+" "+bandaX);
129
             domainAxis.setAutoRangeIncludesZero(false);
130
             NumberAxis rangeAxis = new NumberAxis(PluginServices.getText(this,"banda")+" "+bandaY);
131
             rangeAxis.setAutoRangeIncludesZero(false);
132
                         
133
             // Se cargan los datos antes de construir el grafico
134
             setDataChart(fLayer);
135
             plot = new ScatterPlotChart(this.data, domainAxis, rangeAxis);
136
             chart = new JFreeChart(PluginServices.getText(this,"diagrama_dispersion"), plot);
137
             data= null;
138
                 chart.getRenderingHints().clear();
139
                 
140
                // Image img = new ImageIcon(ScatterPlotPanel.class.getClassLoader().getResource("images/splash.png")).getImage();
141
                 plot.setBackgroundPaint(null);
142
                 plot.setBackgroundImageAlpha(0.18f);
143
        //         plot.setBackgroundImage(img);
144
                 chart.setBackgroundPaint(Color.white);
145

    
146
                 plot.setBackgroundPaint(new Color(245, 245, 245));
147
                 
148
        }
149

    
150
        
151
        /**
152
         *         Actualizacion del panel que contiene el grafico
153
         * */
154
        public void updateChartPanel(){
155
                jPanelChart.setChart(chart);
156
                // hace un clear cuando se cambian las bandas
157
                jPanelChart.getROIChartList().getListRois().clear();
158
                jPanelChart.repaint();
159
                updateUI();
160
                
161
        }
162
        
163
        
164
        /**
165
         *         Actualizacion del grafico. Esta actualizacion se producira cuando haya variacion en la
166
         *  seleccion de las bandas por parte del usuario.
167
         *  
168
         * */
169
        public void updateChart() {
170
                
171
                 NumberAxis domainAxis = new NumberAxis(PluginServices.getText(this,"banda")+bandaX);
172
             domainAxis.setAutoRangeIncludesZero(false);
173
             NumberAxis rangeAxis = new NumberAxis(PluginServices.getText(this,"banda")+bandaY);
174
             rangeAxis.setAutoRangeIncludesZero(false);
175
             chart=null;
176
             plot=null; 
177
             
178
             // Se cargan los datos correspondiesntes a las bandas seleccionadas
179
             setDataChart(fLayer);
180
             plot = new ScatterPlotChart(this.data, domainAxis, rangeAxis);
181
             
182
                 chart = new JFreeChart(PluginServices.getText(this,"diagrama_dispersion"), plot);
183
                 chart.getRenderingHints().clear();
184
                 data=null;
185
                 
186
/*                 Image img = new ImageIcon(ScatterPlotPanel.class.getClassLoader().getResource("images/splash.png")).getImage();
187
                 //plot.setBackgroundPaint(null);
188
*/                 plot.setBackgroundImageAlpha(0.18f);
189
                 //plot.setBackgroundImage(img);
190
                 chart.setBackgroundPaint(Color.white);
191
                 
192
                 plot.setBackgroundPaint(new Color(245, 245, 245));
193
                 //chart.fireChartChanged();
194
                 plot=null;
195
                 updateChartPanel();
196
            
197
                }
198

    
199
                
200
                
201
        /**
202
         *         Metodo que establece los datos a representar.  
203
         *         El vector data[][] se rellena con los valores de cada punto para las bandas
204
         *  bandX y bandY.
205
         * */
206
        public void setDataChart(FLayer fLayer){
207
                                
208
                this.fLayer = fLayer;
209
                FLyrRasterSE rasterLayer = (FLyrRasterSE) fLayer;
210

    
211
                BufferFactory dataSource = rasterLayer.getBufferFactory();
212

    
213
                int bands[] = null;
214
                bands = new int[rasterLayer.getBandCount()];
215
                for (int i = 0; i < rasterLayer.getBandCount(); i++)
216
                        bands[i] = i;
217
                try {
218
                        grid = new Grid(dataSource, new int[]{bandaX, bandaY});
219
                } catch (RasterBufferInvalidException e) {
220
                                e.printStackTrace();
221
                }
222
                        
223

    
224
                int width= grid.getRasterBuf().getWidth();
225
                int height= grid.getRasterBuf().getHeight();
226
                int indice=0;
227
                        
228
                data= new float[2][width*height];
229
                        
230
                if (grid.getDataType()== IBuffer.TYPE_BYTE){
231
                        for(int i=0; i<width;i++){
232
                                for(int j=0; j<height; j++){
233
                                        data[0][indice]= grid.getRasterBuf().getElemByte(j,i,0)&0x0ff;
234
                                        data[1][indice]= grid.getRasterBuf().getElemByte(j,i,1)&0x0ff;
235
                                        indice++;
236
                                }        
237
                        }
238
                }
239
                        
240
                if (grid.getDataType()== IBuffer.TYPE_SHORT){
241
                                
242
                        for(int i=0; i<width;i++){
243
                                for(int j=0; j<height; j++){
244
                                        data[0][indice]= grid.getRasterBuf().getElemShort(j,i,0);
245
                                        data[1][indice]= grid.getRasterBuf().getElemShort(j,i,1);
246
                                        indice++;
247
                                }
248
                        }
249
                }
250
                        
251
                if (grid.getDataType()== IBuffer.TYPE_INT){
252
                                
253
                        for(int i=0; i<width;i++){
254
                                for(int j=0; j<height; j++){
255
                                        data[0][indice]= grid.getRasterBuf().getElemInt(j,i,bandaX);
256
                                        data[1][indice]= grid.getRasterBuf().getElemInt(j,i,bandaY);
257
                                        indice++;
258
                                }        
259
                        }
260
                }
261
                
262
                if (grid.getDataType()== IBuffer.TYPE_FLOAT){
263
                        for(int i=0; i<width;i++){
264
                                for(int j=0; j<height; j++){
265
                                        data[0][indice]= grid.getRasterBuf().getElemFloat(j,i,bandaX);
266
                                        data[1][indice]= grid.getRasterBuf().getElemFloat(j,i,bandaY);
267
                                        indice++;
268
                                }
269
                        }
270
                }
271
                // OJO AL CASTINTG DE DOUBLE A FLOAT
272
                if (grid.getDataType()== IBuffer.TYPE_DOUBLE){
273
                        for(int i=0; i<width;i++){
274
                                for(int j=0; j<height; j++){
275
                                        data[0][indice]= (float)grid.getRasterBuf().getElemDouble(j,i,bandaX);
276
                                        data[1][indice]= (float)grid.getRasterBuf().getElemDouble(j,i,bandaY);
277
                                        indice++;
278
                                }
279
                        }
280
                }
281
                
282
        } 
283
                
284
                
285
        /**
286
         * Asignacion de la banda X
287
         * */
288
        public void setBandX(int band){
289
                bandaX= band;
290
        }
291
                
292
                
293
        /**
294
         * Asignacion de la banda Y
295
         * */
296
        public void setBandY(int band){
297
                bandaY= band;
298
        }
299
                
300
        
301
        /**
302
         * @return banda X
303
         * */
304
        public int getBandX(){
305
                return bandaX;
306
        }
307
                
308
        
309
        /**
310
         * @return banda Y
311
         * */
312
        public int getBandY(){
313
                return bandaY;
314
        }
315
        
316
        
317
        /**
318
         *  @return grid asociado
319
         * 
320
         * */
321
        public Grid getGrid(){
322
                return grid;
323
        }
324
} 
325

    
326