Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / graphic / GraphicChartPanel.java @ 40561

History | View | Annotate | Download (6.31 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.gui.beans.graphic;
25

    
26
import java.awt.BasicStroke;
27
import java.awt.BorderLayout;
28
import java.awt.Color;
29
import java.awt.Image;
30

    
31
import javax.swing.BorderFactory;
32
import javax.swing.ImageIcon;
33
import javax.swing.JPanel;
34

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

    
57
        private int                viewType    = 0;
58

    
59
        public GraphicChartPanel() {
60
                dataset = new XYSeriesCollection();
61

    
62
                createChart();
63

    
64
                initialize();
65

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

    
70
                chart.setBackgroundPaint(Color.white);
71
                plot.setBackgroundPaint(new Color(245, 245, 245));
72
        }
73

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

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

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

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

    
115
                Image img = new ImageIcon(getClass().getResource("images/splash.png")).getImage();
116
                plot.setBackgroundPaint(null);
117
                plot.setBackgroundImageAlpha(0.18f);
118
                plot.setBackgroundImage(img);
119
        }
120

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

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

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

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

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

    
203
        /**
204
         * Limpia la gr?fica
205
         */
206
        public void cleanChart() {
207
                series = new XYSeries[0];
208
                reloadGraphic();
209
        }
210
}