Skip to content

Commit d4b2422

Browse files
committed
Minor improvements to recent PR.
1 parent f629df9 commit d4b2422

8 files changed

Lines changed: 71 additions & 119 deletions

File tree

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity2.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ protected void onCreate(Bundle savedInstanceState) {
7070

7171
// enable touch gestures
7272
mChart.setTouchEnabled(true);
73+
74+
mChart.setDragDecelerationFrictionCoef(0.95f);
7375

7476
// enable scaling and dragging
7577
mChart.setDragEnabled(true);

‎MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java‎

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,6 @@ public abstract class BarLineChartBase<T extends BarLineScatterCandleData<? exte
9595

9696
protected boolean mDrawBorders = false;
9797

98-
/** If set to true, chart continues to scroll after touch up */
99-
private boolean mDragDecelarationEnabled = true;
100-
101-
/**
102-
* Decelaration friction coefficient in [0 ; 1] interval, higher values indicate that
103-
* speed will decrease slowly, for example if it set to 0, it will stop immediately.
104-
* 1 is an invalid value, and will be converted to 0 automatically.
105-
*/
106-
private float mDragDecelarationFrictionCoef = 0.9f;
107-
10898
/** the listener for user drawing on the chart */
10999
protected OnDrawListener mDrawListener;
110100

@@ -553,7 +543,7 @@ public boolean onTouchEvent(MotionEvent event) {
553543
public void computeScroll() {
554544

555545
if (mListener instanceof BarLineChartTouchListener)
556-
((BarLineChartTouchListener)mListener).computeScroll();
546+
((BarLineChartTouchListener<?>) mListener).computeScroll();
557547
}
558548

559549
/**
@@ -985,46 +975,6 @@ public void setBorderColor(int color) {
985975
mBorderPaint.setColor(color);
986976
}
987977

988-
/**
989-
* If set to true, chart continues to scroll after touch up
990-
*
991-
* default: true
992-
*/
993-
public boolean isDragDecelarationEnabled() {
994-
return mDragDecelarationEnabled;
995-
}
996-
997-
/**
998-
* If set to true, chart continues to scroll after touch up
999-
*
1000-
* @param enabled
1001-
*/
1002-
public void setDragDecelarationEnabled(boolean enabled) {
1003-
mDragDecelarationEnabled = enabled;
1004-
}
1005-
1006-
/**
1007-
* Returns drag deceleration friction coefficient
1008-
* @return
1009-
*/
1010-
public float getDragDecelarationFrictionCoef() {
1011-
return mDragDecelarationFrictionCoef;
1012-
}
1013-
1014-
/**+
1015-
* Decelaration friction coefficient in [0 ; 1] interval, higher values indicate that
1016-
* speed will decrease slowly, for example if it set to 0, it will stop immediately.
1017-
* 1 is an invalid value, and will be converted to 0 automatically.
1018-
*
1019-
* @param newValue
1020-
*/
1021-
public void setDragDecelarationFrictionCoef(float newValue) {
1022-
if (newValue < 0.f || newValue >= 1.f)
1023-
newValue = 0.f;
1024-
1025-
mDragDecelarationFrictionCoef = newValue;
1026-
}
1027-
1028978
/**
1029979
* Returns the Highlight object (contains x-index and DataSet index) of the
1030980
* selected value at the given touch point inside the Line-, Scatter-, or

‎MPChartLib/src/com/github/mikephil/charting/charts/Chart.java‎

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,20 @@ public abstract class Chart<T extends ChartData<? extends DataSet<? extends Entr
7272
*/
7373
protected T mData = null;
7474

75+
/** If set to true, chart continues to scroll after touch up */
76+
private boolean mDragDecelerationEnabled = true;
77+
78+
/**
79+
* Decelaration friction coefficient in [0 ; 1] interval, higher values
80+
* indicate that speed will decrease slowly, for example if it set to 0, it
81+
* will stop immediately, if set to 1, it will scroll with constant speed,
82+
* until the last point
83+
*/
84+
private float mDragDecelerationFrictionCoef = 0.9f;
85+
7586
/** default value-formatter, number of digits depends on provided chart-data */
7687
protected ValueFormatter mDefaultFormatter;
7788

78-
/** the canvas that is used for drawing on the bitmap */
79-
// protected Canvas mDrawCanvas;
80-
8189
/**
8290
* paint object used for drawing the description text in the bottom right
8391
* corner of the chart
@@ -181,7 +189,7 @@ public void onAnimationUpdate(ValueAnimator animation) {
181189
});
182190

183191
// initialize the utils
184-
Utils.init(getContext().getResources());
192+
Utils.init(getContext());
185193

186194
mDefaultFormatter = new DefaultValueFormatter(1);
187195

@@ -618,6 +626,50 @@ public ChartAnimator getAnimator() {
618626
return mAnimator;
619627
}
620628

629+
/**
630+
* If set to true, chart continues to scroll after touch up default: true
631+
*/
632+
public boolean isDragDecelerationEnabled() {
633+
return mDragDecelerationEnabled;
634+
}
635+
636+
/**
637+
* If set to true, chart continues to scroll after touch up. Default: true.
638+
*
639+
* @param enabled
640+
*/
641+
public void setDragDecelerationEnabled(boolean enabled) {
642+
mDragDecelerationEnabled = enabled;
643+
}
644+
645+
/**
646+
* Returns drag deceleration friction coefficient
647+
*
648+
* @return
649+
*/
650+
public float getDragDecelerationFrictionCoef() {
651+
return mDragDecelerationFrictionCoef;
652+
}
653+
654+
/**
655+
* Deceleration friction coefficient in [0 ; 1] interval, higher values
656+
* indicate that speed will decrease slowly, for example if it set to 0, it
657+
* will stop immediately. 1 is an invalid value, and will be converted to 0.999f
658+
* automatically.
659+
*
660+
* @param newValue
661+
*/
662+
public void setDragDecelerationFrictionCoef(float newValue) {
663+
664+
if (newValue < 0.f)
665+
newValue = 0.f;
666+
667+
if(newValue >= 1f)
668+
newValue = 0.999f;
669+
670+
mDragDecelerationFrictionCoef = newValue;
671+
}
672+
621673
/**
622674
* ################ ################ ################ ################
623675
* ANIMATIONS ONLY WORK FOR API LEVEL 11 (Android 3.0.x) AND HIGHER.

‎MPChartLib/src/com/github/mikephil/charting/charts/PieRadarChartBase.java‎

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,6 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends DataSet<?
4141
/** flag that indicates if rotation is enabled or not */
4242
protected boolean mRotateEnabled = true;
4343

44-
/** If set to true, chart continues to scroll after touch up */
45-
private boolean mDragDecelarationEnabled = true;
46-
47-
/**
48-
* Decelaration friction coefficient in [0 ; 1] interval, higher values indicate that
49-
* speed will decrease slowly, for example if it set to 0, it will stop immediately,
50-
* if set to 1, it will scroll with constant speed, until the last point
51-
*/
52-
private float mDragDecelarationFrictionCoef = 0.9f;
53-
5444
/** the pie- and radarchart touchlistener */
5545
protected OnTouchListener mListener;
5646

@@ -381,46 +371,6 @@ public boolean isRotationEnabled() {
381371
return mRotateEnabled;
382372
}
383373

384-
/**
385-
* If set to true, chart continues to scroll after touch up
386-
*
387-
* default: true
388-
*/
389-
public boolean isDragDecelarationEnabled() {
390-
return mDragDecelarationEnabled;
391-
}
392-
393-
/**
394-
* If set to true, chart continues to scroll after touch up
395-
*
396-
* @param enabled
397-
*/
398-
public void setDragDecelarationEnabled(boolean enabled) {
399-
mDragDecelarationEnabled = enabled;
400-
}
401-
402-
/**
403-
* Returns drag deceleration friction coefficient
404-
* @return
405-
*/
406-
public float getDragDecelarationFrictionCoef() {
407-
return mDragDecelarationFrictionCoef;
408-
}
409-
410-
/**+
411-
* Decelaration friction coefficient in [0 ; 1] interval, higher values indicate that
412-
* speed will decrease slowly, for example if it set to 0, it will stop immediately,
413-
* if set to 1, it will scroll with constant speed, until the last point
414-
*
415-
* @param newValue
416-
*/
417-
public void setDragDecelarationFrictionCoef(float newValue) {
418-
if (newValue < 0.f || newValue >= 1.f)
419-
newValue = 0.f;
420-
421-
mDragDecelarationFrictionCoef = newValue;
422-
}
423-
424374
/**
425375
* returns the diameter of the pie- or radar-chart
426376
*

‎MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.annotation.SuppressLint;
55
import android.graphics.Matrix;
66
import android.graphics.PointF;
7-
import android.os.Build;
87
import android.util.Log;
98
import android.view.GestureDetector;
109
import android.view.GestureDetector.SimpleOnGestureListener;
@@ -15,6 +14,7 @@
1514
import android.view.animation.AnimationUtils;
1615

1716
import com.github.mikephil.charting.charts.BarLineChartBase;
17+
import com.github.mikephil.charting.charts.Chart;
1818
import com.github.mikephil.charting.charts.HorizontalBarChart;
1919
import com.github.mikephil.charting.data.BarLineScatterCandleData;
2020
import com.github.mikephil.charting.data.BarLineScatterCandleDataSet;
@@ -198,7 +198,7 @@ public boolean onTouch(View v, MotionEvent event) {
198198
if (Math.abs(velocityX) > Utils.getMinimumFlingVelocity() ||
199199
Math.abs(velocityY) > Utils.getMinimumFlingVelocity()) {
200200

201-
if (mTouchMode == DRAG && mChart.isDragDecelarationEnabled()) {
201+
if (mTouchMode == DRAG && mChart.isDragDecelerationEnabled()) {
202202

203203
stopDeceleration();
204204

@@ -587,8 +587,8 @@ public void computeScroll() {
587587

588588
final long currentTime = AnimationUtils.currentAnimationTimeMillis();
589589

590-
mDecelarationVelocity.x *= mChart.getDragDecelarationFrictionCoef();
591-
mDecelarationVelocity.y *= mChart.getDragDecelarationFrictionCoef();
590+
mDecelarationVelocity.x *= mChart.getDragDecelerationFrictionCoef();
591+
mDecelarationVelocity.y *= mChart.getDragDecelerationFrictionCoef();
592592

593593
final float timeInterval = (float)(currentTime - mDecelarationLastTime) / 1000.f;
594594

@@ -600,6 +600,7 @@ public void computeScroll() {
600600

601601
MotionEvent event = MotionEvent.obtain(currentTime, currentTime, MotionEvent.ACTION_MOVE, mDecelarationCurrentPoint.x, mDecelarationCurrentPoint.y, 0);
602602
performDrag(event);
603+
event.recycle();
603604
mMatrix = mChart.getViewPortHandler().refresh(mMatrix, mChart, false);
604605

605606
mDecelarationLastTime = currentTime;

‎MPChartLib/src/com/github/mikephil/charting/listener/PieRadarChartTouchListener.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public class PieRadarChartTouchListener extends SimpleOnGestureListener implemen
4242
private VelocityTracker mVelocityTracker;
4343

4444
private long mDecelarationLastTime = 0;
45-
private PointF mDecelarationCurrentPoint = new PointF();
4645
private float mDecelarationAngularVelocity = 0.f;
4746

4847
public PieRadarChartTouchListener(PieRadarChartBase<?> ctx) {
@@ -107,7 +106,7 @@ && distance(x, mTouchStartPoint.x, y, mTouchStartPoint.y)
107106
if (Math.abs(velocityX) > Utils.getMinimumFlingVelocity() ||
108107
Math.abs(velocityY) > Utils.getMinimumFlingVelocity()) {
109108

110-
if (mChart.isDragDecelarationEnabled()) {
109+
if (mChart.isDragDecelerationEnabled()) {
111110
stopDeceleration();
112111

113112
mDecelarationLastTime = AnimationUtils.currentAnimationTimeMillis();
@@ -266,7 +265,7 @@ public void computeScroll() {
266265

267266
final long currentTime = AnimationUtils.currentAnimationTimeMillis();
268267

269-
mDecelarationAngularVelocity *= mChart.getDragDecelarationFrictionCoef();
268+
mDecelarationAngularVelocity *= mChart.getDragDecelerationFrictionCoef();
270269

271270
final float timeInterval = (float)(currentTime - mDecelarationLastTime) / 1000.f;
272271

‎MPChartLib/src/com/github/mikephil/charting/utils/Utils.java‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
package com.github.mikephil.charting.utils;
33

4+
import android.annotation.SuppressLint;
45
import android.content.Context;
56
import android.content.res.Resources;
67
import android.graphics.Paint;
@@ -13,9 +14,7 @@
1314
import android.view.VelocityTracker;
1415
import android.view.View;
1516
import android.view.ViewConfiguration;
16-
1717
import com.github.mikephil.charting.components.YAxis.AxisDependency;
18-
1918
import java.text.DecimalFormat;
2019
import java.util.List;
2120

@@ -40,10 +39,6 @@ public abstract class Utils {
4039
*/
4140
public static void init(Context context) {
4241

43-
Resources res = context.getResources();
44-
45-
mMetrics = res.getDisplayMetrics();
46-
4742
if (context == null) {
4843
//noinspection deprecation
4944
mMinimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity();
@@ -54,6 +49,9 @@ public static void init(Context context) {
5449
mMinimumFlingVelocity = viewConfiguration.getScaledMinimumFlingVelocity();
5550
mMaximumFlingVelocity = viewConfiguration.getScaledMaximumFlingVelocity();
5651
}
52+
53+
Resources res = context.getResources();
54+
mMetrics = res.getDisplayMetrics();
5755
}
5856

5957
/**
@@ -501,6 +499,7 @@ public static void velocityTrackerPointerUpCleanUpIfNecessary(MotionEvent ev, Ve
501499
*
502500
* @param view
503501
*/
502+
@SuppressLint("NewApi")
504503
public static void postInvalidateOnAnimation(View view) {
505504
if (Build.VERSION.SDK_INT >= 16)
506505
view.postInvalidateOnAnimation();

‎MPChartLib/src/com/github/mikephil/charting/utils/ViewPortHandler.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.graphics.Matrix;
55
import android.graphics.PointF;
66
import android.graphics.RectF;
7-
import android.util.Log;
87
import android.view.View;
98

109
public class ViewPortHandler {

0 commit comments

Comments
 (0)