Skip to content

Commit

Permalink
Merge pull request #138 from AppDevNext/renzhenfeiMaster
Browse files Browse the repository at this point in the history
fix after call setMinMaxScaleX, when scaleX to bound value, chart will jump
  • Loading branch information
hannesa2 committed Aug 7, 2023
2 parents bf7ac56 + 95aad7d commit db20125
Showing 1 changed file with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public class BarLineChartTouchListener extends ChartTouchListener<BarLineChartBa
*/
private float mMinScalePointerDistance;


private final float[] matrixBuffer = new float[9];
private final Matrix tempMatrix = new Matrix();

/**
* Constructor with initialization parameters.
*
Expand Down Expand Up @@ -378,9 +382,8 @@ private void performZoom(MotionEvent event) {
float scaleY = (mChart.isScaleYEnabled()) ? scale : 1f;

if (canZoomMoreY || canZoomMoreX) {

mMatrix.set(mSavedMatrix);
mMatrix.postScale(scaleX, scaleY, t.x, t.y);
mMatrix.postScale(getLimitedScaleX(scaleX, t), getLimitedScaleY(scaleY, t), t.x, t.y);

if (l != null)
l.onChartScale(event, scaleX, scaleY);
Expand All @@ -399,9 +402,8 @@ private void performZoom(MotionEvent event) {
h.canZoomInMoreX();

if (canZoomMoreX) {

mMatrix.set(mSavedMatrix);
mMatrix.postScale(scaleX, 1f, t.x, t.y);
mMatrix.postScale(getLimitedScaleX(scaleX, t), 1f, t.x, t.y);

if (l != null)
l.onChartScale(event, scaleX, 1f);
Expand All @@ -420,9 +422,8 @@ private void performZoom(MotionEvent event) {
h.canZoomInMoreY();

if (canZoomMoreY) {

mMatrix.set(mSavedMatrix);
mMatrix.postScale(1f, scaleY, t.x, t.y);
mMatrix.postScale(1f, getLimitedScaleY(scaleY, t), t.x, t.y);

if (l != null)
l.onChartScale(event, 1f, scaleY);
Expand All @@ -434,6 +435,59 @@ private void performZoom(MotionEvent event) {
}
}

/**
* limit scaleX range
* @param scaleX
* @param t
* @return
*/
private float getLimitedScaleX(float scaleX, MPPointF t) {
ViewPortHandler h = mChart.getViewPortHandler();
tempMatrix.postScale(scaleX, 1f, t.x, t.y);

mSavedMatrix.getValues(matrixBuffer);
float lastScaleX = matrixBuffer[Matrix.MSCALE_X];

tempMatrix.getValues(matrixBuffer);
float calScaleX = matrixBuffer[Matrix.MSCALE_X];

float resultScaleX = scaleX;

if (calScaleX < h.getMinScaleX()) {
resultScaleX = h.getMinScaleX() / lastScaleX;
} else if (calScaleX > h.getMaxScaleX()) {
resultScaleX = h.getMaxScaleX() / lastScaleX;
}
return resultScaleX;
}

/**
* limit scaleY range
* @param scaleY
* @param t
* @return
*/
private float getLimitedScaleY(float scaleY, MPPointF t) {
ViewPortHandler h = mChart.getViewPortHandler();
tempMatrix.set(mSavedMatrix);
tempMatrix.postScale(1f, scaleY, t.x, t.y);

mSavedMatrix.getValues(matrixBuffer);
float lastScaleY = matrixBuffer[Matrix.MSCALE_Y];

tempMatrix.getValues(matrixBuffer);
float calScaleY = matrixBuffer[Matrix.MSCALE_Y];

float resultScaleY = scaleY;

if (calScaleY < h.getMinScaleY()) {
resultScaleY = h.getMinScaleY() / lastScaleY;
} else if (calScaleY > h.getMaxScaleY()) {
resultScaleY = h.getMaxScaleY() / lastScaleY;
}
return resultScaleY;
}

/**
* Highlights upon dragging, generates callbacks for the selection-listener.
*
Expand Down

0 comments on commit db20125

Please sign in to comment.