KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > render > InterpolationChartRenderer


1 /*
2     JOpenChart Java Charting Library and Toolkit
3     Copyright (C) 2001 Sebastian Müller
4     http://jopenchart.sourceforge.net
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10
11     This library 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 GNU
14     Lesser General Public License for more details.
15
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20     InterpolationRenderer.java
21     Created on 24. September 2001, 12:50
22 */

23
24 package de.progra.charting.render;
25
26 import java.awt.geom.Line2D JavaDoc;
27 import java.awt.geom.Point2D JavaDoc;
28 import java.awt.geom.Area JavaDoc;
29 import de.progra.charting.CoordSystem;
30 import java.awt.geom.AffineTransform JavaDoc;
31 import de.progra.charting.PointToPixelTranslator;
32 import de.progra.charting.ChartUtilities;
33 import java.awt.Graphics2D JavaDoc;
34 import java.awt.Color JavaDoc;
35 import de.progra.charting.model.ChartDataModel;
36
37 /**
38  * This renderer creates a InterpolationChart.
39  * @author mueller
40  * @version 1.0
41  */

42 public class InterpolationChartRenderer extends AbstractChartRenderer {
43
44     /** Creates new InterpolationChartRenderer
45      * @param rcm the RowColorModel needed to determine the right colors
46      * @param cs the CoordSystem used to translate values into points
47      * @param model the DataModel that should be rendered
48      */

49     public InterpolationChartRenderer(CoordSystem cs, ChartDataModel model) {
50         super(cs, model);
51     }
52     
53     /** Finally renders the Object in the Graphics object.
54      * @param g the Graphics2D object in which to render
55      */

56     public void renderChart(Graphics2D JavaDoc g) {
57         ChartDataModel m = getChartDataModel();
58         RowColorModel rcm = getRowColorModel();
59         AffineTransform JavaDoc yaxis1 = getTransform(CoordSystem.FIRST_YAXIS);
60         
61         int datasetcount = m.getDataSetNumber();
62         Point2D JavaDoc val;
63         Point2D JavaDoc paint = null;
64         Point2D JavaDoc oldpaint = null;
65         if(! m.isColumnNumeric())
66             return;
67         
68         for(int set = 0; set < datasetcount; set++) {
69             // Creating Interpolated Function Data
70
/*
71              "for i in range(AMOUNT) :\n"+
72              " x = lowrange + i * (float(abs(highrange - lowrange)) / AMOUNT)\n"+
73              " columns.append(x)\n"+
74              " model.append("+function+")\n";
75             */

76             
77             double[] x = new double[m.getDataSetLength(set)];
78             double[] y = new double[x.length];
79             
80             for(int i = 0; i < m.getDataSetLength(set); i++) {
81                 x[i] = ((Number JavaDoc)m.getColumnValueAt(set, i)).doubleValue();
82                 
83                 // Catch x[i] == Not A Number
84
if(x[i] != x[i]) x[i] = 0.0;
85                 
86                 y[i] = m.getValueAt(set, i).doubleValue();
87             }
88                         
89             int AMOUNT = 2000;
90             double lowrange = m.getChartDataModelConstraints(CoordSystem.FIRST_YAXIS).getMinimumColumnValue();
91             double hirange = m.getChartDataModelConstraints(CoordSystem.FIRST_YAXIS).getMaximumColumnValue();
92
93             double xa[] = new double[AMOUNT];
94             double ya[] = new double[AMOUNT];
95             
96             for(int i = 0; i < AMOUNT; i++) {
97                 xa[i] = lowrange + i * (Math.abs(hirange - lowrange) / (double)AMOUNT);
98                 ya[i] = ChartUtilities.interpolate(x, y, xa[i]);
99             }
100             
101             // Rendering xa[] and ya[]
102
for(int i = 0; i < AMOUNT; i++) {
103                 
104                 val = new Point2D.Double JavaDoc(xa[i], ya[i]);
105                     
106                 oldpaint = paint;
107                 if(yaxis1.transform(val, null) != null)
108                     paint = yaxis1.transform(val, paint);
109                 else
110                     continue;
111                 
112                 g.setColor(rcm.getColor(set));
113                 
114                 
115                 if(oldpaint != null) {
116                     
117                     g.drawLine((int)oldpaint.getX(), (int)oldpaint.getY(),
118                                (int)paint.getX(), (int)paint.getY());
119                 }
120             }
121             oldpaint = null;
122             paint = null;
123         }
124    }
125 }
126
Popular Tags