001    /*
002     * This file is part of the Echo Point Project.  This project is a
003     * collection of Components that have extended the Echo Web Application
004     * Framework Version 3.
005     *
006     * Version: MPL 1.1
007     *
008     * The contents of this file are subject to the Mozilla Public License Version
009     * 1.1 (the "License"); you may not use this file except in compliance with
010     * the License. You may obtain a copy of the License at
011     * http://www.mozilla.org/MPL/
012     *
013     * Software distributed under the License is distributed on an "AS IS" basis,
014     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015     * for the specific language governing rights and limitations under the
016     * License.
017     */
018    
019    package echopoint.google.chart.model;
020    
021    import java.io.Serializable;
022    import java.util.ArrayList;
023    import java.util.Collection;
024    import java.util.Collections;
025    
026    /**
027     * A model object that represents a data set that is to be plotted.  Note that
028     * the data models are designed to correspond to the way Google expecte data
029     * and hence does not provide a more logical <code>Point</code> class.  A
030     * point class would not work naturally for bar charts either.
031     *
032     * @author Rakesh 2008-08-10
033     * @version $Id: ChartData.java 116 2009-02-19 00:23:08Z sptrakesh $
034     */
035    public class ChartData<N extends Number> implements Serializable
036    {
037      private static final long serialVersionUID = 1l;
038    
039      /**
040       * The collection of numbers to be displayed along the x-axis.  This is
041       * mandatory.
042       */
043      private Collection<N> xdata = new ArrayList<N>();
044    
045      /**
046       * The maximum value to be plotted along the x-axis.  This is used to ensure
047       * a gap between the edge of the graph and the highest data point.
048       */
049      private int xmax = -1;
050    
051      /**
052       * The optional collection of numbers to be displayed along the y-axis.  This
053       * in conjunction with {@link #xdata} precisely defines the co-ordinate of
054       * a point to be plotted.
055       */
056      private Collection<N> ydata = new ArrayList<N>();
057    
058      /**
059       * The optional maximum value to be plotted along the y-axis.  This is
060       * used to ensure a gap between the edge of the graph and the highest data
061       * point.
062       */
063      private int ymax = -1;
064    
065      /**
066       * The colour to apply to the data set.  If no values are specified, default
067       * colours will be assigned sequentially from <code>
068       * echopoint.google.internal.AbstractChart#COLORS</code> client side array.
069       */
070      private String color;
071    
072      /**
073       * The legend to display for this data set.  Note that if a legend is
074       * specified for one data set in the collection of data sets being plotted
075       * on the same graph, all sets must be assigned one.
076       */
077      private String legend;
078    
079      /**
080       * The marker styles to assign to the data points.  If the collection has
081       * only one element, all points are assigned the same marker.  If not, the
082       * size of the collection must equal the size of {@link #xdata} and {@link
083       * #ydata}.
084       */
085      private Collection<ShapeMarker> markers = new ArrayList<ShapeMarker>();
086    
087      /** Default constructor. */
088      public ChartData() {}
089    
090      /**
091       * Create a new instance using the mandatory field values.
092       *
093       * @param xdata The {@link #xdata} to use.
094       * @param xmax The {@link #xmax} to use.
095       */
096      public ChartData( final Collection<N> xdata, final int xmax )
097      {
098        setXdata( xdata );
099        setXmax( xmax );
100      }
101    
102      /**
103       * Accessor for property 'xdata'.  Returns an unmodifiable view of the
104       * data.
105       *
106       * @return Value for property 'xdata'.
107       */
108      public Collection<N> getXdata()
109      {
110        return Collections.unmodifiableCollection( xdata );
111      }
112    
113      /**
114       * Mutator for property 'xdata'.
115       *
116       * @param xdata Value to set for property 'xdata'.
117       */
118      public void setXdata( final Collection<N> xdata )
119      {
120        this.xdata.clear();
121        this.xdata.addAll( xdata );
122        xmax = getXmax();
123      }
124    
125      /**
126       * Accessor for property 'xmax'. If {@link #xmax} is not defined, return
127       * the maximum value from {@link #xdata}.
128       *
129       * @return Value for property 'xmax'.
130       */
131      public int getXmax()
132      {
133        if ( xmax > 0 ) return xmax;
134    
135        double max = 0.0;
136        for ( N value : xdata )
137        {
138          if ( ( value != null ) && ( value.doubleValue() > max ) )
139          {
140            max = value.doubleValue();
141          }
142        }
143    
144        return (int) max + 5;
145      }
146    
147      /**
148       * Mutator for property 'xmax'.
149       *
150       * @param xmax Value to set for property 'xmax'.
151       */
152      public void setXmax( final int xmax )
153      {
154        this.xmax = xmax;
155      }
156    
157      /**
158       * Accessor for property 'ydata'.  Returns an unmodifiable view of the data.
159       *
160       * @return Value for property 'ydata'.
161       */
162      public Collection<N> getYdata()
163      {
164        return Collections.unmodifiableCollection( ydata );
165      }
166    
167      /**
168       * Mutator for property 'ydata'.
169       *
170       * @param ydata Value to set for property 'ydata'.
171       */
172      public void setYdata( final Collection<N> ydata )
173      {
174        this.ydata.clear();
175        this.ydata.addAll( ydata );
176        ymax = getYmax();
177      }
178    
179      /**
180       * Accessor for property 'ymax'.
181       *
182       * @return Value for property 'ymax'.
183       */
184      public int getYmax()
185      {
186        if ( ymax != -1 ) return ymax;
187    
188        double max = 0.0;
189        for ( N value : ydata )
190        {
191          if ( ( value != null ) && ( value.doubleValue() > max ) )
192          {
193            max = value.doubleValue();
194          }
195        }
196    
197        return (int) max + 5;
198      }
199    
200      /**
201       * Mutator for property 'ymax'.
202       *
203       * @param ymax Value to set for property 'ymax'.
204       */
205      public void setYmax( final int ymax )
206      {
207        this.ymax = ymax;
208      }
209    
210      /**
211       * Accessor for property 'color'.
212       *
213       * @return Value for property 'color'.
214       */
215      public String getColor()
216      {
217        return color;
218      }
219    
220      /**
221       * Mutator for property 'color'.
222       *
223       * @param color Value to set for property 'color'.
224       */
225      public void setColor( final String color )
226      {
227        this.color = color;
228      }
229    
230      /**
231       * Accessor for property 'legend'.
232       *
233       * @return Value for property 'legend'.
234       */
235      public String getLegend()
236      {
237        return legend;
238      }
239    
240      /**
241       * Mutator for property 'legend'.
242       *
243       * @param legend Value to set for property 'legend'.
244       */
245      public void setLegend( final String legend )
246      {
247        this.legend = legend;
248      }
249    
250      /**
251       * Accessor for property 'markers'.
252       *
253       * @return Value for property 'markers'.
254       */
255      public Collection<ShapeMarker> getMarkers()
256      {
257        return markers;
258      }
259    
260      /**
261       * Mutator for property 'markers'.
262       *
263       * @param markers Value to set for property 'markers'.
264       */
265      public void setMarkers( final Collection<ShapeMarker> markers )
266      {
267        this.markers.clear();
268        this.markers.addAll( markers );
269      }
270    }