Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.library / org.gvsig.raster / org.gvsig.raster.swing / org.gvsig.raster.swing.buffer / org.gvsig.raster.swing.buffer.impl / src / main / java / org / gvsig / raster / swing / buffer / impl / histogram / graphic / GraphicChartPanel.java @ 43805

History | View | Annotate | Download (7.19 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.raster.swing.buffer.impl.histogram.graphic;
25

    
26
import java.awt.BasicStroke;
27
import java.awt.BorderLayout;
28
import java.awt.Color;
29
import java.awt.Image;
30
import java.net.URL;
31
import java.util.List;
32

    
33
import javax.swing.BorderFactory;
34
import javax.swing.ImageIcon;
35
import javax.swing.JPanel;
36

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

    
59
        private int                viewType    = 0;
60

    
61
        public GraphicChartPanel() {
62
                dataset = new XYSeriesCollection();
63

    
64
                createChart();
65

    
66
                initialize();
67

    
68
                Plot plot = this.jPanelChart.getChart().getPlot();
69
                plot.setOutlineStroke(new BasicStroke(1));
70
                plot.setOutlinePaint(Color.black);
71

    
72
                chart.setBackgroundPaint(Color.white);
73
                plot.setBackgroundPaint(new Color(245, 245, 245));
74
        }
75

    
76
        /**
77
         * This method initializes this
78
         */
79
        private void initialize() {
80
                this.setLayout(new BorderLayout());
81
                this.add(getChart(), BorderLayout.CENTER);
82
        }
83

    
84
        /**
85
         * @return
86
         */
87
        public ChartPanel getChart() {
88
                if (jPanelChart == null) {
89
                        jPanelChart = new ChartPanel(chart);
90
                        jPanelChart.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
91
                }
92
                return jPanelChart;
93
        }
94

    
95
        /**
96
         * Creates a chart.
97
         * @param dataset the dataset.
98
         * @return A chart.
99
         */
100
        private void createChart() {
101
                chart = ChartFactory.createXYLineChart(null, null, null, dataset,
102
                                                        PlotOrientation.VERTICAL, false, true, true);
103

    
104
                // Definir la lista de colores
105
                XYPlot plot = chart.getXYPlot();
106
                plot.getRenderer().setSeriesPaint(0, Color.red);
107
                plot.getRenderer().setSeriesPaint(1, Color.green);
108
                plot.getRenderer().setSeriesPaint(2, Color.blue);
109
                plot.getRenderer().setSeriesPaint(3, Color.cyan);
110
                plot.getRenderer().setSeriesPaint(4, Color.black);
111
                plot.getRenderer().setSeriesPaint(5, Color.darkGray);
112
                plot.getRenderer().setSeriesPaint(6, Color.gray);
113
                plot.getRenderer().setSeriesPaint(7, Color.magenta);
114
                plot.getRenderer().setSeriesPaint(8, Color.yellow);
115
                plot.getRenderer().setSeriesPaint(9, Color.orange);
116

    
117
        URL resourceImageIcon = getClass().getResource("splash.png");
118
        if (resourceImageIcon != null) {
119
            Image img = new ImageIcon(resourceImageIcon).getImage();
120
            if (img != null) {
121
                plot.setBackgroundPaint(null);
122
                plot.setBackgroundImageAlpha(0.18f);
123
                plot.setBackgroundImage(img);
124
            }
125
        }
126
        }
127

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

    
143
        /**
144
         * Asigna nuevos valores para la gr?fica
145
         * @param values Matriz de [n?mero de gr?ficas][Valores por gr?fica]
146
         */
147
        public void setNewChart(double[][][] values, String names[]) {
148
                series = new XYSeries[values.length];
149
                for (int iGraf = 0; iGraf < values.length; iGraf++) {
150
                        series[iGraf] = new XYSeries(names[iGraf]);
151
                        for (int i = 0; i < values[iGraf].length; i++) {
152
                                series[iGraf].add(values[iGraf][i][0], values[iGraf][i][1]);
153
                        }
154
                }
155
                reloadGraphic();
156
        }
157

    
158
        public void setNewChart(List<List<double[]>> values, String[] names) {
159
        series = new XYSeries[values.size()];
160
        for (int iGraf = 0; iGraf < values.size(); iGraf++) {
161
            series[iGraf] = new XYSeries(names[iGraf]);
162
            for (int i = 0; i < values.get(iGraf).size(); i++) {
163
                series[iGraf].add(values.get(iGraf).get(i)[0], values.get(iGraf).get(i)[1]);
164
            }
165
        }
166
        reloadGraphic();
167
    }
168

    
169
           public void setNewChart(XYSeries[] values) {
170
                series = new XYSeries[values.length];
171
                System.arraycopy(values, 0, series, 0, values.length);
172
                reloadGraphic();
173
            }
174

    
175
        /**
176
         * Define el tipo de visualizacion para la grafica.
177
         * Valores: 0 - Normal, 1 - Acumulado, 2 - Logaritmico
178
         * @param type
179
         */
180
        public void setViewType(int type) {
181
                this.viewType = type;
182
                reloadGraphic();
183
        }
184

    
185
        /**
186
         * Recarga los datos de la grafica dependiendo del tipo de visualizacion
187
         */
188
        private void reloadGraphic() {
189
                dataset.removeAllSeries();
190
                switch (viewType) {
191
                        case 0: // Normal
192
                                for (int i = 0; i < series.length; i++)
193
                                        dataset.addSeries(series[i]);
194
                                break;
195
                        case 1: // Acumulado
196
                                XYSeries[] seriesAcum = new XYSeries[series.length];
197
                                for (int i = 0; i < series.length; i++) {
198
                                        seriesAcum[i] = new XYSeries(series[i].getKey());
199
                                        double total = 0;
200
                                        for (int j = 0; j < series[i].getItemCount(); j++) {
201
                                                total += series[i].getY(j).doubleValue();
202
                                                seriesAcum[i].add(series[i].getX(j), total);
203
                                        }
204
                                        dataset.addSeries(seriesAcum[i]);
205
                                }
206
                                break;
207
                        case 2: // Logaritmico
208
                                XYSeries[] seriesLog = new XYSeries[series.length];
209

    
210
                                double minim = Double.MAX_VALUE;
211
                                for (int i = 0; i < series.length; i++)
212
                                        for (int j = 0; j < series[i].getItemCount(); j++)
213
                                                if (minim > series[i].getY(j).doubleValue())
214
                                                        minim = series[i].getY(j).doubleValue();
215

    
216
                                for (int i = 0; i < series.length; i++) {
217
                                        seriesLog[i] = new XYSeries(series[i].getKey());
218
                                        for (int j = 0; j < series[i].getItemCount(); j++)
219
                                                seriesLog[i].add(series[i].getX(j), java.lang.Math.log(series[i].getY(j).doubleValue() - minim + 1.0));
220
                                        dataset.addSeries(seriesLog[i]);
221
                                }
222
                                break;
223
                }
224
                jPanelChart.repaint();
225
        }
226

    
227
        /**
228
         * Limpia la gr?fica
229
         */
230
        public void cleanChart() {
231
                series = new XYSeries[0];
232
                reloadGraphic();
233
        }
234

    
235
}