Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- added function to retrieve all DataPoints #504

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 60 additions & 38 deletions src/main/java/com/jjoe64/graphview/series/BaseSeries.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@

import android.graphics.Canvas;
import android.graphics.PointF;
import android.util.Log;

import com.jjoe64.graphview.GraphView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

/**
* Basis implementation for series.
Expand All @@ -46,6 +42,7 @@
* @author jjoe64
*/
public abstract class BaseSeries<E extends DataPointInterface> implements Series<E> {

/**
* holds the data
*/
Expand Down Expand Up @@ -106,8 +103,7 @@ public BaseSeries() {
/**
* creates series with data
*
* @param data data points
* important: array has to be sorted from lowest x-value to the highest
* @param data data points important: array has to be sorted from lowest x-value to the highest
*/
public BaseSeries(E[] data) {
mGraphViews = new ArrayList<>();
Expand All @@ -121,23 +117,29 @@ public BaseSeries(E[] data) {
* @return the lowest x value, or 0 if there is no data
*/
public double getLowestValueX() {
if (mData.isEmpty()) return 0d;
if (mData.isEmpty()) {
return 0d;
}
return mData.get(0).getX();
}

/**
* @return the highest x value, or 0 if there is no data
*/
public double getHighestValueX() {
if (mData.isEmpty()) return 0d;
return mData.get(mData.size()-1).getX();
if (mData.isEmpty()) {
return 0d;
}
return mData.get(mData.size() - 1).getX();
}

/**
* @return the lowest y value, or 0 if there is no data
*/
public double getLowestValueY() {
if (mData.isEmpty()) return 0d;
if (mData.isEmpty()) {
return 0d;
}
if (!Double.isNaN(mLowestYCache)) {
return mLowestYCache;
}
Expand All @@ -155,7 +157,9 @@ public double getLowestValueY() {
* @return the highest y value, or 0 if there is no data
*/
public double getHighestValueY() {
if (mData.isEmpty()) return 0d;
if (mData.isEmpty()) {
return 0d;
}
if (!Double.isNaN(mHighestYCache)) {
return mHighestYCache;
}
Expand Down Expand Up @@ -234,8 +238,11 @@ public E next() {
if (nextNextValue != null) {
nextValue = nextNextValue;
nextNextValue = null;
} else if (org.hasNext()) nextValue = org.next();
else nextValue = null;
} else if (org.hasNext()) {
nextValue = org.next();
} else {
nextValue = null;
}
return r;
} else {
throw new NoSuchElementException();
Expand All @@ -250,6 +257,22 @@ public boolean hasNext() {
}
}


/**
* Get all {@link DataPoint} entries
*
* @return the data points
*/
public List<DataPoint> getDataPoints() {
List<DataPoint> dataPoints = new ArrayList<>();
Iterator itr = getValues(0, mData.size() - 1);
while (itr.hasNext()) {
dataPoints.add((DataPoint) itr.next());
}
return dataPoints;
}


/**
* @return the title of the series
*/
Expand Down Expand Up @@ -278,8 +301,6 @@ public int getColor() {
* set the color of the series. This will be used in
* plotting (depends on the series implementation) and
* is used in the legend.
*
* @param mColor
*/
public void setColor(int mColor) {
this.mColor = mColor;
Expand Down Expand Up @@ -328,7 +349,7 @@ protected E findDataPoint(float x, float y) {
float x2 = x;
float y2 = y;

float distance = (float) Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
float distance = (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
if (shortest == null || distance < shortestDistance) {
shortestDistance = distance;
shortest = entry.getValue();
Expand Down Expand Up @@ -401,8 +422,8 @@ protected void resetDataPoints() {
* clears the data of this series and sets new.
* will redraw the graph
*
* @param data the values must be in the correct order!
* x-value has to be ASC. First the lowest x value and at least the highest x value.
* @param data the values must be in the correct order! x-value has to be ASC. First the lowest
* x value and at least the highest x value.
*/
public void resetData(E[] data) {
mData.clear();
Expand Down Expand Up @@ -432,19 +453,19 @@ public void onGraphViewAttached(GraphView graphView) {
}

/**
*
* @param dataPoint values the values must be in the correct order!
* x-value has to be ASC. First the lowest x value and at least the highest x value.
* @param dataPoint values the values must be in the correct order! x-value has to be ASC. First
* the lowest x value and at least the highest x value.
* @param scrollToEnd true => graphview will scroll to the end (maxX)
* @param maxDataPoints if max data count is reached, the oldest data
* value will be lost to avoid memory leaks
* @param silent set true to avoid rerender the graph
* @param maxDataPoints if max data count is reached, the oldest data value will be lost to
* avoid memory leaks
* @param silent set true to avoid rerender the graph
*/
public void appendData(E dataPoint, boolean scrollToEnd, int maxDataPoints, boolean silent) {
checkValueOrder(dataPoint);

if (!mData.isEmpty() && dataPoint.getX() < mData.get(mData.size()-1).getX()) {
throw new IllegalArgumentException("new x-value must be greater then the last value. x-values has to be ordered in ASC.");
if (!mData.isEmpty() && dataPoint.getX() < mData.get(mData.size() - 1).getX()) {
throw new IllegalArgumentException(
"new x-value must be greater then the last value. x-values has to be ordered in ASC.");
}
synchronized (mData) {
int curDataCount = mData.size();
Expand Down Expand Up @@ -491,12 +512,11 @@ public void appendData(E dataPoint, boolean scrollToEnd, int maxDataPoints, bool
}

/**
*
* @param dataPoint values the values must be in the correct order!
* x-value has to be ASC. First the lowest x value and at least the highest x value.
* @param dataPoint values the values must be in the correct order! x-value has to be ASC. First
* the lowest x value and at least the highest x value.
* @param scrollToEnd true => graphview will scroll to the end (maxX)
* @param maxDataPoints if max data count is reached, the oldest data
* value will be lost to avoid memory leaks
* @param maxDataPoints if max data count is reached, the oldest data value will be lost to
* avoid memory leaks
*/
public void appendData(E dataPoint, boolean scrollToEnd, int maxDataPoints) {
appendData(dataPoint, scrollToEnd, maxDataPoints, false);
Expand All @@ -513,23 +533,24 @@ public boolean isEmpty() {
/**
* checks that the data is in the correct order
*
* @param onlyLast if not null, it will only check that this
* datapoint is after the last point.
* @param onlyLast if not null, it will only check that this datapoint is after the last point.
*/
protected void checkValueOrder(DataPointInterface onlyLast) {
if (mData.size()>1) {
if (mData.size() > 1) {
if (onlyLast != null) {
// only check last
if (onlyLast.getX() < mData.get(mData.size()-1).getX()) {
throw new IllegalArgumentException("new x-value must be greater then the last value. x-values has to be ordered in ASC.");
if (onlyLast.getX() < mData.get(mData.size() - 1).getX()) {
throw new IllegalArgumentException(
"new x-value must be greater then the last value. x-values has to be ordered in ASC.");
}
} else {
double lx = mData.get(0).getX();

for (int i = 1; i < mData.size(); i++) {
if (mData.get(i).getX() != Double.NaN) {
if (lx > mData.get(i).getX()) {
throw new IllegalArgumentException("The order of the values is not correct. X-Values have to be ordered ASC. First the lowest x value and at least the highest x value.");
throw new IllegalArgumentException(
"The order of the values is not correct. X-Values have to be ordered ASC. First the lowest x value and at least the highest x value.");
}
lx = mData.get(i).getX();
}
Expand All @@ -538,7 +559,8 @@ protected void checkValueOrder(DataPointInterface onlyLast) {
}
}

public abstract void drawSelection(GraphView mGraphView, Canvas canvas, boolean b, DataPointInterface value);
public abstract void drawSelection(GraphView mGraphView, Canvas canvas, boolean b,
DataPointInterface value);

public void clearCursorModeCache() {
mIsCursorModeCache = null;
Expand Down