-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathChart.kt
More file actions
1291 lines (1104 loc) · 40.5 KB
/
Chart.kt
File metadata and controls
1291 lines (1104 loc) · 40.5 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 info.appdev.charting.charts
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Bitmap.CompressFormat
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Paint.Align
import android.graphics.RectF
import android.graphics.Typeface
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.view.accessibility.AccessibilityEvent
import androidx.core.graphics.createBitmap
import info.appdev.charting.animation.ChartAnimator
import info.appdev.charting.animation.Easing.EasingFunction
import info.appdev.charting.components.Description
import info.appdev.charting.components.IMarker
import info.appdev.charting.components.Legend
import info.appdev.charting.components.XAxis
import info.appdev.charting.data.ChartData
import info.appdev.charting.data.Entry
import info.appdev.charting.formatter.DefaultValueFormatter
import info.appdev.charting.formatter.IValueFormatter
import info.appdev.charting.highlight.Highlight
import info.appdev.charting.highlight.IHighlighter
import info.appdev.charting.interfaces.dataprovider.base.IBaseProvider
import info.appdev.charting.interfaces.datasets.IDataSet
import info.appdev.charting.listener.ChartTouchListener
import info.appdev.charting.listener.OnChartGestureListener
import info.appdev.charting.listener.OnChartValueSelectedListener
import info.appdev.charting.renderer.DataRenderer
import info.appdev.charting.renderer.LegendRenderer
import info.appdev.charting.utils.PointF
import info.appdev.charting.utils.PointF.Companion.getInstance
import info.appdev.charting.utils.SaveUtils.saveToGallery
import info.appdev.charting.utils.SaveUtils.saveToPath
import info.appdev.charting.utils.Utils
import info.appdev.charting.utils.ViewPortHandler
import info.appdev.charting.utils.convertDpToPixel
import info.appdev.charting.utils.getDecimals
import info.appdev.charting.utils.initUtils
import timber.log.Timber
import kotlin.math.abs
import kotlin.math.max
@Suppress("unused")
abstract class Chart<T : ChartData<out IDataSet<out Entry>>> : ViewGroup, IBaseProvider<T> {
/**
* Returns true if log-output is enabled for the chart, fals if not.
*/
var isLogging = false
/**
* object that holds all data that was originally set for the chart, before
* it was modified or any filtering algorithms had been applied
*/
protected var mData: T? = null
/**
* Set this to false to prevent values from being highlighted by tap gesture.
* Values can still be highlighted via drag or programmatically. Default: true
*/
var isHighlightPerTap = true
/**
* If set to true, chart continues to scroll after touch up
*/
var isDragDeceleration = true
/**
* Deceleration friction coefficient in [0 ; 1] interval, higher values
* indicate that speed will decrease slowly, for example if it set to 0, it
* will stop immediately. 1 is an invalid value, and will be converted to
* 0.999f automatically.
*/
private var mDragDecelerationFrictionCoef = 0.9f
/**
* default value-formatter, number of digits depends on provided chart-data
*/
protected var mDefaultValueFormatter: DefaultValueFormatter = DefaultValueFormatter(0)
/**
* paint object used for drawing the description text in the bottom right
* corner of the chart
*/
protected var mDescPaint = Paint(Paint.ANTI_ALIAS_FLAG)
/**
* paint object for drawing the information text when there are no values in
* the chart
*/
protected var mInfoPaint = Paint(Paint.ANTI_ALIAS_FLAG)
/**
* the object representing the labels on the x-axis
*/
protected var mXAxis: XAxis = XAxis()
/**
* if true, touch gestures are enabled on the chart
*/
protected var mTouchEnabled: Boolean = true
/**
* Returns the Description object of the chart that is responsible for holding all information related
* to the description text that is displayed in the bottom right corner of the chart (by default).
*/
var description = Description()
/**
* Returns the Legend object of the chart. This method can be used to get an
* instance of the legend in order to customize the automatically generated
* Legend.
*/
var legend: Legend = Legend()
protected set
/**
* listener that is called when a value on the chart is selected
*/
protected var mSelectionListener: OnChartValueSelectedListener? = null
protected var chartTouchListener: ChartTouchListener<*>? = null
/**
* text that is displayed when the chart is empty
*/
private var mNoDataText = "No chart data available."
/**
* Sets a gesture-listener for the chart for custom callbacks when executing
* gestures on the chart surface.
*/
var onChartGestureListener: OnChartGestureListener? = null
/**
* Returns the renderer object responsible for rendering / drawing the Legend.
*/
var legendRenderer: LegendRenderer? = null
protected set
/**
* object responsible for rendering the data
*/
protected var dataRenderer: DataRenderer? = null
var highlighter: IHighlighter? = null
/**
* Returns the ViewPortHandler of the chart that is responsible for the
* content area of the chart and its offsets and dimensions.
* Object that manages the bounds and drawing constraints of the chart
*/
var viewPortHandler: ViewPortHandler = ViewPortHandler()
protected set
/**
* object responsible for animations
*/
protected var mAnimator: ChartAnimator = ChartAnimator()
/**
* Extra offsets to be appended to the viewport
*/
private var mExtraTopOffset = 0f
private var mExtraRightOffset = 0f
private var mExtraBottomOffset = 0f
private var mExtraLeftOffset = 0f
/**
* Additional data on top of dynamically generated description. This can be set by the user.
*/
var accessibilitySummaryDescription: String? = ""
/**
* default constructor for initialization in code
*/
constructor(context: Context?) : super(context) {
init()
}
/**
* constructor for initialization in xml
*/
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
init()
}
/**
* even more awesome constructor
*/
constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) {
init()
}
init {
mXAxis = XAxis()
setWillNotDraw(false)
mAnimator = ChartAnimator { animation: ValueAnimator? ->
postInvalidate()
}
// initialize the utils
Utils.init(context)
context.initUtils()
this.description = Description()
this.legend = Legend()
mDescPaint = Paint(Paint.ANTI_ALIAS_FLAG)
mInfoPaint.color = Color.rgb(247, 189, 51) // orange
mInfoPaint.textAlign = Align.CENTER
mInfoPaint.textSize = 12f.convertDpToPixel()
if (this.isLogging) {
Timber.i("Chart.init()")
// enable being detected by ScreenReader
setFocusable(true)
}
}
/**
* initialize all paints and stuff
*/
protected open fun init() {
mMaxHighlightDistance = 500f.convertDpToPixel()
this.legendRenderer = LegendRenderer(this.viewPortHandler, this.legend)
}
/**
* Clears the chart from all data (sets it to null) and refreshes it (by
* calling invalidate()).
*/
fun clear() {
mData = null
mOffsetsCalculated = false
this.highlighted = null
chartTouchListener!!.setLastHighlighted(null)
invalidate()
}
/**
* Removes all DataSets (and thereby Entries) from the chart. Does not set the data object to null. Also refreshes the
* chart by calling invalidate().
*/
fun clearValues() {
mData!!.clearValues()
invalidate()
}
/**
* Returns true if the chart is empty (meaning it's data object is either null or contains no entries).
*/
val isEmpty: Boolean
get() {
return if (mData == null) {
true
} else {
mData!!.entryCount <= 0
}
}
/**
* Lets the chart know its underlying data has changed and performs all
* necessary recalculations. It is crucial that this method is called
* everytime data is changed dynamically. Not calling this method can lead
* to crashes or unexpected behaviour.
*/
abstract fun notifyDataSetChanged()
/**
* Calculates the offsets of the chart to the border depending on the
* position of an eventual legend or depending on the length of the y-axis
* and x-axis labels and their position
*/
protected abstract fun calculateOffsets()
/**
* Calculates the y-min and y-max value and the y-delta and x-delta value
*/
protected abstract fun calcMinMax()
/**
* Calculates the required number of digits for the values that might be
* drawn in the chart (if enabled), and creates the default-value-formatter
*/
protected fun setupDefaultFormatter(min: Float, max: Float) {
val reference: Float = if (mData == null || mData!!.entryCount < 2) {
max(abs(min), abs(max))
} else {
abs(max - min)
}
val digits = reference.getDecimals()
// setup the formatter with a new number of digits
mDefaultValueFormatter.setup(digits)
}
/**
* flag that indicates if offsets calculation has already been done or not
*/
private var mOffsetsCalculated = false
override fun onDraw(canvas: Canvas) {
// super.onDraw(canvas);
if (mData == null) {
val hasText = !TextUtils.isEmpty(mNoDataText)
if (hasText) {
val pt = this.center
when (mInfoPaint.textAlign) {
Align.LEFT -> {
pt.x = 0F
canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint)
}
Align.RIGHT -> {
pt.x *= 2.0f
canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint)
}
else -> canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint)
}
}
return
}
if (!mOffsetsCalculated) {
calculateOffsets()
mOffsetsCalculated = true
}
}
/**
* Draws the description text in the bottom right corner of the chart (per default)
*/
protected fun drawDescription(canvas: Canvas) {
// check if description should be drawn
if (description.isEnabled) {
val position = description.position
mDescPaint.typeface = description.typeface
mDescPaint.textSize = description.textSize
mDescPaint.color = description.textColor
mDescPaint.textAlign = description.textAlign
val x: Float
val y: Float
// if no position specified, draw on default position
if (position == null) {
x = width - viewPortHandler.offsetRight() - description.xOffset
y = height - viewPortHandler.offsetBottom() - description.yOffset
} else {
x = position.x
y = position.y
}
canvas.drawText(description.text!!, x, y, mDescPaint)
}
}
/**
* array of Highlight objects that reference the highlighted slices in the
* chart
*/
var highlighted: Array<Highlight>? = null
protected set
/**
* The maximum distance in dp away from an entry causing it to highlight.
*/
protected var mMaxHighlightDistance: Float = 0f
override var maxHighlightDistance: Float
get() = mMaxHighlightDistance
/**
* Sets the maximum distance in screen dp a touch can be away from an entry to cause it to get highlighted.
* Default: 500dp
*/
set(distDp) {
mMaxHighlightDistance = distDp.convertDpToPixel()
}
/**
* Returns true if there are values to highlight, false if there are no
* values to highlight. Checks if the highlight array is null, has a length
* of zero or if the first object is null.
*/
fun valuesToHighlight(): Boolean {
return this.highlighted != null && highlighted!!.isNotEmpty()
}
/**
* Sets the last highlighted value for the touch listener.
*/
protected fun setLastHighlighted(highs: Array<Highlight>?) {
if (highs == null || highs.isEmpty()) {
chartTouchListener!!.setLastHighlighted(null)
} else {
chartTouchListener!!.setLastHighlighted(highs[0])
}
}
/**
* Highlights the values at the given indices in the given DataSets. Provide
* null or an empty array to undo all highlighting. This should be used to
* programmatically highlight values.
* This method *will not* call the listener.
*/
fun highlightValues(highs: Array<Highlight>?) {
// set the indices to highlight
this.highlighted = highs
setLastHighlighted(highs)
invalidate()
}
fun highlightValues(highs: MutableList<Highlight>, markers: MutableList<IMarker>) {
require(highs.size == markers.size) { "Markers and highs must be mutually corresponding. High size = " + highs.size + " Markers size = " + markers.size }
setMarkers(markers)
highlightValues(highs.toTypedArray<Highlight>())
}
/**
* Highlights any y-value at the given x-value in the given DataSet.
* Provide -1 as the dataSetIndex to undo all highlighting.
* This method will call the listener.
* * @param x The x-value to highlight
*
* @param dataSetIndex The dataset index to search in
* @param dataIndex The data index to search in (only used in CombinedChartView currently)
*/
open fun getHighlightValue(x: Float, dataSetIndex: Int, dataIndex: Int) {
highlightValue(x, dataSetIndex, dataIndex, true)
}
/**
* Highlights any y-value at the given x-value in the given DataSet.
* Provide -1 as the dataSetIndex to undo all highlighting.
*
* @param x The x-value to highlight
* @param dataSetIndex The dataset index to search in
* @param dataIndex The data index to search in (only used in CombinedChartView currently)
* @param callListener Should the listener be called for this change
*/
fun highlightValue(x: Float, dataSetIndex: Int, dataIndex: Int = -1, callListener: Boolean = true) {
highlightValue(x, Float.NaN, dataSetIndex, dataIndex, callListener)
}
/**
* Highlights any y-value at the given x-value in the given DataSet.
* Provide -1 as the dataSetIndex to undo all highlighting.
*
* @param x The x-value to highlight
* @param dataSetIndex The dataset index to search in
* @param callListener Should the listener be called for this change
*/
fun highlightValue(x: Float, dataSetIndex: Int, callListener: Boolean) {
highlightValue(x, Float.NaN, dataSetIndex, -1, callListener)
}
/**
* Highlights the value at the given x-value and y-value in the given DataSet.
* Provide -1 as the dataSetIndex to undo all highlighting.
* This method will call the listener.
*
* @param x The x-value to highlight
* @param y The y-value to highlight. Supply `NaN` for "any"
* @param dataSetIndex The dataset index to search in
* @param dataIndex The data index to search in (only used in CombinedChartView currently)
*/
fun highlightValue(x: Float, y: Float, dataSetIndex: Int, dataIndex: Int = -1, callListener: Boolean = true) {
if (dataSetIndex < 0 || dataSetIndex >= mData!!.dataSetCount) {
highlightValue(null, callListener)
} else {
highlightValue(Highlight(x, y, dataSetIndex, dataIndex), callListener)
}
}
/**
* Highlights any y-value at the given x-value in the given DataSet.
* Provide -1 as the dataSetIndex to undo all highlighting.
*
* @param x The x-value to highlight
* @param y The y-value to highlight. Supply `NaN` for "any"
* @param dataSetIndex The dataset index to search in
* @param callListener Should the listener be called for this change
*/
fun highlightValue(x: Float, y: Float, dataSetIndex: Int, callListener: Boolean) {
highlightValue(x, y, dataSetIndex, -1, callListener)
}
/**
* Highlights the values represented by the provided Highlight object
* This method *will not* call the listener.
*
* @param highlight contains information about which entry should be highlighted
*/
fun highlightValue(highlight: Highlight?) {
highlightValue(highlight, false)
}
/**
* Highlights the value selected by touch gesture. Unlike
* highlightValues(...), this generates a callback to the
* OnChartValueSelectedListener.
*
* @param high - the highlight object
* @param callListener - call the listener
*/
fun highlightValue(high: Highlight?, callListener: Boolean) {
var high = high
var entry: Entry? = null
if (high == null) {
this.highlighted = null
} else {
if (this.isLogging) {
Timber.i("Highlighted: $high")
}
entry = mData!!.getEntryForHighlight(high)
if (entry == null) {
this.highlighted = null
high = null
} else {
// set the indices to highlight
this.highlighted = arrayOf(high)
}
}
setLastHighlighted(this.highlighted)
if (callListener && mSelectionListener != null) {
if (!valuesToHighlight()) {
mSelectionListener!!.onNothingSelected()
} else {
// notify the listener
mSelectionListener!!.onValueSelected(entry!!, high!!)
}
}
// redraw the chart
invalidate()
}
/**
* Returns the Highlight object (contains x-index and DataSet index) of the
* selected value at the given touch point inside the Line-, Scatter-, or
* CandleStick-Chart.
*/
open fun getHighlightByTouchPoint(x: Float, y: Float): Highlight? {
if (mData == null) {
Timber.e("Can't select by touch. No data set.")
return null
} else {
return this.highlighter!!.getHighlight(x, y)
}
}
/**
* Set a new (e.g. custom) ChartTouchListener NOTE: make sure to
* setTouchEnabled(true); if you need touch gestures on the chart
*/
var onTouchListener: ChartTouchListener<*>
get() = chartTouchListener!!
set(touchListener) {
this.chartTouchListener = touchListener
}
/**
* returns true if drawing the marker is enabled when tapping on values
* (use the setMarker(IMarker marker) method to specify a marker)
*/
var isDrawMarkersEnabled: Boolean = true
/**
* the view that represents the marker
*/
var marker: MutableList<IMarker> = ArrayList()
protected set
/**
* draws all MarkerViews on the highlighted positions
*/
protected open fun drawMarkers(canvas: Canvas) {
// if there is no marker view or drawing marker is disabled
if (!this.isDrawMarkersEnabled || !valuesToHighlight()) {
return
}
for (i in highlighted!!.indices) {
val highlight = this.highlighted!![i]
// When changing data sets and calling animation functions, sometimes an erroneous highlight is generated
// on the dataset that is removed. Null check to prevent crash
val dataset: IDataSet<*>? = mData!!.getDataSetByIndex(highlight.dataSetIndex)
if (dataset == null || !dataset.isVisible) {
continue
}
val e = mData!!.getEntryForHighlight(highlight) ?: continue
// make sure entry not null before using it
// Cast to non-star-projected type to allow calling getEntryIndex
@Suppress("UNCHECKED_CAST")
val set = dataset as IDataSet<Entry>
val entryIndex = set.getEntryIndex(e)
if (entryIndex > set.entryCount * mAnimator.phaseX) {
continue
}
val pos = getMarkerPosition(highlight)
// check bounds
if (!viewPortHandler.isInBounds(pos[0], pos[1])) {
continue
}
// callbacks to update the content
if (!marker.isEmpty()) {
val markerIndex = i % marker.size
val markerItem = marker[markerIndex]
markerItem.refreshContent(e, highlight)
// draw the marker
markerItem.draw(canvas, pos[0], pos[1])
}
}
}
/**
* Returns the actual position in pixels of the MarkerView for the given
* Highlight object.
*/
protected open fun getMarkerPosition(high: Highlight): FloatArray {
return floatArrayOf(high.drawX, high.drawY)
}
/**
* Returns the animator responsible for animating chart values.
*/
val animator: ChartAnimator
get() = mAnimator
/**
* Deceleration friction coefficient in [0 ; 1] interval, higher values
* indicate that speed will decrease slowly, for example if it set to 0, it
* will stop immediately. 1 is an invalid value, and will be converted to
* 0.999f automatically.
*/
var dragDecelerationFrictionCoef: Float
get() = mDragDecelerationFrictionCoef
set(newValue) {
var newValue = newValue
if (newValue < 0f) {
newValue = 0f
}
if (newValue >= 1f) {
newValue = 0.999f
}
mDragDecelerationFrictionCoef = newValue
}
/**
* Animates the drawing / rendering of the chart on both x- and y-axis with
* the specified animation time. If animate(...) is called, no further
* calling of invalidate() is necessary to refresh the chart.
*
* @param easingX a custom easing function to be used on the animation phase
* @param easingY a custom easing function to be used on the animation phase
*/
fun animateXY(durationMillisX: Int, durationMillisY: Int, easingX: EasingFunction?, easingY: EasingFunction?) {
mAnimator.animateXY(durationMillisX, durationMillisY, easingX, easingY)
}
/**
* Animates the drawing / rendering of the chart on both x- and y-axis with
* the specified animation time. If animate(...) is called, no further
* calling of invalidate() is necessary to refresh the chart.
*
* @param easing a custom easing function to be used on the animation phase
*/
fun animateXY(durationMillisX: Int, durationMillisY: Int, easing: EasingFunction?) {
mAnimator.animateXY(durationMillisX, durationMillisY, easing)
}
/**
* Animates the rendering of the chart on the x-axis with the specified
* animation time. If animate(...) is called, no further calling of
* invalidate() is necessary to refresh the chart.
*
* @param easing a custom easing function to be used on the animation phase
*/
fun animateX(durationMillis: Int, easing: EasingFunction?) {
mAnimator.animateX(durationMillis, easing)
}
/**
* Animates the rendering of the chart on the y-axis with the specified
* animation time. If animate(...) is called, no further calling of
* invalidate() is necessary to refresh the chart.
*
* @param easing a custom easing function to be used on the animation phase
*/
fun animateY(durationMillis: Int, easing: EasingFunction?) {
mAnimator.animateY(durationMillis, easing)
}
/**
* Animates the rendering of the chart on the x-axis with the specified
* animation time. If animate(...) is called, no further calling of
* invalidate() is necessary to refresh the chart.
*/
fun animateX(durationMillis: Int) {
mAnimator.animateX(durationMillis)
}
/**
* Animates the rendering of the chart on the y-axis with the specified
* animation time. If animate(...) is called, no further calling of
* invalidate() is necessary to refresh the chart.
*/
fun animateY(durationMillis: Int) {
mAnimator.animateY(durationMillis)
}
/**
* Animates the drawing / rendering of the chart on both x- and y-axis with
* the specified animation time. If animate(...) is called, no further
* calling of invalidate() is necessary to refresh the chart.
*/
fun animateXY(durationMillisX: Int, durationMillisY: Int) {
mAnimator.animateXY(durationMillisX, durationMillisY)
}
/**
* Returns the object representing all x-labels, this method can be used to
* acquire the XAxis object and modify it (e.g. change the position of the
* labels, styling, etc.)
*/
open val xAxis: XAxis
get() = mXAxis
/**
* Returns the default IValueFormatter that has been determined by the chart
* considering the provided minimum and maximum values.
*/
override val defaultValueFormatter: IValueFormatter
get() = mDefaultValueFormatter
/**
* set a selection listener for the chart
*/
fun setOnChartValueSelectedListener(l: OnChartValueSelectedListener?) {
this.mSelectionListener = l
}
/**
* returns the current y-max value across all DataSets
*/
val yMax: Float
get() = mData!!.yMax
/**
* returns the current y-min value across all DataSets
*/
val yMin: Float
get() = mData!!.yMin
override val yChartMin: Float
get() = mData!!.yMin
override val yChartMax: Float
get() = mData!!.yMax
override val xChartMax: Float
get() = mXAxis.mAxisMaximum
override val xChartMin: Float
get() = mXAxis.mAxisMinimum
override val xRange: Float
get() = mXAxis.axisRange
/**
* Returns a recyclable PointF instance.
* Returns the center point of the chart (the whole View) in pixels.
*/
val center: PointF
get() = getInstance(width / 2f, height / 2f)
/**
* Returns a recyclable PointF instance.
* Returns the center of the chart taking offsets under consideration.
* (returns the center of the content rectangle)
*/
override val centerOffsets: PointF
get() = viewPortHandler.contentCenter
/**
* Sets extra offsets (around the chart view) to be appended to the
* auto-calculated offsets.
*/
fun setExtraOffsets(left: Float, top: Float, right: Float, bottom: Float) {
this.extraLeftOffset = left
this.extraTopOffset = top
this.extraRightOffset = right
this.extraBottomOffset = bottom
}
/**
* Set an extra offset to be appended to the viewport's top
*/
var extraTopOffset: Float
get() = mExtraTopOffset
set(offset) {
mExtraTopOffset = offset.convertDpToPixel()
}
/**
* Set an extra offset to be appended to the viewport's right
*/
var extraRightOffset: Float
get() = mExtraRightOffset
set(offset) {
mExtraRightOffset = offset.convertDpToPixel()
}
/**
* Set an extra offset to be appended to the viewport's bottom
*/
var extraBottomOffset: Float
get() = mExtraBottomOffset
set(offset) {
mExtraBottomOffset = offset.convertDpToPixel()
}
/**
* Set an extra offset to be appended to the viewport's left
*/
var extraLeftOffset: Float
get() = mExtraLeftOffset
set(offset) {
mExtraLeftOffset = offset.convertDpToPixel()
}
/**
* Sets the text that informs the user that there is no data available with
* which to draw the chart.
*/
fun setNoDataText(text: String) {
mNoDataText = text
}
/**
* Sets the color of the no data text.
*/
fun setNoDataTextColor(color: Int) {
mInfoPaint.color = color
}
/**
* Sets the typeface to be used for the no data text.
*/
fun setNoDataTextTypeface(tf: Typeface?) {
mInfoPaint.typeface = tf
}
/**
* alignment of the no data text
*/
fun setNoDataTextAlignment(align: Align?) {
mInfoPaint.textAlign = align
}
/**
* Set this to false to disable all gestures and touches on the chart,
* default: true
*/
fun setTouchEnabled(enabled: Boolean) {
this.mTouchEnabled = enabled
}
fun setMarkers(marker: MutableList<IMarker>) {
this.marker = marker
}
/**
* sets the marker that is displayed when a value is clicked on the chart
*/
fun setMarker(marker: IMarker) {
setMarkers(mutableListOf(marker))
}
@Deprecated("Use 'setMarker()' instead")
fun setMarkerView(iMarker: IMarker) {
setMarker(iMarker)
}
@get:Deprecated("Use 'marker' directly")
val markerView: MutableList<IMarker>
get() = this.marker
/**
* Returns the rectangle that defines the borders of the chart-value surface
* (into which the actual values are drawn).
*/
override val contentRect: RectF
get() = viewPortHandler.contentRect
/**
* disables intercept touch events
*/
fun disableScroll() {
val parent = getParent()
parent?.requestDisallowInterceptTouchEvent(true)
}
/**
* enables intercept touch events
*/
fun enableScroll() {
val parent = getParent()
parent?.requestDisallowInterceptTouchEvent(false)
}
/**
* set a new paint object for the specified parameter in the chart e.g.
* Chart.PAINT_VALUES
*
* @param p the new paint object
* @param which Chart.PAINT_VALUES, Chart.PAINT_GRID, Chart.PAINT_VALUES,
* ...
*/
open fun setPaint(p: Paint, which: Int) {
when (which) {
PAINT_INFO -> mInfoPaint = p
PAINT_DESCRIPTION -> mDescPaint = p
}
}
/**
* Returns the paint object associated with the provided constant.
*
* @param which e.g. Chart.PAINT_LEGEND_LABEL
*/
open fun getPaint(which: Int): Paint? {
return when (which) {
PAINT_INFO -> mInfoPaint
PAINT_DESCRIPTION -> mDescPaint
else -> null
}
}
@get:Deprecated("Use 'isDrawMarkersEnabled' directly")
val isDrawMarkerViewsEnabled: Boolean
get() = this.isDrawMarkersEnabled
@Deprecated("Use 'isDrawMarkersEnabled' directly", ReplaceWith("isDrawMarkersEnabled = value"))
fun setDrawMarkerViews(value: Boolean) {
this.isDrawMarkersEnabled = value
}
/**
* Set this to true to draw a user specified marker when tapping on
* chart values (use the setMarker(IMarker marker) method to specify a marker).
* Default: true
*/
@Deprecated("Use 'isDrawMarkersEnabled' directly")
fun setDrawMarkers(value: Boolean) {
this.isDrawMarkersEnabled = value
}
/**
* Data object for the chart. The data object contains all values and information needed for displaying.
*/