forked from PhilJay/MPAndroidChart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewPortHandler.java
More file actions
497 lines (376 loc) · 11.9 KB
/
ViewPortHandler.java
File metadata and controls
497 lines (376 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package com.github.mikephil.charting.utils;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
import android.view.View;
public class ViewPortHandler {
/** matrix used for touch events */
protected final Matrix mMatrixTouch = new Matrix();
/** this rectangle defines the area in which graph values can be drawn */
protected RectF mContentRect = new RectF();
protected float mChartWidth = 0f;
protected float mChartHeight = 0f;
/** minimum scale value on the y-axis */
private float mMinScaleY = 1f;
/** minimum scale value on the x-axis */
private float mMinScaleX = 1f;
/** maximum scale value on the x-axis */
private float mMaxScaleX = Float.MAX_VALUE;
/** contains the current scale factor of the x-axis */
private float mScaleX = 1f;
/** contains the current scale factor of the y-axis */
private float mScaleY = 1f;
/** offset that allows the chart to be dragged over its bounds on the x-axis */
private float mTransOffsetX = 0f;
/** offset that allows the chart to be dragged over its bounds on the x-axis */
private float mTransOffsetY = 0f;
public ViewPortHandler() {
}
/**
* Sets the width and height of the chart.
*
* @param width
* @param height
*/
public void setChartDimens(float width, float height) {
float offsetLeft = this.offsetLeft();
float offsetTop = this.offsetTop();
float offsetRight = this.offsetRight();
float offsetBottom = this.offsetBottom();
mChartHeight = height;
mChartWidth = width;
restrainViewPort(offsetLeft, offsetTop, offsetRight, offsetBottom);
}
public boolean hasChartDimens() {
if (mChartHeight > 0 && mChartWidth > 0)
return true;
else
return false;
}
public void restrainViewPort(float offsetLeft, float offsetTop, float offsetRight,
float offsetBottom) {
mContentRect.set(offsetLeft, offsetTop, mChartWidth - offsetRight, mChartHeight
- offsetBottom);
}
public float offsetLeft() {
return mContentRect.left;
}
public float offsetRight() {
return mChartWidth - mContentRect.right;
}
public float offsetTop() {
return mContentRect.top;
}
public float offsetBottom() {
return mChartHeight - mContentRect.bottom;
}
public float contentTop() {
return mContentRect.top;
}
public float contentLeft() {
return mContentRect.left;
}
public float contentRight() {
return mContentRect.right;
}
public float contentBottom() {
return mContentRect.bottom;
}
public float contentWidth() {
return mContentRect.width();
}
public float contentHeight() {
return mContentRect.height();
}
public RectF getContentRect() {
return mContentRect;
}
public PointF getContentCenter() {
return new PointF(mContentRect.centerX(), mContentRect.centerY());
}
public float getChartHeight() {
return mChartHeight;
}
public float getChartWidth() {
return mChartWidth;
}
/**
* ################ ################ ################ ################
*/
/** CODE BELOW THIS RELATED TO SCALING AND GESTURES */
/**
* Zooms in by 1.4f, x and y are the coordinates (in pixels) of the zoom
* center.
*
* @param x
* @param y
*/
public Matrix zoomIn(float x, float y) {
Matrix save = new Matrix();
save.set(mMatrixTouch);
save.postScale(1.4f, 1.4f, x, y);
return save;
}
/**
* Zooms out by 0.7f, x and y are the coordinates (in pixels) of the zoom
* center.
*/
public Matrix zoomOut(float x, float y) {
Matrix save = new Matrix();
save.set(mMatrixTouch);
save.postScale(0.7f, 0.7f, x, y);
return save;
}
/**
* Zooms in or out by the given scale factor. x and y are the coordinates
* (in pixels) of the zoom center.
*
* @param scaleX if < 1f --> zoom out, if > 1f --> zoom in
* @param scaleY if < 1f --> zoom out, if > 1f --> zoom in
* @param x
* @param y
*/
public Matrix zoom(float scaleX, float scaleY, float x, float y) {
Matrix save = new Matrix();
save.set(mMatrixTouch);
// Log.i(LOG_TAG, "Zooming, x: " + x + ", y: " + y);
save.postScale(scaleX, scaleY, x, y);
return save;
}
/**
* Resets all zooming and dragging and makes the chart fit exactly it's
* bounds.
*/
public Matrix fitScreen() {
mMinScaleX = 1f;
mMinScaleY = 1f;
Matrix save = new Matrix();
save.set(mMatrixTouch);
float[] vals = new float[9];
save.getValues(vals);
// reset all translations and scaling
vals[Matrix.MTRANS_X] = 0f;
vals[Matrix.MTRANS_Y] = 0f;
vals[Matrix.MSCALE_X] = 1f;
vals[Matrix.MSCALE_Y] = 1f;
save.setValues(vals);
return save;
}
/**
* Centers the viewport around the specified position (x-index and y-value)
* in the chart. Centering the viewport outside the bounds of the chart is
* not possible. Makes most sense in combination with the
* setScaleMinima(...) method.
*
* @param transformedPts the position to center view viewport to
* @param view
* @return save
*/
public synchronized void centerViewPort(final float[] transformedPts, final View view) {
Matrix save = new Matrix();
save.set(mMatrixTouch);
final float x = transformedPts[0] - offsetLeft();
final float y = transformedPts[1] - offsetTop();
//Log.i("", "Moving view to x: " + x + ", y: " + y);
save.postTranslate(-x, -y);
refresh(save, view, true);
}
/**
* call this method to refresh the graph with a given matrix
*
* @param newMatrix
* @return
*/
public Matrix refresh(Matrix newMatrix, View chart, boolean invalidate) {
mMatrixTouch.set(newMatrix);
// make sure scale and translation are within their bounds
limitTransAndScale(mMatrixTouch, mContentRect);
if (invalidate)
chart.invalidate();
newMatrix.set(mMatrixTouch);
return newMatrix;
}
/**
* limits the maximum scale and X translation of the given matrix
*
* @param matrix
*/
public void limitTransAndScale(Matrix matrix, RectF content) {
float[] vals = new float[9];
matrix.getValues(vals);
float curTransX = vals[Matrix.MTRANS_X];
float curScaleX = vals[Matrix.MSCALE_X];
float curTransY = vals[Matrix.MTRANS_Y];
float curScaleY = vals[Matrix.MSCALE_Y];
// min scale-x is 1f, max is the max float
mScaleX = Math.min(Math.max(mMinScaleX, curScaleX), mMaxScaleX);
// min scale-y is 1f
mScaleY = Math.max(mMinScaleY, curScaleY);
float width = 0f;
float height = 0f;
if (content != null) {
width = content.width();
height = content.height();
}
float maxTransX = -width * (mScaleX - 1f);
float newTransX = Math.min(Math.max(curTransX, maxTransX - mTransOffsetX), mTransOffsetX);
// if(curScaleX < mMinScaleX) {
// newTransX = (-width * (mScaleX - 1f)) / 2f;
// }
float maxTransY = height * (mScaleY - 1f);
float newTransY = Math.max(Math.min(curTransY, maxTransY + mTransOffsetY), -mTransOffsetY);
// if(curScaleY < mMinScaleY) {
// newTransY = (height * (mScaleY - 1f)) / 2f;
// }
vals[Matrix.MTRANS_X] = newTransX;
vals[Matrix.MSCALE_X] = mScaleX;
vals[Matrix.MTRANS_Y] = newTransY;
vals[Matrix.MSCALE_Y] = mScaleY;
matrix.setValues(vals);
}
public void setMinimumScaleX(float xScale) {
if (xScale < 1f)
xScale = 1f;
mMinScaleX = xScale;
limitTransAndScale(mMatrixTouch, mContentRect);
}
public void setMaximumScaleX(float xScale) {
mMaxScaleX = xScale;
limitTransAndScale(mMatrixTouch, mContentRect);
}
public void setMinMaxScaleX(float minScaleX, float maxScaleX) {
if (minScaleX < 1f)
minScaleX = 1f;
mMinScaleX = minScaleX;
mMaxScaleX = maxScaleX;
limitTransAndScale(mMatrixTouch, mContentRect);
}
public void setMinimumScaleY(float yScale) {
if (yScale < 1f)
yScale = 1f;
mMinScaleY = yScale;
limitTransAndScale(mMatrixTouch, mContentRect);
}
/**
* Returns the charts-touch matrix used for translation and scale on touch.
*
* @return
*/
public Matrix getMatrixTouch() {
return mMatrixTouch;
}
/**
* ################ ################ ################ ################
*/
/** BELOW METHODS FOR BOUNDS CHECK */
public boolean isInBoundsX(float x) {
if (isInBoundsLeft(x) && isInBoundsRight(x))
return true;
else
return false;
}
public boolean isInBoundsY(float y) {
if (isInBoundsTop(y) && isInBoundsBottom(y))
return true;
else
return false;
}
public boolean isInBounds(float x, float y) {
if (isInBoundsX(x) && isInBoundsY(y))
return true;
else
return false;
}
public boolean isInBoundsLeft(float x) {
return mContentRect.left <= x ? true : false;
}
public boolean isInBoundsRight(float x) {
x = (float)((int)(x * 100.f)) / 100.f;
return mContentRect.right >= x ? true : false;
}
public boolean isInBoundsTop(float y) {
return mContentRect.top <= y ? true : false;
}
public boolean isInBoundsBottom(float y) {
y = (float)((int)(y * 100.f)) / 100.f;
return mContentRect.bottom >= y ? true : false;
}
/**
* returns the current x-scale factor
*/
public float getScaleX() {
return mScaleX;
}
/**
* returns the current y-scale factor
*/
public float getScaleY() {
return mScaleY;
}
/**
* if the chart is fully zoomed out, return true
*
* @return
*/
public boolean isFullyZoomedOut() {
if (isFullyZoomedOutX() && isFullyZoomedOutY())
return true;
else
return false;
}
/**
* Returns true if the chart is fully zoomed out on it's y-axis (vertical).
*
* @return
*/
public boolean isFullyZoomedOutY() {
if (mScaleY > mMinScaleY || mMinScaleY > 1f)
return false;
else
return true;
}
/**
* Returns true if the chart is fully zoomed out on it's x-axis
* (horizontal).
*
* @return
*/
public boolean isFullyZoomedOutX() {
if (mScaleX > mMinScaleX || mMinScaleX > 1f)
return false;
else
return true;
}
/**
* Set an offset in dp that allows the user to drag the chart over it's
* bounds on the x-axis.
*
* @param offset
*/
public void setDragOffsetX(float offset) {
mTransOffsetX = Utils.convertDpToPixel(offset);
}
/**
* Set an offset in dp that allows the user to drag the chart over it's
* bounds on the y-axis.
*
* @param offset
*/
public void setDragOffsetY(float offset) {
mTransOffsetY = Utils.convertDpToPixel(offset);
}
/**
* Returns true if both drag offsets (x and y) are zero or smaller.
*
* @return
*/
public boolean hasNoDragOffset() {
return mTransOffsetX <= 0 && mTransOffsetY <= 0 ? true : false;
}
public boolean canZoomOutMoreX() {
return (mScaleX > mMinScaleX);
}
public boolean canZoomInMoreX() {
return (mScaleX < mMaxScaleX);
}
}