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

marker single tap listener #5125

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
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ public void onChartSingleTapped(MotionEvent me) {
Log.i("SingleTap", "Chart single-tapped.");
}

@Override
public void onMarkerSingleTapped(MotionEvent me) {
Log.i("MarkerSingleTap", "Marker single-tapped.");
}

@Override
public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
Log.i("Fling", "Chart fling. VelocityX: " + velocityX + ", VelocityY: " + velocityY);
Expand Down Expand Up @@ -351,4 +356,7 @@ public void onStartTrackingTouch(SeekBar seekBar) {}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {}

@Override
public void onPointerCaptureChanged(boolean hasCapture) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public void onChartSingleTapped(MotionEvent me) {
Log.i("SingleTap", "Chart single-tapped.");
}

@Override
public void onMarkerSingleTapped(MotionEvent me) {
Log.i("MarkerSingleTap", "Marker single-tapped.");
}

@Override
public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
Log.i("Fling", "Chart fling. VelocityX: " + velocityX + ", VelocityY: " + velocityY);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.mikephil.charting.components;

import android.graphics.Canvas;
import android.view.MotionEvent;

import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
Expand Down Expand Up @@ -44,4 +45,12 @@ public interface IMarker {
* @param posY
*/
void draw(Canvas canvas, float posX, float posY);

/**
* Checks the click position is on the marker or not
*
* @param posX
* @param posY
*/
boolean isClickOnMarker(float posX, float posY);
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,20 @@ public void draw(Canvas canvas, float posX, float posY) {

int saveId = canvas.save();
// translate to the correct position and draw
canvas.translate(posX + offset.x, posY + offset.y);
float drawingPosX = posX + offset.x;
float drawingPosY = posY + offset.y;
canvas.translate(drawingPosX, drawingPosY);
mDrawable.draw(canvas);
canvas.restoreToCount(saveId);

mDrawable.setBounds(mDrawableBoundsCache);
}

@Override
public boolean isClickOnMarker(float posX, float posY) {
if(mDrawableBoundsCache == null) {
return false;
}
return mDrawableBoundsCache.contains((int) posX, (int) posY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
Expand All @@ -25,6 +26,7 @@ public class MarkerView extends RelativeLayout implements IMarker {
private MPPointF mOffset = new MPPointF();
private MPPointF mOffset2 = new MPPointF();
private WeakReference<Chart> mWeakChart;
private Rect rect;

/**
* Constructor. Sets up the MarkerView with a custom layout resource.
Expand Down Expand Up @@ -117,13 +119,22 @@ public void refreshContent(Entry e, Highlight highlight) {

@Override
public void draw(Canvas canvas, float posX, float posY) {

MPPointF offset = getOffsetForDrawingAtPoint(posX, posY);

int saveId = canvas.save();
// translate to the correct position and draw
canvas.translate(posX + offset.x, posY + offset.y);
float drawingPosX = posX + offset.x;
float drawingPosY = posY + offset.y;
canvas.translate(drawingPosX, drawingPosY);
draw(canvas);
canvas.restoreToCount(saveId);
rect = new Rect((int) drawingPosX, (int) drawingPosY, (int) drawingPosX + getWidth(), (int) drawingPosY + getHeight());
}

@Override
public boolean isClickOnMarker(float posX, float posY) {
if(rect == null) {
return false;
}
return rect.contains((int) posX, (int) posY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,9 @@ public boolean onSingleTapUp(MotionEvent e) {

OnChartGestureListener l = mChart.getOnChartGestureListener();

if (l != null) {
if (l != null && mChart != null && mChart.isDrawMarkersEnabled() && mChart.valuesToHighlight() && mChart.getMarker().isClickOnMarker(e.getX(), e.getY())) {
l.onMarkerSingleTapped(e);
} else if (l != null) {
l.onChartSingleTapped(e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public interface OnChartGestureListener {
*/
void onChartSingleTapped(MotionEvent me);

/**
* Callbacks when the marker is single-tapped.
*
* @param me
*/
void onMarkerSingleTapped(MotionEvent me);

/**
* Callbacks then a fling gesture is made on the chart.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ public boolean onSingleTapUp(MotionEvent e) {

OnChartGestureListener l = mChart.getOnChartGestureListener();

if (l != null) {
if (l != null && mChart != null && mChart.isDrawMarkersEnabled() && mChart.valuesToHighlight() && mChart.getMarker().isClickOnMarker(e.getX(), e.getY())) {
l.onMarkerSingleTapped(e);
} else if (l != null) {
l.onChartSingleTapped(e);
}

Expand Down