001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: LineChart.java,v $
023   Revision 1.8  2004/05/05 22:24:14  markl
024   comment block updates
025
026   Revision 1.7  2003/11/07 19:16:52  markl
027   Added get/setPointSpacing(), get/setPointRadius() methods.
028
029   Revision 1.6  2003/01/19 09:33:21  markl
030   Javadoc & comment header updates.
031
032   Revision 1.5  2001/03/12 07:23:30  markl
033   Javadoc cleanup.
034
035   Revision 1.4  2000/10/17 20:43:10  markl
036   Fixes to catch null values.
037
038   Revision 1.3  2000/10/15 09:40:30  markl
039   Added javadoc and final API polishing.
040
041   Revision 1.2  2000/10/13 02:04:20  markl
042   Added remaining classes, and integrated components with models.
043   ----------------------------------------------------------------------------
044*/
045
046package kiwi.ui.graph;
047
048import java.awt.*;
049import java.util.*;
050import javax.swing.*;
051
052import kiwi.util.*;
053
054/** A chart that renders the value of each variable as a series of points
055 * connected by lines. This type of chart is used to illustrate trends across
056 * data samples.
057 *
058 * <p><center><img src="snapshot/LineChart.gif"><br>
059 * <i>An example LineChart.</i>
060 * </center>
061 *
062 * @author Mark Lindner
063 */
064
065public class LineChart extends ChartView
066  {
067  private int pointSpacing = 40;
068  private int pointRadius = 2;
069  private int pointWidth;
070  private int scaleWidth = 50;
071
072  /** Construct a new <code>LineChart</code> for the specified chart
073   * definition.
074   *
075   * @param chart The chart definition.
076   */
077  
078  public LineChart(Chart chart)
079    {
080    super(chart);
081    
082    setOrientation(VERTICAL);
083    setTickInterval(chart.getTickInterval());
084    setPointRadius(2);
085    }
086
087  /** Set the point spacing (the space between points on the line, in pixels)
088   *
089   * @param spacing The new spacing. The value must be greater than zero.
090   *
091   * @since Kiwi 1.4.3
092   */
093
094  public void setPointSpacing(int spacing)
095    {
096    if(spacing > 0)
097      pointSpacing = spacing;
098    }
099
100  /** Get the point spacing (the space between the points on the line, in
101   * pixels.)
102   *
103   * @return The point spacing, in pixels.
104   *
105   * @since Kiwi 1.4.3
106   */
107  
108  public int getPointSpacing()
109    {
110    return(pointSpacing);
111    }
112
113  /** Set the point radius (the size of a point on the line, in pixels)
114   *
115   * @param radius The new radius. The value must be greater than or equal
116   * to zero.
117   *
118   * @since Kiwi 1.4.3
119   */
120
121  public void setPointRadius(int radius)
122    {
123    if(radius >= 0)
124      pointRadius = radius;
125
126    pointWidth = (pointRadius * 2) + 1;
127    }
128  
129  /** Get the point radius (the size of a point on the line, in pixels)
130   *
131   * @return The point radius, in pixels.
132   *
133   * @since Kiwi 1.4.3
134   */
135
136  public int getPointRadius()
137    {
138    return(pointRadius);
139    }
140  
141  /** Paint the chart.
142   */
143  
144  protected void paintChart(Graphics gc)
145    {
146    int lx = 0, ly = 0, px, py;
147    Dimension d = getSize();
148
149    Enumeration e = chart.getValues();
150    while(e.hasMoreElements())
151      {
152      boolean first = true;
153      px = (horizontalPad * 2) + scaleWidth;      
154
155      ChartValue cv = (ChartValue)e.nextElement();
156      String var = cv.getName();
157      gc.setColor(cv.getColor());
158
159      Enumeration f = model.getDataSamples();
160      while(f.hasMoreElements())
161        {
162        DataSample ds = (DataSample)f.nextElement();
163        Object o = ds.getValue(var);
164        double value = 0.0;
165        if((o != null) && (o instanceof Number))
166          value = ((Number)o).doubleValue();
167          
168        py = (int)(d.height - verticalPad - (value * scale));
169        
170        if(!first)
171          gc.drawLine(lx, ly, px, py);
172
173        gc.fillRect(px - pointRadius, py - pointRadius, pointWidth,
174                    pointWidth);
175        lx = px;
176        ly = py;
177        first = false;
178
179        px += pointSpacing;
180        }
181      }
182
183    drawVerticalScale(gc, horizontalPad + scaleWidth);    
184    }
185
186  /** Compute the maximum value.
187   */
188
189  protected double getMaxValue()
190    {
191    double maxval = 0.0;
192
193    Enumeration e = chart.getValues();
194    while(e.hasMoreElements())
195      {
196      ChartValue cv = (ChartValue)e.nextElement();
197      String var = cv.getName();
198      
199      Enumeration f = model.getDataSamples();
200      while(f.hasMoreElements())
201        {
202        DataSample ds = (DataSample)f.nextElement();
203        Object o = ds.getValue(var);
204        double value = 0.0;
205        if((o != null) && (o instanceof Number))
206          value = ((Number)o).doubleValue();
207
208        if(value > maxval)
209          maxval = value;
210        }
211      }
212
213    return(maxval);
214    }
215  
216  }
217
218/* end of source file */