2 * Copyright (C) 2009 - 2012 SC 4ViewSoft SRL
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
16 package org.achartengine.model;
\r
18 import java.io.Serializable;
\r
19 import java.util.Iterator;
\r
20 import java.util.SortedMap;
\r
22 import org.achartengine.util.IndexXYMap;
\r
23 import org.achartengine.util.MathHelper;
\r
24 import org.achartengine.util.XYEntry;
\r
27 * An XY series encapsulates values for XY charts like line, time, area,
\r
28 * scatter... charts.
\r
30 public class XYSeries implements Serializable {
\r
31 /** The series title. */
\r
32 private String mTitle;
\r
33 /** A map to contain values for X and Y axes and index for each bundle */
\r
34 private final IndexXYMap<Double, Double> mXY = new IndexXYMap<Double, Double>();
\r
35 /** The minimum value for the X axis. */
\r
36 private double mMinX = MathHelper.NULL_VALUE;
\r
37 /** The maximum value for the X axis. */
\r
38 private double mMaxX = -MathHelper.NULL_VALUE;
\r
39 /** The minimum value for the Y axis. */
\r
40 private double mMinY = MathHelper.NULL_VALUE;
\r
41 /** The maximum value for the Y axis. */
\r
42 private double mMaxY = -MathHelper.NULL_VALUE;
\r
43 /** The scale number for this series. */
\r
44 private final int mScaleNumber;
\r
47 * Builds a new XY series.
\r
49 * @param title the series title.
\r
51 public XYSeries(String title) {
\r
56 * Builds a new XY series.
\r
58 * @param title the series title.
\r
59 * @param scaleNumber the series scale number
\r
61 public XYSeries(String title, int scaleNumber) {
\r
63 mScaleNumber = scaleNumber;
\r
67 public int getScaleNumber() {
\r
68 return mScaleNumber;
\r
72 * Initializes the range for both axes.
\r
74 private void initRange() {
\r
75 mMinX = MathHelper.NULL_VALUE;
\r
76 mMaxX = -MathHelper.NULL_VALUE;
\r
77 mMinY = MathHelper.NULL_VALUE;
\r
78 mMaxY = -MathHelper.NULL_VALUE;
\r
79 int length = getItemCount();
\r
80 for (int k = 0; k < length; k++) {
\r
88 * Updates the range on both axes.
\r
90 * @param x the new x value
\r
91 * @param y the new y value
\r
93 private void updateRange(double x, double y) {
\r
94 mMinX = Math.min(mMinX, x);
\r
95 mMaxX = Math.max(mMaxX, x);
\r
96 mMinY = Math.min(mMinY, y);
\r
97 mMaxY = Math.max(mMaxY, y);
\r
101 * Returns the series title.
\r
103 * @return the series title
\r
105 public String getTitle() {
\r
110 * Sets the series title.
\r
112 * @param title the series title
\r
114 public void setTitle(String title) {
\r
119 * Adds a new value to the series.
\r
121 * @param x the value for the X axis
\r
122 * @param y the value for the Y axis
\r
124 public synchronized void add(double x, double y) {
\r
130 * Removes an existing value from the series.
\r
132 * @param index the index in the series of the value to remove
\r
134 public synchronized void remove(int index) {
\r
135 XYEntry<Double, Double> removedEntry = mXY.removeByIndex(index);
\r
136 double removedX = removedEntry.getKey();
\r
137 double removedY = removedEntry.getValue();
\r
138 if (removedX == mMinX || removedX == mMaxX || removedY == mMinY || removedY == mMaxY) {
\r
144 * Removes all the existing values from the series.
\r
146 public synchronized void clear() {
\r
152 * Returns the X axis value at the specified index.
\r
154 * @param index the index
\r
155 * @return the X value
\r
157 public synchronized double getX(int index) {
\r
158 return mXY.getXByIndex(index);
\r
162 * Returns the Y axis value at the specified index.
\r
164 * @param index the index
\r
165 * @return the Y value
\r
167 public synchronized double getY(int index) {
\r
168 return mXY.getYByIndex(index);
\r
172 * Returns submap of x and y values according to the given start and end
\r
174 * @param start start x value
\r
175 * @param stop stop x value
\r
176 * @return a submap of x and y values
\r
178 public synchronized SortedMap<Double, Double> getRange(double start, double stop,
\r
179 int beforeAfterPoints) {
\r
180 // we need to add one point before the start and one point after the end (if
\r
182 // to ensure that line doesn't end before the end of the screen
\r
184 // this would be simply: start = mXY.lowerKey(start) but NavigableMap is
\r
185 // available since API 9
\r
186 SortedMap<Double, Double> headMap = mXY.headMap(start);
\r
187 if (!headMap.isEmpty()) {
\r
188 start = headMap.lastKey();
\r
191 // this would be simply: end = mXY.higherKey(end) but NavigableMap is
\r
192 // available since API 9
\r
193 // so we have to do this hack in order to support older versions
\r
194 SortedMap<Double, Double> tailMap = mXY.tailMap(stop);
\r
195 if (!tailMap.isEmpty()) {
\r
196 Iterator<Double> tailIterator = tailMap.keySet().iterator();
\r
197 Double next = tailIterator.next();
\r
198 if (tailIterator.hasNext()) {
\r
199 stop = tailIterator.next();
\r
204 return mXY.subMap(start, stop);
\r
207 public int getIndexForKey(double key) {
\r
208 return mXY.getIndexForKey(key);
\r
212 * Returns the series item count.
\r
214 * @return the series item count
\r
216 public synchronized int getItemCount() {
\r
221 * Returns the minimum value on the X axis.
\r
223 * @return the X axis minimum value
\r
225 public double getMinX() {
\r
230 * Returns the minimum value on the Y axis.
\r
232 * @return the Y axis minimum value
\r
234 public double getMinY() {
\r
239 * Returns the maximum value on the X axis.
\r
241 * @return the X axis maximum value
\r
243 public double getMaxX() {
\r
248 * Returns the maximum value on the Y axis.
\r
250 * @return the Y axis maximum value
\r
252 public double getMaxY() {
\r