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: Chart.java,v $
023   Revision 1.8  2004/05/05 22:24:14  markl
024   comment block updates
025
026   Revision 1.7  2004/05/05 18:42:11  markl
027   comment header updates
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:29  markl
033   Javadoc cleanup.
034
035   Revision 1.4  2000/10/15 09:40:28  markl
036   Added javadoc and final API polishing.
037
038   Revision 1.3  2000/10/13 08:22:55  markl
039   Added max sample count and updated read()/write() methods.
040
041   Revision 1.2  2000/10/13 08:06:05  markl
042   Integration fixes, cleanup.
043
044   Revision 1.1  2000/10/13 02:04:18  markl
045   Added remaining classes, and integrated components with models.
046   ----------------------------------------------------------------------------
047*/
048
049package kiwi.ui.graph;
050
051import java.awt.*;
052import java.io.*;
053import java.util.*;
054
055import kiwi.text.*;
056
057/** A class that represents a chart definition. The chart definition describes
058 * various aspects of the chart, including the data values that the chart
059 * displays, the decimal precision of the data values, the intervals at which
060 * tickmarks should be drawn on the chart's scale, and the maximum number of
061 * data samples to display in the chart.
062 *
063 * @author Mark Lindner
064 */
065
066public class Chart
067  {
068  private String name;
069  private String type = null;
070  private double tickInterval = 10.0;
071  private int precision = 2;
072  private int maxSampleCount = 10;
073  private Vector values;
074
075  /** Construct a new, unnamed <code>Chart</code>.
076   */
077  
078  public Chart()
079    {
080    this(null);
081    }
082  
083  /** Construct a new <code>Chart</code> with the specified name.
084   *
085   * @param name The name of the chart.
086   */
087
088  public Chart(String name)
089    {
090    this.name = name;
091
092    values = new Vector();
093    }
094
095  /** Add a <code>ChartValue</code> to this chart definition.
096   *
097   * @param value The value to add.
098   */
099  
100  public void addValue(ChartValue value)
101    {
102    values.addElement(value);
103    }
104
105  /** Add a <code>ChartValue</code> to this chart definition at the specified
106   * position.
107   *
108   * @param value The value to add.
109   * @param position The position to add the value at.
110   */
111  
112  public void addValue(ChartValue value, int position)
113    {
114    values.insertElementAt(value, position);
115    }
116
117  /** Get the value at the specified position in this chart definition.
118   *
119   * @param position The position of the value.
120   * @return the <code>ChartValue</code> object at the specified position.
121   */
122  
123  public ChartValue getValueAt(int position)
124    {
125    return((ChartValue)(values.elementAt(position)));
126    }
127
128  /** Get the values in this chart.
129   *
130   * @return An <code>Enumeration</code> of <code>ChartValue</code> objects in
131   * this chart definition.
132   */
133  
134  public Enumeration getValues()
135    {
136    return(values.elements());
137    }
138
139  /** Remove the value at the specified position from this chart definition.
140   *
141   * @param position The position of the value.
142   */
143  
144  public void removeValueAt(int position)
145    {
146    values.removeElementAt(position);
147    }
148
149  /** Remove all of the values from this chart definition.
150   */
151
152  public void removeAllValues()
153    {
154    values.removeAllElements();
155    }
156
157  /** Get the number of values in this chart definition.
158   *
159   * @return The value count.
160   */
161  
162  public int getValueCount()
163    {
164    return(values.size());
165    }
166
167  /** Set the tick interval for this chart definition. The tick interval is
168   * specified (conceptually) in the same units  as the actual data values in
169   * the chart.
170   *
171   * @param interval The tick interval.
172   */
173  
174  public void setTickInterval(double interval)
175    {
176    if(interval < 0.01)
177      throw(new IllegalArgumentException("Invalid tick interval"));
178    
179    tickInterval = interval;
180    }
181
182  /** Get the tick interval for this chart definition.
183   *
184   * @return The tick interval.
185   */
186  
187  public double getTickInterval()
188    {
189    return(tickInterval);
190    }
191
192  /** Set the decimal precision for this chart definition. This value
193   * determines how many decimal places are significant in the data values
194   * that are displayed by this chart; the values in the chart scale will also
195   * be rendered to this many decimal places.
196   *
197   * @param precision The precision.
198   */
199
200  public void setPrecision(int precision)
201    {
202    if(precision < 0 || precision > 10)
203      throw(new IllegalArgumentException("Invalid precision"));
204    
205    this.precision = precision;
206    }
207
208  /** Get the decimal precision for this chart definition.
209   *
210   * @return The precision.
211   */
212  
213  public int getPrecision()
214    {
215    return(precision);
216    }
217
218  /** Get the name for this chart definition.
219   *
220   * @return The name of this chart definition.
221   */
222  
223  public String getName()
224    {
225    return(name);
226    }
227
228  /** Get the chart type for this chart definition.
229   *
230   * @return The chart type.
231   */
232
233  public String getType()
234    {
235    return(type);
236    }
237
238  /** Set the chart type for this chart definition.
239   *
240   * @param type The chart type.
241   */
242  
243  public void setType(String type)
244    {
245    this.type = type;
246    }
247
248  /** Set the maximum number of data samples that will be displayed in the
249   * chart. This essentially specifies how many data samples will be used to
250   * generate the chart. The value must be at least 1.
251   *
252   * @param maxSampleCount The maximum sample count.
253   */
254
255  public void setMaxSampleCount(int maxSampleCount)
256    {
257    if(maxSampleCount < 1)
258      throw(new IllegalArgumentException("Invalid max sample count"));
259
260    this.maxSampleCount = maxSampleCount;
261    }
262
263  /** Get the maximum number of data samples that will be displayed in the
264   * chart.
265   *
266   * @return The maximum sample count.
267   */
268  
269  public int getMaxSampleCount()
270    {
271    return(maxSampleCount);
272    }
273
274  /** Write this chart definition to a stream.
275   *
276   * @param outs The stream to read write to.
277   * @throws java.io.IOException If an I/O error occurs.
278   */
279  
280  public void write(BufferedWriter outs) throws IOException
281    {
282    outs.write(name);
283    outs.newLine();
284
285    outs.write(type);
286    outs.newLine();
287
288    outs.write(String.valueOf(tickInterval));
289    outs.newLine();
290
291    outs.write(String.valueOf(precision));
292    outs.newLine();
293
294    outs.write(String.valueOf(maxSampleCount));
295    outs.newLine();
296
297    Enumeration e = getValues();
298    while(e.hasMoreElements())
299      {
300      ChartValue v = (ChartValue)e.nextElement();
301      outs.write(v.getName());
302      outs.newLine();
303      outs.write(v.getLabel());
304      outs.newLine();
305      outs.write(ColorFormatter.format(v.getColor()));
306      outs.newLine();
307      }
308    
309    outs.newLine();
310    }
311
312  /** Read this chart definition from a stream.
313   *
314   * @param ins The stream to read from.
315   * @throws java.io.IOException If an I/O error occurs.
316   */
317  
318  public void read(BufferedReader ins) throws IOException
319    {
320    String s, t, u;
321
322    s = readLine(ins);
323    name = s;
324
325    s = readLine(ins);
326    type = s;
327
328    s = readLine(ins);
329    tickInterval = Double.parseDouble(s);
330
331    s = readLine(ins);
332    precision = Integer.parseInt(s);
333
334    s = readLine(ins);
335    maxSampleCount = Integer.parseInt(s);
336    
337    removeAllValues();
338    
339    for(;;)
340      {
341      s = readLine(ins);
342      if(s.trim().length() == 0)
343        break;
344      t = readLine(ins);
345      if(t.trim().length() == 0)
346        break;
347      u = readLine(ins);
348      if(u.trim().length() == 0)
349        break;
350
351      Color c = Color.gray;
352
353      try
354        {
355        c = ColorFormatter.parse(u);
356        }
357      catch(ParsingException ex)
358        {
359        }
360
361      ChartValue v = new ChartValue(s, t, c);
362      addValue(v);
363      }
364    }
365
366  /*
367   */
368
369  private String readLine(BufferedReader reader) throws IOException
370    {
371    String s = reader.readLine();
372    if(s == null)
373      throw(new IOException("End of file."));
374    
375    return(s);
376    }
377
378  /** Get a string representation of this chart.
379   *
380   * @return The name of the chart.
381   */
382  
383  public String toString()
384    {
385    return(name);
386    }
387  
388  }
389
390/* end of source file */