forked from PhilJay/MPAndroidChart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarLineChartBase.java
More file actions
1540 lines (1252 loc) · 46.4 KB
/
BarLineChartBase.java
File metadata and controls
1540 lines (1252 loc) · 46.4 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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.github.mikephil.charting.charts;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.components.YAxis.AxisDependency;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarLineScatterCandleData;
import com.github.mikephil.charting.data.BarLineScatterCandleDataSet;
import com.github.mikephil.charting.data.DataSet;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.filter.Approximator;
import com.github.mikephil.charting.interfaces.BarLineScatterCandleDataProvider;
import com.github.mikephil.charting.jobs.MoveViewJob;
import com.github.mikephil.charting.listener.BarLineChartTouchListener;
import com.github.mikephil.charting.listener.OnDrawListener;
import com.github.mikephil.charting.renderer.XAxisRenderer;
import com.github.mikephil.charting.renderer.YAxisRenderer;
import com.github.mikephil.charting.utils.FillFormatter;
import com.github.mikephil.charting.utils.Highlight;
import com.github.mikephil.charting.utils.PointD;
import com.github.mikephil.charting.utils.SelectionDetail;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.Utils;
import java.util.ArrayList;
import java.util.List;
/**
* Base-class of LineChart, BarChart, ScatterChart and CandleStickChart.
*
* @author Philipp Jahoda
*/
@SuppressLint("RtlHardcoded")
public abstract class BarLineChartBase<T extends BarLineScatterCandleData<? extends BarLineScatterCandleDataSet<? extends Entry>>>
extends Chart<T> implements BarLineScatterCandleDataProvider {
/** the maximum number of entried to which values will be drawn */
protected int mMaxVisibleCount = 100;
/** flag that indicates if auto scaling on the y axis is enabled */
private boolean mAutoScaleMinMaxEnabled = false;
private Integer mAutoScaleLastLowestVisibleXIndex = null;
private Integer mAutoScaleLastHighestVisibleXIndex = null;
/**
* flag that indicates if pinch-zoom is enabled. if true, both x and y axis
* can be scaled with 2 fingers, if false, x and y axis can be scaled
* separately
*/
protected boolean mPinchZoomEnabled = false;
/** flag that indicates if double tap zoom is enabled or not */
protected boolean mDoubleTapToZoomEnabled = true;
/**
* flag that indicates if highlighting per dragging over a fully zoomed out
* chart is enabled
*/
protected boolean mHighlightPerDragEnabled = true;
/** if true, dragging is enabled for the chart */
private boolean mDragEnabled = true;
private boolean mScaleXEnabled = true;
private boolean mScaleYEnabled = true;
/** if true, data filtering is enabled */
protected boolean mFilterData = false;
/** paint object for the (by default) lightgrey background of the grid */
protected Paint mGridBackgroundPaint;
protected Paint mBorderPaint;
/** flag indicating if the grid background should be drawn or not */
protected boolean mDrawGridBackground = true;
protected boolean mDrawBorders = false;
/** the listener for user drawing on the chart */
protected OnDrawListener mDrawListener;
/**
* the object representing the labels on the y-axis, this object is prepared
* in the pepareYLabels() method
*/
protected YAxis mAxisLeft;
protected YAxis mAxisRight;
/** the object representing the labels on the x-axis */
protected XAxis mXAxis;
protected YAxisRenderer mAxisRendererLeft;
protected YAxisRenderer mAxisRendererRight;
protected Transformer mLeftAxisTransformer;
protected Transformer mRightAxisTransformer;
protected XAxisRenderer mXAxisRenderer;
// /** the approximator object used for data filtering */
// private Approximator mApproximator;
public BarLineChartBase(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public BarLineChartBase(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BarLineChartBase(Context context) {
super(context);
}
@Override
protected void init() {
super.init();
mAxisLeft = new YAxis(AxisDependency.LEFT);
mAxisRight = new YAxis(AxisDependency.RIGHT);
mXAxis = new XAxis();
mLeftAxisTransformer = new Transformer(mViewPortHandler);
mRightAxisTransformer = new Transformer(mViewPortHandler);
mAxisRendererLeft = new YAxisRenderer(mViewPortHandler, mAxisLeft, mLeftAxisTransformer);
mAxisRendererRight = new YAxisRenderer(mViewPortHandler, mAxisRight, mRightAxisTransformer);
mXAxisRenderer = new XAxisRenderer(mViewPortHandler, mXAxis, mLeftAxisTransformer);
mChartTouchListener = new BarLineChartTouchListener(this, mViewPortHandler.getMatrixTouch());
mGridBackgroundPaint = new Paint();
mGridBackgroundPaint.setStyle(Style.FILL);
// mGridBackgroundPaint.setColor(Color.WHITE);
mGridBackgroundPaint.setColor(Color.rgb(240, 240, 240)); // light
// grey
mBorderPaint = new Paint();
mBorderPaint.setStyle(Style.STROKE);
mBorderPaint.setColor(Color.BLACK);
mBorderPaint.setStrokeWidth(Utils.convertDpToPixel(1f));
}
// for performance tracking
private long totalTime = 0;
private long drawCycles = 0;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mDataNotSet)
return;
long starttime = System.currentTimeMillis();
calcModulus();
mXAxisRenderer.calcXBounds(this, mXAxis.mAxisLabelModulus);
mRenderer.calcXBounds(this, mXAxis.mAxisLabelModulus);
// execute all drawing commands
drawGridBackground(canvas);
if (mAxisLeft.isEnabled())
mAxisRendererLeft.computeAxis(mAxisLeft.mAxisMinimum, mAxisLeft.mAxisMaximum);
if (mAxisRight.isEnabled())
mAxisRendererRight.computeAxis(mAxisRight.mAxisMinimum, mAxisRight.mAxisMaximum);
mXAxisRenderer.renderAxisLine(canvas);
mAxisRendererLeft.renderAxisLine(canvas);
mAxisRendererRight.renderAxisLine(canvas);
if (mAutoScaleMinMaxEnabled) {
final int lowestVisibleXIndex = getLowestVisibleXIndex();
final int highestVisibleXIndex = getHighestVisibleXIndex();
if (mAutoScaleLastLowestVisibleXIndex == null ||
mAutoScaleLastLowestVisibleXIndex != lowestVisibleXIndex ||
mAutoScaleLastHighestVisibleXIndex == null ||
mAutoScaleLastHighestVisibleXIndex != highestVisibleXIndex) {
calcMinMax();
calculateOffsets();
mAutoScaleLastLowestVisibleXIndex = lowestVisibleXIndex;
mAutoScaleLastHighestVisibleXIndex = highestVisibleXIndex;
}
}
// make sure the graph values and grid cannot be drawn outside the
// content-rect
int clipRestoreCount = canvas.save();
canvas.clipRect(mViewPortHandler.getContentRect());
mXAxisRenderer.renderGridLines(canvas);
mAxisRendererLeft.renderGridLines(canvas);
mAxisRendererRight.renderGridLines(canvas);
if (mXAxis.isDrawLimitLinesBehindDataEnabled())
mXAxisRenderer.renderLimitLines(canvas);
if (mAxisLeft.isDrawLimitLinesBehindDataEnabled())
mAxisRendererLeft.renderLimitLines(canvas);
if (mAxisRight.isDrawLimitLinesBehindDataEnabled())
mAxisRendererRight.renderLimitLines(canvas);
mRenderer.drawData(canvas);
if (!mXAxis.isDrawLimitLinesBehindDataEnabled())
mXAxisRenderer.renderLimitLines(canvas);
if (!mAxisLeft.isDrawLimitLinesBehindDataEnabled())
mAxisRendererLeft.renderLimitLines(canvas);
if (!mAxisRight.isDrawLimitLinesBehindDataEnabled())
mAxisRendererRight.renderLimitLines(canvas);
// if highlighting is enabled
if (valuesToHighlight())
mRenderer.drawHighlighted(canvas, mIndicesToHightlight);
// Removes clipping rectangle
canvas.restoreToCount(clipRestoreCount);
mRenderer.drawExtras(canvas);
mXAxisRenderer.renderAxisLabels(canvas);
mAxisRendererLeft.renderAxisLabels(canvas);
mAxisRendererRight.renderAxisLabels(canvas);
mRenderer.drawValues(canvas);
mLegendRenderer.renderLegend(canvas);
drawMarkers(canvas);
drawDescription(canvas);
if (mLogEnabled) {
long drawtime = (System.currentTimeMillis() - starttime);
totalTime += drawtime;
drawCycles += 1;
long average = totalTime / drawCycles;
Log.i(LOG_TAG, "Drawtime: " + drawtime + " ms, average: " + average + " ms, cycles: "
+ drawCycles);
}
}
/**
* RESET PERFORMANCE TRACKING FIELDS
*/
public void resetTracking() {
totalTime = 0;
drawCycles = 0;
}
protected void prepareValuePxMatrix() {
if (mLogEnabled)
Log.i(LOG_TAG, "Preparing Value-Px Matrix, xmin: " + mXChartMin + ", xmax: "
+ mXChartMax + ", xdelta: " + mDeltaX);
mRightAxisTransformer.prepareMatrixValuePx(mXChartMin, mDeltaX, mAxisRight.mAxisRange,
mAxisRight.mAxisMinimum);
mLeftAxisTransformer.prepareMatrixValuePx(mXChartMin, mDeltaX, mAxisLeft.mAxisRange,
mAxisLeft.mAxisMinimum);
}
protected void prepareOffsetMatrix() {
mRightAxisTransformer.prepareMatrixOffset(mAxisRight.isInverted());
mLeftAxisTransformer.prepareMatrixOffset(mAxisLeft.isInverted());
}
@Override
public void notifyDataSetChanged() {
if (mDataNotSet) {
if (mLogEnabled)
Log.i(LOG_TAG, "Preparing... DATA NOT SET.");
return;
} else {
if (mLogEnabled)
Log.i(LOG_TAG, "Preparing...");
}
if (mRenderer != null)
mRenderer.initBuffers();
calcMinMax();
if (mAxisLeft.needsDefaultFormatter())
mAxisLeft.setValueFormatter(mDefaultFormatter);
if (mAxisRight.needsDefaultFormatter())
mAxisRight.setValueFormatter(mDefaultFormatter);
mAxisRendererLeft.computeAxis(mAxisLeft.mAxisMinimum, mAxisLeft.mAxisMaximum);
mAxisRendererRight.computeAxis(mAxisRight.mAxisMinimum, mAxisRight.mAxisMaximum);
mXAxisRenderer.computeAxis(mData.getXValAverageLength(), mData.getXVals());
if (mLegend != null)
mLegendRenderer.computeLegend(mData);
calculateOffsets();
}
@Override
protected void calcMinMax() {
if (mAutoScaleMinMaxEnabled)
mData.calcMinMax(getLowestVisibleXIndex(), getHighestVisibleXIndex());
float minLeft = mData.getYMin(AxisDependency.LEFT);
float maxLeft = mData.getYMax(AxisDependency.LEFT);
float minRight = mData.getYMin(AxisDependency.RIGHT);
float maxRight = mData.getYMax(AxisDependency.RIGHT);
float leftRange = Math.abs(maxLeft - (mAxisLeft.isStartAtZeroEnabled() ? 0 : minLeft));
float rightRange = Math.abs(maxRight - (mAxisRight.isStartAtZeroEnabled() ? 0 : minRight));
// in case all values are equal
if (leftRange == 0f) {
maxLeft = maxLeft + 1f;
if (!mAxisLeft.isStartAtZeroEnabled())
minLeft = minLeft - 1f;
}
if (rightRange == 0f) {
maxRight = maxRight + 1f;
if (!mAxisRight.isStartAtZeroEnabled())
minRight = minRight - 1f;
}
float topSpaceLeft = leftRange / 100f * mAxisLeft.getSpaceTop();
float topSpaceRight = rightRange / 100f * mAxisRight.getSpaceTop();
float bottomSpaceLeft = leftRange / 100f * mAxisLeft.getSpaceBottom();
float bottomSpaceRight = rightRange / 100f * mAxisRight.getSpaceBottom();
mXChartMax = mData.getXVals().size() - 1;
mDeltaX = Math.abs(mXChartMax - mXChartMin);
mAxisLeft.mAxisMaximum = !Float.isNaN(mAxisLeft.getAxisMaxValue()) ? mAxisLeft
.getAxisMaxValue() : maxLeft + topSpaceLeft;
mAxisRight.mAxisMaximum = !Float.isNaN(mAxisRight.getAxisMaxValue()) ? mAxisRight
.getAxisMaxValue() : maxRight + topSpaceRight;
mAxisLeft.mAxisMinimum = !Float.isNaN(mAxisLeft.getAxisMinValue()) ? mAxisLeft
.getAxisMinValue() : minLeft - bottomSpaceLeft;
mAxisRight.mAxisMinimum = !Float.isNaN(mAxisRight.getAxisMinValue()) ? mAxisRight
.getAxisMinValue() : minRight - bottomSpaceRight;
// consider starting at zero (0)
if (mAxisLeft.isStartAtZeroEnabled())
mAxisLeft.mAxisMinimum = 0f;
if (mAxisRight.isStartAtZeroEnabled())
mAxisRight.mAxisMinimum = 0f;
mAxisLeft.mAxisRange = Math.abs(mAxisLeft.mAxisMaximum - mAxisLeft.mAxisMinimum);
mAxisRight.mAxisRange = Math.abs(mAxisRight.mAxisMaximum - mAxisRight.mAxisMinimum);
}
@Override
protected void calculateOffsets() {
if (!mCustomViewPortEnabled) {
float offsetLeft = 0f, offsetRight = 0f, offsetTop = 0f, offsetBottom = 0f;
// setup offsets for legend
if (mLegend != null && mLegend.isEnabled()) {
if (mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART
|| mLegend.getPosition() == LegendPosition.RIGHT_OF_CHART_CENTER) {
offsetRight += Math.min(mLegend.mNeededWidth, mViewPortHandler.getChartWidth()
* mLegend.getMaxSizePercent())
+ mLegend.getXOffset() * 2f;
} else if (mLegend.getPosition() == LegendPosition.LEFT_OF_CHART
|| mLegend.getPosition() == LegendPosition.LEFT_OF_CHART_CENTER) {
offsetLeft += Math.min(mLegend.mNeededWidth, mViewPortHandler.getChartWidth()
* mLegend.getMaxSizePercent())
+ mLegend.getXOffset() * 2f;
} else if (mLegend.getPosition() == LegendPosition.BELOW_CHART_LEFT
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_RIGHT
|| mLegend.getPosition() == LegendPosition.BELOW_CHART_CENTER) {
float yOffset = mLegend.mTextHeightMax; // It's
// possible
// that we do
// not need
// this offset
// anymore as
// it is
// available
// through the
// extraOffsets
offsetBottom += Math.min(mLegend.mNeededHeight + yOffset,
mViewPortHandler.getChartHeight() * mLegend.getMaxSizePercent());
}
}
// offsets for y-labels
if (mAxisLeft.needsOffset()) {
offsetLeft += mAxisLeft.getRequiredWidthSpace(mAxisRendererLeft
.getPaintAxisLabels());
}
if (mAxisRight.needsOffset()) {
offsetRight += mAxisRight.getRequiredWidthSpace(mAxisRendererRight
.getPaintAxisLabels());
}
if (mXAxis.isEnabled() && mXAxis.isDrawLabelsEnabled()) {
float xlabelheight = mXAxis.mLabelHeight * 2f;
// offsets for x-labels
if (mXAxis.getPosition() == XAxisPosition.BOTTOM) {
offsetBottom += xlabelheight;
} else if (mXAxis.getPosition() == XAxisPosition.TOP) {
offsetTop += xlabelheight;
} else if (mXAxis.getPosition() == XAxisPosition.BOTH_SIDED) {
offsetBottom += xlabelheight;
offsetTop += xlabelheight;
}
}
offsetTop += getExtraTopOffset();
offsetRight += getExtraRightOffset();
offsetBottom += getExtraBottomOffset();
offsetLeft += getExtraLeftOffset();
float min = Utils.convertDpToPixel(10f);
mViewPortHandler.restrainViewPort(Math.max(min, offsetLeft), Math.max(min, offsetTop),
Math.max(min, offsetRight), Math.max(min, offsetBottom));
if (mLogEnabled) {
Log.i(LOG_TAG, "offsetLeft: " + offsetLeft + ", offsetTop: " + offsetTop
+ ", offsetRight: " + offsetRight + ", offsetBottom: " + offsetBottom);
Log.i(LOG_TAG, "Content: " + mViewPortHandler.getContentRect().toString());
}
}
prepareOffsetMatrix();
prepareValuePxMatrix();
}
/**
* calculates the modulus for x-labels and grid
*/
protected void calcModulus() {
if (mXAxis == null || !mXAxis.isEnabled())
return;
if (!mXAxis.isAxisModulusCustom()) {
float[] values = new float[9];
mViewPortHandler.getMatrixTouch().getValues(values);
mXAxis.mAxisLabelModulus = (int) Math
.ceil((mData.getXValCount() * mXAxis.mLabelWidth)
/ (mViewPortHandler.contentWidth() * values[Matrix.MSCALE_X]));
}
if (mLogEnabled)
Log.i(LOG_TAG, "X-Axis modulus: " + mXAxis.mAxisLabelModulus + ", x-axis label width: "
+ mXAxis.mLabelWidth + ", content width: " + mViewPortHandler.contentWidth());
if (mXAxis.mAxisLabelModulus < 1)
mXAxis.mAxisLabelModulus = 1;
}
@Override
protected float[] getMarkerPosition(Entry e, int dataSetIndex) {
float xPos = e.getXIndex();
if (this instanceof BarChart) {
BarData bd = (BarData) mData;
float space = bd.getGroupSpace();
float j = mData.getDataSetByIndex(dataSetIndex)
.getEntryPosition(e);
float x = (j * (mData.getDataSetCount() - 1)) + dataSetIndex + space * j + space
/ 2f;
xPos += x;
}
// position of the marker depends on selected value index and value
float[] pts = new float[] {
xPos, e.getVal() * mAnimator.getPhaseY()
};
getTransformer(mData.getDataSetByIndex(dataSetIndex).getAxisDependency())
.pointValuesToPixel(pts);
return pts;
}
/**
* draws the grid background
*/
protected void drawGridBackground(Canvas c) {
if (mDrawGridBackground) {
// draw the grid background
c.drawRect(mViewPortHandler.getContentRect(), mGridBackgroundPaint);
}
if (mDrawBorders) {
c.drawRect(mViewPortHandler.getContentRect(), mBorderPaint);
}
}
/**
* Returns the Transformer class that contains all matrices and is
* responsible for transforming values into pixels on the screen and
* backwards.
*
* @return
*/
public Transformer getTransformer(AxisDependency which) {
if (which == AxisDependency.LEFT)
return mLeftAxisTransformer;
else
return mRightAxisTransformer;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (mChartTouchListener == null || mDataNotSet)
return false;
// check if touch gestures are enabled
if (!mTouchEnabled)
return false;
else
return mChartTouchListener.onTouch(this, event);
}
@Override
public void computeScroll() {
if (mChartTouchListener instanceof BarLineChartTouchListener)
((BarLineChartTouchListener) mChartTouchListener).computeScroll();
}
/**
* ################ ################ ################ ################
*/
/**
* CODE BELOW THIS RELATED TO SCALING AND GESTURES AND MODIFICATION OF THE
* VIEWPORT
*/
/**
* Zooms in by 1.4f, into the charts center. center.
*/
public void zoomIn() {
Matrix save = mViewPortHandler.zoomIn(getWidth() / 2f, -(getHeight() / 2f));
mViewPortHandler.refresh(save, this, true);
}
/**
* Zooms out by 0.7f, from the charts center. center.
*/
public void zoomOut() {
Matrix save = mViewPortHandler.zoomOut(getWidth() / 2f, -(getHeight() / 2f));
mViewPortHandler.refresh(save, this, true);
}
/**
* 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 void zoom(float scaleX, float scaleY, float x, float y) {
Matrix save = mViewPortHandler.zoom(scaleX, scaleY, x, -y);
mViewPortHandler.refresh(save, this, true);
}
/**
* Resets all zooming and dragging and makes the chart fit exactly it's
* bounds.
*/
public void fitScreen() {
Matrix save = mViewPortHandler.fitScreen();
mViewPortHandler.refresh(save, this, true);
}
/**
* Sets the minimum scale factor value to which can be zoomed out. 1f =
* fitScreen
*
* @param scaleX
* @param scaleY
*/
public void setScaleMinima(float scaleX, float scaleY) {
mViewPortHandler.setMinimumScaleX(scaleX);
mViewPortHandler.setMinimumScaleY(scaleY);
}
/**
* Sets the size of the area (range on the x-axis) that should be maximum
* visible at once (no further zooming out allowed). If this is e.g. set to
* 10, no more than 10 values on the x-axis can be viewed at once without
* scrolling.
*
* @param maxXRange The maximum visible range of x-values.
*/
public void setVisibleXRangeMaximum(float maxXRange) {
float xScale = mDeltaX / (maxXRange);
mViewPortHandler.setMinimumScaleX(xScale);
}
/**
* Sets the size of the area (range on the x-axis) that should be minimum
* visible at once (no further zooming in allowed). If this is e.g. set to
* 10, no more than 10 values on the x-axis can be viewed at once without
* scrolling.
*
* @param minXRange The minimum visible range of x-values.
*/
public void setVisibleXRangeMinimum(float minXRange) {
float xScale = mDeltaX / (minXRange);
mViewPortHandler.setMaximumScaleX(xScale);
}
/**
* Limits the maximum and minimum value count that can be visible by
* pinching and zooming. e.g. minRange=10, maxRange=100 no less than 10
* values and no more that 100 values can be viewed at once without
* scrolling
*
* @param minXRange
* @param maxXRange
*/
public void setVisibleXRange(float minXRange, float maxXRange) {
float maxScale = mDeltaX / minXRange;
float minScale = mDeltaX / maxXRange;
mViewPortHandler.setMinMaxScaleX(minScale, maxScale);
}
/**
* Sets the size of the area (range on the y-axis) that should be maximum
* visible at once.
*
* @param maxYRange the maximum visible range on the y-axis
* @param axis - the axis for which this limit should apply
*/
public void setVisibleYRangeMaximum(float maxYRange, AxisDependency axis) {
float yScale = getDeltaY(axis) / maxYRange;
mViewPortHandler.setMinimumScaleY(yScale);
}
/**
* Moves the left side of the current viewport to the specified x-index.
*
* @param xIndex
*/
public void moveViewToX(float xIndex) {
Runnable job = new MoveViewJob(mViewPortHandler, xIndex, 0f,
getTransformer(AxisDependency.LEFT), this);
if (mViewPortHandler.hasChartDimens()) {
post(job);
} else {
mJobs.add(job);
}
// float[] pts = new float[] {
// xIndex, 0f
// };
//
// getTransformer(AxisDependency.LEFT).pointValuesToPixel(pts);
//
// mViewPortHandler.centerViewPort(pts, this);
}
/**
* Centers the viewport to the specified y-value on the y-axis.
*
* @param yValue
* @param axis - which axis should be used as a reference for the y-axis
*/
public void moveViewToY(float yValue, AxisDependency axis) {
float valsInView = getDeltaY(axis) / mViewPortHandler.getScaleY();
Runnable job = new MoveViewJob(mViewPortHandler, 0f, yValue + valsInView / 2f,
getTransformer(axis), this);
if (mViewPortHandler.hasChartDimens()) {
post(job);
} else {
mJobs.add(job);
}
}
/**
* This will move the left side of the current viewport to the specified
* x-index on the x-axis, and center the viewport to the specified y-value
* on the y-axis.
*
* @param xIndex
* @param yValue
* @param axis - which axis should be used as a reference for the y-axis
*/
public void moveViewTo(float xIndex, float yValue, AxisDependency axis) {
float valsInView = getDeltaY(axis) / mViewPortHandler.getScaleY();
Runnable job = new MoveViewJob(mViewPortHandler, xIndex, yValue + valsInView / 2f,
getTransformer(axis), this);
if (mViewPortHandler.hasChartDimens()) {
post(job);
} else {
mJobs.add(job);
}
}
/**
* This will move the center of the current viewport to the specified
* x-index and y-value.
*
* @param xIndex
* @param yValue
* @param axis - which axis should be used as a reference for the y-axis
*/
public void centerViewTo(int xIndex, float yValue, AxisDependency axis) {
float valsInView = getDeltaY(axis) / mViewPortHandler.getScaleY();
float xsInView = getXAxis().getValues().size() / mViewPortHandler.getScaleX();
Runnable job = new MoveViewJob(mViewPortHandler,
xIndex - xsInView / 2f, yValue + valsInView / 2f,
getTransformer(axis), this);
if (mViewPortHandler.hasChartDimens()) {
post(job);
} else {
mJobs.add(job);
}
}
/** flag that indicates if a custom viewport offset has been set */
private boolean mCustomViewPortEnabled = false;
/**
* Sets custom offsets for the current ViewPort (the offsets on the sides of
* the actual chart window). Setting this will prevent the chart from
* automatically calculating it's offsets. Use resetViewPortOffsets() to
* undo this. ONLY USE THIS WHEN YOU KNOW WHAT YOU ARE DOING, else use
* setExtraOffsets(...).
*
* @param left
* @param top
* @param right
* @param bottom
*/
public void setViewPortOffsets(final float left, final float top,
final float right, final float bottom) {
mCustomViewPortEnabled = true;
post(new Runnable() {
@Override
public void run() {
mViewPortHandler.restrainViewPort(left, top, right, bottom);
prepareOffsetMatrix();
prepareValuePxMatrix();
}
});
}
/**
* Resets all custom offsets set via setViewPortOffsets(...) method. Allows
* the chart to again calculate all offsets automatically.
*/
public void resetViewPortOffsets() {
mCustomViewPortEnabled = false;
calculateOffsets();
}
/**
* ################ ################ ################ ################
*/
/** CODE BELOW IS GETTERS AND SETTERS */
/**
* Returns the delta-y value (y-value range) of the specified axis.
*
* @param axis
* @return
*/
public float getDeltaY(AxisDependency axis) {
if (axis == AxisDependency.LEFT)
return mAxisLeft.mAxisRange;
else
return mAxisRight.mAxisRange;
}
/**
* Sets the OnDrawListener
*
* @param drawListener
*/
public void setOnDrawListener(OnDrawListener drawListener) {
this.mDrawListener = drawListener;
}
/**
* Gets the OnDrawListener. May be null.
*
* @return
*/
public OnDrawListener getDrawListener() {
return mDrawListener;
}
/**
* Returns the position (in pixels) the provided Entry has inside the chart
* view or null, if the provided Entry is null.
*
* @param e
* @return
*/
public PointF getPosition(Entry e, AxisDependency axis) {
if (e == null)
return null;
float[] vals = new float[] {
e.getXIndex(), e.getVal()
};
getTransformer(axis).pointValuesToPixel(vals);
return new PointF(vals[0], vals[1]);
}
/**
* sets the number of maximum visible drawn values on the chart only active
* when setDrawValues() is enabled
*
* @param count
*/
public void setMaxVisibleValueCount(int count) {
this.mMaxVisibleCount = count;
}
public int getMaxVisibleCount() {
return mMaxVisibleCount;
}
/**
* Set this to true to allow highlighting per dragging over the chart
* surface when it is fully zoomed out. Default: true
*
* @param enabled
*/
public void setHighlightPerDragEnabled(boolean enabled) {
mHighlightPerDragEnabled = enabled;
}
public boolean isHighlightPerDragEnabled() {
return mHighlightPerDragEnabled;
}
/**
* Sets the color for the background of the chart-drawing area (everything
* behind the grid lines).
*
* @param color
*/
public void setGridBackgroundColor(int color) {
mGridBackgroundPaint.setColor(color);
}
/**
* Set this to true to enable dragging (moving the chart with the finger)
* for the chart (this does not effect scaling).
*
* @param enabled
*/
public void setDragEnabled(boolean enabled) {
this.mDragEnabled = enabled;
}
/**
* Returns true if dragging is enabled for the chart, false if not.
*
* @return
*/
public boolean isDragEnabled() {
return mDragEnabled;
}
/**
* Set this to true to enable scaling (zooming in and out by gesture) for
* the chart (this does not effect dragging) on both X- and Y-Axis.
*
* @param enabled
*/
public void setScaleEnabled(boolean enabled) {
this.mScaleXEnabled = enabled;
this.mScaleYEnabled = enabled;
}
public void setScaleXEnabled(boolean enabled) {
mScaleXEnabled = enabled;
}
public void setScaleYEnabled(boolean enabled) {
mScaleYEnabled = enabled;
}
public boolean isScaleXEnabled() {
return mScaleXEnabled;
}
public boolean isScaleYEnabled() {
return mScaleYEnabled;
}
/**
* Set this to true to enable zooming in by double-tap on the chart.
* Default: enabled
*
* @param enabled
*/
public void setDoubleTapToZoomEnabled(boolean enabled) {
mDoubleTapToZoomEnabled = enabled;
}
/**
* Returns true if zooming via double-tap is enabled false if not.
*
* @return
*/
public boolean isDoubleTapToZoomEnabled() {
return mDoubleTapToZoomEnabled;
}
/**
* set this to true to draw the grid background, false if not
*
* @param enabled
*/
public void setDrawGridBackground(boolean enabled) {
mDrawGridBackground = enabled;
}
/**