Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / graphic / GraphicChartPanel.java @ 12898

History | View | Annotate | Download (4.92 KB)

1 10741 nacho
/* 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 11004 bsanchez
import java.awt.BorderLayout;
23 10741 nacho
import java.awt.Color;
24
25
import javax.swing.JPanel;
26
27
import org.jfree.chart.ChartFactory;
28
import org.jfree.chart.ChartPanel;
29
import org.jfree.chart.JFreeChart;
30
import org.jfree.chart.plot.Plot;
31
import org.jfree.chart.plot.PlotOrientation;
32
import org.jfree.chart.plot.XYPlot;
33
import org.jfree.data.xy.XYSeries;
34
import org.jfree.data.xy.XYSeriesCollection;
35
/**
36 11070 bsanchez
 * Componente gr?fico de JFree
37
 *
38
 * @version 05/04/2007
39
 * @author Nacho Brodin (brodin_ign@gva.es)
40 12368 bsanchez
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
41 11070 bsanchez
 *
42
 */public class GraphicChartPanel extends JPanel {
43 10885 bsanchez
        private static final long serialVersionUID = 7328137487119964665L;
44 10741 nacho
        private JFreeChart                         chart;
45
        private ChartPanel                         jPanelChart = null;
46
        private XYSeries                         series[] = null;
47
        private XYSeriesCollection         dataset = null;
48
49 11004 bsanchez
        public GraphicChartPanel() {
50 10741 nacho
                dataset = new XYSeriesCollection();
51 11070 bsanchez
52
                createChart();
53 12368 bsanchez
54 10741 nacho
                initialize();
55 12368 bsanchez
56 10741 nacho
                Plot plot = this.jPanelChart.getChart().getPlot();
57 11033 bsanchez
                plot.setOutlineStroke(new BasicStroke(1));
58 11070 bsanchez
                plot.setOutlinePaint(Color.black);
59
60
                chart.setBackgroundPaint(Color.white);
61
                plot.setBackgroundPaint(new Color(245, 245, 245));
62 10741 nacho
        }
63 12368 bsanchez
64 10741 nacho
        /**
65 12368 bsanchez
         * This method initializes this
66
         *
67 10741 nacho
         */
68
        private void initialize() {
69 11004 bsanchez
                this.setLayout(new BorderLayout());
70 12368 bsanchez
                this.add(getChart(), BorderLayout.CENTER);
71 10741 nacho
        }
72
73
        /**
74 12368 bsanchez
         *
75 10741 nacho
         * @return
76
         */
77
        public ChartPanel getChart(){
78
                if(jPanelChart == null){
79
                        jPanelChart = new ChartPanel(chart);
80
                        jPanelChart.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,1));
81
                }
82
                return         jPanelChart;
83
        }
84
85 12368 bsanchez
                 /**
86
                 * Creates a chart.
87
                 *
88
                 * @param dataset  the dataset.
89
                 *
90
                 * @return A chart.
91
                 */
92
                private void createChart() {
93
94
                        chart =
95
                                 ChartFactory.createXYLineChart(null, null, null,
96
                                        dataset,
97
                                        PlotOrientation.VERTICAL,
98
                                                false,
99
                                                true,
100
                                                true);
101
102
                                // color
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
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
                        dataset.removeAllSeries();
122
                        series = new XYSeries[values.length];
123
                        for (int iGraf = 0; iGraf < values.length; iGraf++) {
124
                                series[iGraf] = new XYSeries(names[iGraf]);
125
                                for (int i = 0; i < values[iGraf].length; i++) {
126
                                        series[iGraf].add(new Integer(i),  new Integer(values[iGraf][i]));
127
                                }
128
                                dataset.addSeries(series[iGraf]);
129
                        }
130
                        jPanelChart.repaint();
131
                }
132
133
                /**
134
                 * Asigna nuevos valores para la gr?fica
135
                 * @param values Matriz de [n?mero de gr?ficas][Valores por gr?fica]
136
                 */
137
                public void setNewChart(double[][][] values, String names[]){
138
                        dataset.removeAllSeries();
139
                        series = new XYSeries[values.length];
140
                        for (int iGraf = 0; iGraf < values.length; iGraf++) {
141
                                series[iGraf] = new XYSeries(names[iGraf]);
142
                                for (int i = 0; i < values[iGraf].length; i++) {
143
                                        series[iGraf].add(values[iGraf][i][0],  values[iGraf][i][1]);
144
                                }
145
                                dataset.addSeries(series[iGraf]);
146
                        }
147
                        jPanelChart.repaint();
148
                }
149
150
                /**
151
                 * Reemplaza los valores de la gr?fica
152
                 * @param values Matriz de [n?mero de gr?ficas][Valores por gr?fica]
153
                 */
154
                public void replaceValuesChart(int[][] values){
155
                        for (int iGraf = 0; iGraf < values.length; iGraf++) {
156
                                for (int i = 0; i < values[iGraf].length; i++) {
157
                                        series[iGraf].getDataItem(i).setY(values[iGraf][i]);
158
                                }
159
                        }
160
                        jPanelChart.repaint();
161
                }
162
163
                /**
164
                 * Limpia la gr?fica
165
                 */
166
                public void cleanChart(){
167
                        dataset.removeAllSeries();
168
                        jPanelChart.repaint();
169
                }
170 11004 bsanchez
}