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

#484: Add support for modifying the sensitivity of onTap() method inv… #485

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
13 changes: 1 addition & 12 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ tmp/**/*
*.bak
*.swp
*~.nib
# Local configuration file (sdk path, etc)
local.properties
.classpath
.settings/
Expand Down Expand Up @@ -52,25 +53,13 @@ local.properties
bin/
gen/

# Local configuration file (sdk path, etc)
local.properties

# Eclipse project files
.classpath
.project

# Proguard folder generated by Eclipse
proguard/

# Proguard folder generated by Intellij
proguard_logs/

# Intellij project files
*.iml
*.ipr
*.iws
.idea/

adt-bundle-windows-x86_64/

### Windows ###
Expand Down
71 changes: 50 additions & 21 deletions src/main/java/com/jjoe64/graphview/series/BaseSeries.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,16 @@ public abstract class BaseSeries<E extends DataPointInterface> implements Series
private double mLowestYCache = Double.NaN;

/**
* cahce for highest y value
* cache for highest y value
*/
private double mHighestYCache = Double.NaN;

/**
* stores the threshold distance value will
* trigger the onTap method of the series.
*/
private double mTapTriggerDistanceThreshold = 120;

/**
* listener to handle tap events on a data point
*/
Expand All @@ -105,8 +111,8 @@ 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<GraphView>();
Expand All @@ -129,7 +135,7 @@ public double getLowestValueX() {
*/
public double getHighestValueX() {
if (mData.isEmpty()) return 0d;
return mData.get(mData.size()-1).getX();
return mData.get(mData.size() - 1).getX();
}

/**
Expand Down Expand Up @@ -174,7 +180,7 @@ public double getHighestValueY() {
* If it is only a part of the data, the range is returned plus one datapoint
* before and after to get a nice scrolling.
*
* @param from minimal x-value
* @param from minimal x-value
* @param until maximal x-value
* @return data for the range +/- 1 datapoint
*/
Expand Down Expand Up @@ -327,14 +333,14 @@ 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();
}
}
if (shortest != null) {
if (shortestDistance < 120) {
if (shortestDistance < mTapTriggerDistanceThreshold) {
return shortest;
}
}
Expand Down Expand Up @@ -365,8 +371,8 @@ public E findDataPointAtX(float x) {
/**
* register the datapoint to find it at a tap
*
* @param x pixel
* @param y pixel
* @param x pixel
* @param y pixel
* @param dp the data point to save
*/
protected void registerDataPoint(float x, float y, E dp) {
Expand Down Expand Up @@ -430,17 +436,17 @@ 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 scrollToEnd true => graphview will scroll to the end (maxX)
* @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 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()) {
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) {
Expand Down Expand Up @@ -487,9 +493,9 @@ 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 scrollToEnd true => graphview will scroll to the end (maxX)
* @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
*/
Expand All @@ -508,14 +514,14 @@ 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()) {
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 {
Expand All @@ -538,4 +544,27 @@ protected void checkValueOrder(DataPointInterface onlyLast) {
public void clearCursorModeCache() {
mIsCursorModeCache = null;
}

/**
* An enum class to represent the sensitivity of the tap events.
* Each enum value maps an amount to itself.
*/
public enum TapSensitivity {
LOW(150), NORMAL(120), MEDIUM(90), HIGH(60), EXTREME(30);
private final double tapThreshold;

TapSensitivity(final double tapThreshold) {
this.tapThreshold = tapThreshold;
}
}

/**
* sets the tap sensitivity threshold.
*
* @param tapSensitivity the sensitivity level of tap
*/
public void setTapThreshold(final TapSensitivity tapSensitivity) {
this.mTapTriggerDistanceThreshold = tapSensitivity.tapThreshold;

}
}