Statistics
| Revision:

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

History | View | Annotate | Download (5.15 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

    
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
/**
37
 * Componente gr?fico de JFree
38
 *
39
 * @version 05/04/2007
40
 * @author Nacho Brodin (brodin_ign@gva.es)
41
 * @author Borja S?nchez Zamorano (borja.sanchez@iver.es)
42
 *
43
 */public class GraphicChartPanel extends JPanel {
44
        private static final long serialVersionUID = 7328137487119964665L;
45
        private JFreeChart                         chart;
46
        private ChartPanel                         jPanelChart = null;
47
        private XYSeries                         series[] = null;
48
        private XYSeriesCollection         dataset = null;
49

    
50
        public GraphicChartPanel() {
51
                dataset = new XYSeriesCollection();
52

    
53
                createChart();
54
                        
55
                initialize();
56
                
57
                Plot plot = this.jPanelChart.getChart().getPlot();
58
                plot.setOutlineStroke(new BasicStroke(1));
59
                plot.setOutlinePaint(Color.black);
60

    
61
                chart.setBackgroundPaint(Color.white);
62
                plot.setBackgroundPaint(new Color(245, 245, 245));
63
        }
64
        
65
        /**
66
         * This method initializes this                        
67
         * 
68
         */
69
        private void initialize() {
70
                this.setLayout(new BorderLayout());
71
                this.add(getChart(), BorderLayout.CENTER);        
72
        }
73

    
74
        /**
75
         * 
76
         * @return
77
         */
78
        public ChartPanel getChart(){
79
                if(jPanelChart == null){
80
                        jPanelChart = new ChartPanel(chart);
81
                        jPanelChart.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,1));
82
                }
83
                return         jPanelChart;
84
        }
85

    
86
           /**
87
     * Creates a chart.
88
     * 
89
     * @param dataset  the dataset.
90
     * 
91
     * @return A chart.
92
     */
93
    private void createChart() {
94
            
95
            chart = 
96
                     ChartFactory.createXYLineChart(null, null, null, 
97
                            dataset, 
98
                            PlotOrientation.VERTICAL,
99
                        false,
100
                        true, 
101
                        true);
102
        
103
        // color
104
        XYPlot plot = chart.getXYPlot();
105
        plot.getRenderer().setSeriesPaint(0, Color.red);
106
        plot.getRenderer().setSeriesPaint(1, Color.green);
107
        plot.getRenderer().setSeriesPaint(2, Color.blue);
108
        plot.getRenderer().setSeriesPaint(3, Color.cyan);
109
        plot.getRenderer().setSeriesPaint(4, Color.black);
110
        plot.getRenderer().setSeriesPaint(5, Color.darkGray);
111
        plot.getRenderer().setSeriesPaint(6, Color.gray);
112
        plot.getRenderer().setSeriesPaint(7, Color.magenta);
113
        plot.getRenderer().setSeriesPaint(8, Color.yellow);
114
        plot.getRenderer().setSeriesPaint(9, Color.orange);
115
    }
116
    
117
    /**
118
     * Asigna nuevos valores para la gr?fica
119
     * @param values Matriz de [n?mero de gr?ficas][Valores por gr?fica]
120
     */
121
    public void setNewChart(int[][] values, String names[]){
122
            dataset.removeAllSeries();
123
            series = new XYSeries[values.length];
124
            for (int iGraf = 0; iGraf < values.length; iGraf++) {
125
                    series[iGraf] = new XYSeries(names[iGraf]);
126
                    for (int i = 0; i < values[iGraf].length; i++) {
127
                            series[iGraf].add(new Integer(i),  new Integer(values[iGraf][i]));
128
                    }
129
                    dataset.addSeries(series[iGraf]);
130
            }
131
            jPanelChart.repaint();
132
    }
133
    
134
    /**
135
     * Asigna nuevos valores para la gr?fica
136
     * @param values Matriz de [n?mero de gr?ficas][Valores por gr?fica]
137
     */
138
    public void setNewChart(double[][][] values, String names[]){
139
            dataset.removeAllSeries();
140
            series = new XYSeries[values.length];
141
            for (int iGraf = 0; iGraf < values.length; iGraf++) {
142
                    series[iGraf] = new XYSeries(names[iGraf]);
143
                    for (int i = 0; i < values[iGraf].length; i++) {
144
                            series[iGraf].add(values[iGraf][i][0],  values[iGraf][i][1]);
145
                    }
146
                    dataset.addSeries(series[iGraf]);
147
            }
148
            jPanelChart.repaint();
149
    }
150
    
151
    /**
152
     * Reemplaza los valores de la gr?fica
153
     * @param values Matriz de [n?mero de gr?ficas][Valores por gr?fica]
154
     */
155
    public void replaceValuesChart(int[][] values){
156
            for (int iGraf = 0; iGraf < values.length; iGraf++) {
157
                    for (int i = 0; i < values[iGraf].length; i++) {
158
                            series[iGraf].getDataItem(i).setY(values[iGraf][i]);
159
                    }
160
            }
161
            jPanelChart.repaint();
162
    }
163
    
164
    /**
165
     * Limpia la gr?fica
166
     */
167
    public void cleanChart(){
168
            dataset.removeAllSeries();
169
            jPanelChart.repaint();
170
    }
171
}