Statistics
| Revision:

root / trunk / libraries / libUIComponent_praster / src / org / gvsig / gui / beans / graphic / GraphicChartPanel.java @ 8026

History | View | Annotate | Download (5.71 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.Color;
23
import java.awt.FlowLayout;
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
        
43
        private static final int        HEIGHT_MARGIN = 5;
44
        private static final int        WIDTH_MARGIN = 4;
45
        private JFreeChart                         chart;
46
        private ChartPanel                         jPanelChart = null;
47
        private XYSeries                         series[] = null;
48
        private XYSeriesCollection         dataset = null;
49
        private int                                 width = 300, height = 150;
50

    
51
        
52
        public GraphicChartPanel(int width, int height) {
53
                this.width = width;
54
                this.height = height;
55

    
56
                int nSeries = 3;
57
                series = new XYSeries[nSeries];
58
                dataset = new XYSeriesCollection();
59
                
60
                for(int iSerie = 0; iSerie < nSeries; iSerie++){
61
                        series[iSerie] = new XYSeries("");
62
                        for (int i = 0; i < 256; i++) 
63
                         series[iSerie].add(new Integer(i),  new Integer(i * (iSerie + 1)));
64
                        dataset.addSeries(series[iSerie]);
65
                }
66
                            
67
            createChart();
68
                        
69
                initialize();
70
                
71
                Plot plot = this.jPanelChart.getChart().getPlot();
72
        plot.setOutlineStroke(new BasicStroke(1));
73
        plot.setOutlinePaint(Color.magenta);
74
        }
75
        
76
        /**
77
         * This method initializes this                        
78
         * 
79
         */
80
        private void initialize() {
81
        FlowLayout flowLayout = new FlowLayout();
82
        flowLayout.setVgap(0);
83
        flowLayout.setHgap(0);
84
        this.setLayout(flowLayout);
85
        this.setSize(new java.awt.Dimension(width, height));
86
                this.setPreferredSize(new java.awt.Dimension(width, height));
87
                this.add(getChart(), null);        
88
        }
89

    
90
        /**
91
         * 
92
         * @return
93
         */
94
        public ChartPanel getChart(){
95
                if(jPanelChart == null){
96
                        jPanelChart = new ChartPanel(chart);
97
                        jPanelChart.setSize(new java.awt.Dimension(width - WIDTH_MARGIN, height - HEIGHT_MARGIN));
98
                        jPanelChart.setPreferredSize(new java.awt.Dimension(width - WIDTH_MARGIN, height - HEIGHT_MARGIN));
99
                        jPanelChart.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,1));
100
                }
101
                return         jPanelChart;
102
        }
103

    
104
           /**
105
     * Creates a chart.
106
     * 
107
     * @param dataset  the dataset.
108
     * 
109
     * @return A chart.
110
     */
111
    private void createChart() {
112
            
113
            chart = 
114
                     ChartFactory.createXYLineChart(null, null, null, 
115
                            dataset, 
116
                            PlotOrientation.VERTICAL,
117
                        false,
118
                        true, 
119
                        true);
120
        
121
        // color
122
        XYPlot plot = chart.getXYPlot();
123
        plot.getRenderer().setSeriesPaint(0, Color.red);
124
        plot.getRenderer().setSeriesPaint(1, Color.green); 
125
        plot.getRenderer().setSeriesPaint(2, Color.blue);
126
        plot.getRenderer().setSeriesPaint(3, Color.cyan);
127
        plot.getRenderer().setSeriesPaint(4, Color.black);
128
        plot.getRenderer().setSeriesPaint(5, Color.darkGray);
129
        plot.getRenderer().setSeriesPaint(6, Color.gray);
130
        plot.getRenderer().setSeriesPaint(7, Color.magenta);
131
        plot.getRenderer().setSeriesPaint(8, Color.yellow);
132
        plot.getRenderer().setSeriesPaint(9, Color.orange);
133
        
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(int[][] values, String names[]){
141
            dataset.removeAllSeries();
142
            series = new XYSeries[values.length];
143
            for (int iGraf = 0; iGraf < values.length; iGraf++) {
144
                    series[iGraf] = new XYSeries(names[iGraf]);
145
                    for (int i = 0; i < values[iGraf].length; i++) {
146
                            series[iGraf].add(new Integer(i),  new Integer(values[iGraf][i]));
147
                    }
148
                    dataset.addSeries(series[iGraf]);
149
            }
150
            jPanelChart.repaint();
151
    }
152
    
153
    /**
154
     * Reemplaza los valores de la gr?fica
155
     * @param values Matriz de [n?mero de gr?ficas][Valores por gr?fica]
156
     */
157
    public void replaceValuesChart(int[][] values){
158
            for (int iGraf = 0; iGraf < values.length; iGraf++) {
159
                    for (int i = 0; i < values[iGraf].length; i++) {
160
                            series[iGraf].getDataItem(i).setY(values[iGraf][i]);
161
                    }
162
            }
163
            jPanelChart.repaint();
164
    }
165
    
166
    /**
167
     * Limpia la gr?fica
168
     */
169
    public void cleanChart(){
170
            dataset.removeAllSeries();
171
            jPanelChart.repaint();
172
    }
173
    
174
        /**
175
         * Asigna el tama?o del componente
176
         * @param w Nuevo ancho del componente padre
177
         * @param h Nuevo alto del componente padre
178
         */
179
        public void setComponentSize(int w, int h){
180
                width = w;
181
                height = h;
182
                this.setSize(new java.awt.Dimension(w, h));
183
                this.setPreferredSize(new java.awt.Dimension(w, h));
184
                jPanelChart.setSize(new java.awt.Dimension(w - WIDTH_MARGIN, h - HEIGHT_MARGIN));
185
                jPanelChart.setPreferredSize(new java.awt.Dimension(w - WIDTH_MARGIN, h - HEIGHT_MARGIN));
186
        }
187
}