forked from PhilJay/MPAndroidChart
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathLineChartActivity.kt
More file actions
269 lines (224 loc) · 9.96 KB
/
LineChartActivity.kt
File metadata and controls
269 lines (224 loc) · 9.96 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
package info.appdev.chartexample
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import info.appdev.chartexample.DataTools.Companion.setData
import info.appdev.chartexample.custom.MyMarkerView
import info.appdev.chartexample.databinding.ActivityLinechartBinding
import info.appdev.chartexample.notimportant.DemoBase
import info.appdev.charting.animation.Easing
import info.appdev.charting.components.Legend.LegendForm
import info.appdev.charting.components.LimitLine
import info.appdev.charting.components.LimitLine.LimitLabelPosition
import info.appdev.charting.components.LimitRange
import info.appdev.charting.components.XAxis
import info.appdev.charting.data.Entry
import info.appdev.charting.data.LineDataSet
import info.appdev.charting.highlight.Highlight
import info.appdev.charting.listener.OnChartValueSelectedListener
import timber.log.Timber
/**
* Example of a heavily customized [info.appdev.charting.charts.LineChart] with limit lines, custom line shapes, etc.
*/
class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener {
private lateinit var binding: ActivityLinechartBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLinechartBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.seekBarX.setOnSeekBarChangeListener(this)
binding.seekBarY.max = 180
binding.seekBarY.setOnSeekBarChangeListener(this)
// background color
binding.chart1.setBackgroundColor(Color.WHITE)
// disable description text
binding.chart1.description.isEnabled = false
binding.chart1.legend.isEnabled = false
binding.chart1.xAxis.position = XAxis.XAxisPosition.BOTTOM
// enable touch gestures
binding.chart1.setTouchEnabled(true)
// set listeners
binding.chart1.setOnChartValueSelectedListener(this)
binding.chart1.setDrawGridBackground(false)
// create marker to display box when values are selected
val mv = MyMarkerView(this, R.layout.custom_marker_view)
// Set the marker to the chart
mv.chartView = binding.chart1
binding.chart1.marker.add(mv)
// enable scaling and dragging
binding.chart1.isDragEnabled = true
binding.chart1.setScaleEnabled(true)
// force pinch zoom along both axis
binding.chart1.setPinchZoom(true)
// vertical grid lines
binding.chart1.xAxis.enableGridDashedLine(10f, 10f, 0f)
// disable dual axis (only use LEFT axis)
binding.chart1.axisRight.isEnabled = false
// horizontal grid lines
binding.chart1.axisLeft.enableGridDashedLine(10f, 10f, 0f)
// axis range
binding.chart1.axisLeft.axisMaximum = 200f
binding.chart1.axisLeft.axisMinimum = -50f
val limitLineUpper = LimitLine(150f, "Upper Limit").apply {
lineWidth = 4f
enableDashedLine(10f, 10f, 0f)
labelPosition = LimitLabelPosition.RIGHT_TOP
textSize = 10f
typeface = tfRegular
lineColor = Color.GREEN
}
val limitLineLower = LimitLine(-30f, "Lower Limit").apply {
lineWidth = 4f
enableDashedLine(10f, 10f, 0f)
labelPosition = LimitLabelPosition.RIGHT_BOTTOM
textSize = 10f
typeface = tfRegular
lineColor = Color.GREEN
}
val limitRange = LimitRange(45f, 90f, "Middle Range").apply {
lineWidth = 2f
labelPosition = LimitLabelPosition.RIGHT_TOP
textSize = 10f
typeface = tfRegular
lineColor = Color.CYAN
rangeColor = Color.argb(30, 255, 235, 0)
}
val limitRangeLower = LimitRange(45f, 52f).apply {
rangeColor = Color.argb(30, 230, 0, 0)
}
// draw limit lines behind data instead of on top
binding.chart1.axisLeft.setDrawLimitLinesBehindData(true)
binding.chart1.xAxis.setDrawLimitLinesBehindData(true)
// add limit lines
binding.chart1.axisLeft.addLimitLine(limitLineUpper)
binding.chart1.axisLeft.addLimitLine(limitLineLower)
// binding.chart1.axisLeft.addLimitLine(llXAxis10)
// add limit range
binding.chart1.axisLeft.addLimitRange(limitRange)
binding.chart1.axisLeft.addLimitRange(limitRangeLower)
// add data
binding.seekBarX.progress = 45
binding.seekBarY.progress = 180
setData(this, binding.chart1, 45, 180f)
// draw points over time
binding.chart1.animateX(1500)
// get the legend (only possible after setting data)
binding.chart1.legend.form = LegendForm.LINE
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.line, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.viewGithub -> {
val i = Intent(Intent.ACTION_VIEW)
i.data =
"https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/info/appdev/chartexample/LineChartActivity1.java".toUri()
startActivity(i)
}
R.id.actionToggleValues -> {
binding.chart1.lineData.dataSets?.forEach { set ->
set.isDrawValues = !set.isDrawValues
}
binding.chart1.invalidate()
}
R.id.actionToggleIcons -> {
binding.chart1.data?.dataSets?.forEach { set ->
set.isDrawIcons = !set.isDrawIcons
}
binding.chart1.invalidate()
}
R.id.actionToggleHighlight -> {
binding.chart1.data?.let {
it.isHighlightEnabled = !it.isHighlightEnabled
binding.chart1.invalidate()
}
}
R.id.actionToggleFilled -> {
binding.chart1.data?.dataSets?.forEach { set ->
set.isDrawFilledEnabled = !set.isDrawFilledEnabled
binding.chart1.invalidate()
}
}
R.id.actionToggleCircles -> {
binding.chart1.data?.dataSets?.map { it as LineDataSet }?.forEach { set ->
set.isDrawCirclesEnabled = !set.isDrawCirclesEnabled
}
binding.chart1.invalidate()
}
R.id.actionToggleCubic -> {
binding.chart1.data?.dataSets?.map { it as LineDataSet }?.forEach { set ->
set.lineMode = if (set.lineMode == LineDataSet.Mode.CUBIC_BEZIER)
LineDataSet.Mode.LINEAR
else
LineDataSet.Mode.CUBIC_BEZIER
}
binding.chart1.invalidate()
}
R.id.actionToggleStepped -> {
binding.chart1.data?.dataSets?.map { it as LineDataSet }?.forEach { set ->
set.lineMode = if (set.lineMode == LineDataSet.Mode.STEPPED)
LineDataSet.Mode.LINEAR
else
LineDataSet.Mode.STEPPED
}
binding.chart1.invalidate()
}
R.id.actionToggleHorizontalCubic -> {
binding.chart1.data?.dataSets?.map { it as LineDataSet }?.forEach { set ->
set.lineMode = if (set.lineMode == LineDataSet.Mode.HORIZONTAL_BEZIER)
LineDataSet.Mode.LINEAR
else
LineDataSet.Mode.HORIZONTAL_BEZIER
}
binding.chart1.invalidate()
}
R.id.actionTogglePinch -> {
binding.chart1.setPinchZoom(!binding.chart1.isPinchZoomEnabled)
binding.chart1.invalidate()
}
R.id.actionToggleAutoScaleMinMax -> {
binding.chart1.isAutoScaleMinMaxEnabled = !binding.chart1.isAutoScaleMinMaxEnabled
binding.chart1.notifyDataSetChanged()
}
R.id.animateX -> binding.chart1.animateX(2000)
R.id.animateY -> binding.chart1.animateY(2000, Easing.easeInCubic)
R.id.animateXY -> binding.chart1.animateXY(2000, 2000)
R.id.actionSave -> {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery()
} else {
requestStoragePermission(binding.chart1)
}
}
}
return true
}
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
binding.tvXMax.text = binding.seekBarX.progress.toString()
binding.tvYMax.text = binding.seekBarY.progress.toString()
setData(this, binding.chart1, binding.seekBarX.progress, binding.seekBarY.progress.toFloat())
// redraw
binding.chart1.invalidate()
}
override fun saveToGallery() {
saveToGallery(binding.chart1, "LineChartActivity1")
}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onStopTrackingTouch(seekBar: SeekBar) {}
override fun onValueSelected(entry: Entry, highlight: Highlight) {
Timber.i(entry.toString())
Timber.i("LOW HIGH low:${binding.chart1.lowestVisibleX}, high:${binding.chart1.highestVisibleX}")
Timber.i("MIN MAX xMin:${binding.chart1.xChartMin}, xMax:${binding.chart1.xChartMax}, yMin:${binding.chart1.yChartMin}, yMax:${binding.chart1.yChartMax}")
}
override fun onNothingSelected() = Unit
}