Statistics
| Revision:

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

History | View | Annotate | Download (4.66 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
 * 
38
 * Nacho Brodin (brodin_ign@gva.es)
39
 */
40

    
41
public class GraphicChartPanel extends JPanel {
42
        private static final long serialVersionUID = 7328137487119964665L;
43
        private JFreeChart                         chart;
44
        private ChartPanel                         jPanelChart = null;
45
        private XYSeries                         series[] = null;
46
        private XYSeriesCollection         dataset = null;
47

    
48
        public GraphicChartPanel() {
49
                int nSeries = 3;
50
                
51
                series = new XYSeries[nSeries];
52
                dataset = new XYSeriesCollection();
53
                
54
                for(int iSerie = 0; iSerie < nSeries; iSerie++){
55
                        series[iSerie] = new XYSeries("");
56
                        for (int i = 0; i < 256; i++) 
57
                         series[iSerie].add(new Integer(i),  new Integer(i * (iSerie + 1)));
58
                        dataset.addSeries(series[iSerie]);
59
                }
60
                            
61
            createChart();
62
                        
63
                initialize();
64
                
65
                Plot plot = this.jPanelChart.getChart().getPlot();
66
                plot.setOutlineStroke(new BasicStroke(1));
67
                plot.setOutlinePaint(Color.magenta);
68
        }
69
        
70
        /**
71
         * This method initializes this                        
72
         * 
73
         */
74
        private void initialize() {
75
                this.setLayout(new BorderLayout());
76
                this.add(getChart(), BorderLayout.CENTER);        
77
        }
78

    
79
        /**
80
         * 
81
         * @return
82
         */
83
        public ChartPanel getChart(){
84
                if(jPanelChart == null){
85
                        jPanelChart = new ChartPanel(chart);
86
                        jPanelChart.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,1));
87
                }
88
                return         jPanelChart;
89
        }
90

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