-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathBarChartActivity.kt
More file actions
297 lines (241 loc) · 10.9 KB
/
BarChartActivity.kt
File metadata and controls
297 lines (241 loc) · 10.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
package info.appdev.chartexample
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.RectF
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.content.res.ResourcesCompat
import androidx.core.net.toUri
import info.appdev.chartexample.DataTools.Companion.getValues
import info.appdev.chartexample.custom.XYMarkerView
import info.appdev.chartexample.databinding.ActivityBarchartBinding
import info.appdev.chartexample.formatter.DayAxisValueFormatter
import info.appdev.chartexample.formatter.MyAxisValueFormatter
import info.appdev.chartexample.notimportant.DemoBase
import info.appdev.charting.components.Legend
import info.appdev.charting.components.Legend.LegendForm
import info.appdev.charting.components.XAxis.XAxisPosition
import info.appdev.charting.components.YAxis.AxisDependency
import info.appdev.charting.components.YAxis.YAxisLabelPosition
import info.appdev.charting.data.BarData
import info.appdev.charting.data.BarDataSet
import info.appdev.charting.data.BarEntryFloat
import info.appdev.charting.data.EntryFloat
import info.appdev.charting.formatter.IAxisValueFormatter
import info.appdev.charting.highlight.Highlight
import info.appdev.charting.interfaces.datasets.IBarDataSet
import info.appdev.charting.listener.OnChartValueSelectedListener
import info.appdev.charting.utils.Fill
import info.appdev.charting.utils.PointF
import timber.log.Timber
class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener {
private lateinit var binding: ActivityBarchartBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityBarchartBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.seekBarY.setOnSeekBarChangeListener(this)
binding.seekBarX.setOnSeekBarChangeListener(this)
binding.chart1.setOnChartValueSelectedListener(this)
binding.chart1.setRoundedBarRadius(50f)
binding.chart1.isDrawMarkersEnabled = true
binding.chart1.isDrawBarShadow = false
binding.chart1.isDrawValueAboveBar = true
binding.chart1.description.isEnabled = false
// if more than 60 entries are displayed in the chart, no values will be
// drawn
binding.chart1.setMaxVisibleValueCount(60)
// scaling can now only be done on x- and y-axis separately
binding.chart1.isPinchZoom = false
binding.chart1.setDrawGridBackground(false)
// chart.setDrawYLabels(false);
val xAxisFormatter: IAxisValueFormatter = DayAxisValueFormatter(binding.chart1)
binding.chart1.xAxis.apply {
position = XAxisPosition.BOTTOM
typeface = tfLight
isDrawGridLines = false
granularity = 1f // only intervals of 1 day
labelCount = 7
valueFormatter = xAxisFormatter
}
val custom: IAxisValueFormatter = MyAxisValueFormatter()
binding.chart1.axisLeft.apply {
typeface = tfLight
setLabelCount(8, false)
valueFormatter = custom
spaceTop = 15f
axisMinimum = 0f// this replaces setStartAtZero(true)
setPosition(YAxisLabelPosition.OUTSIDE_CHART)
}
binding.chart1.axisRight.apply {
isDrawGridLines = false
typeface = tfLight
setLabelCount(8, false)
valueFormatter = custom
spaceTop = 15f
axisMinimum = 0f// this replaces setStartAtZero(true)
}
binding.chart1.legend.apply {
verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM
horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
orientation = Legend.LegendOrientation.HORIZONTAL
setDrawInside(false)
form = LegendForm.SQUARE
formSize = 9f
textSize = 11f
xEntrySpace = 4f
}
val mv = XYMarkerView(this, xAxisFormatter)
mv.chartView = binding.chart1 // For bounds control
binding.chart1.setMarker(mv) // Set the marker to the chart
// setting data
binding.seekBarY.progress = 50
binding.seekBarX.progress = 12
// chart.setDrawLegend(false);
}
private fun setData(count: Int, range: Float) {
val start = 1f
val values = ArrayList<BarEntryFloat>()
val sampleValues = getValues(100)
var i = start.toInt()
while (i < start + count) {
val `val` = (sampleValues[i]!!.toFloat() * (range + 1))
if (`val` * 100 < 25) {
values.add(BarEntryFloat(i.toFloat(), `val`, ResourcesCompat.getDrawable(resources, R.drawable.star, null)))
} else {
values.add(BarEntryFloat(i.toFloat(), `val`))
}
i++
}
val set1: BarDataSet
if (binding.chart1.barData != null &&
binding.chart1.barData!!.dataSetCount > 0
) {
set1 = binding.chart1.barData!!.getDataSetByIndex(0) as BarDataSet
set1.entries = values
binding.chart1.barData?.notifyDataChanged()
binding.chart1.notifyDataSetChanged()
} else {
set1 = BarDataSet(values, "The year 2017")
set1.isDrawIcons = false
val startColor1 = ContextCompat.getColor(this, android.R.color.holo_orange_light)
val startColor2 = ContextCompat.getColor(this, android.R.color.holo_blue_light)
val startColor3 = ContextCompat.getColor(this, android.R.color.holo_orange_light)
val startColor4 = ContextCompat.getColor(this, android.R.color.holo_green_light)
val startColor5 = ContextCompat.getColor(this, android.R.color.holo_red_light)
val endColor1 = ContextCompat.getColor(this, android.R.color.holo_blue_dark)
val endColor2 = ContextCompat.getColor(this, android.R.color.holo_purple)
val endColor3 = ContextCompat.getColor(this, android.R.color.holo_green_dark)
val endColor4 = ContextCompat.getColor(this, android.R.color.holo_red_dark)
val endColor5 = ContextCompat.getColor(this, android.R.color.holo_orange_dark)
val gradientFills: MutableList<Fill> = ArrayList()
gradientFills.add(Fill(startColor1, endColor1))
gradientFills.add(Fill(startColor2, endColor2))
gradientFills.add(Fill(startColor3, endColor3))
gradientFills.add(Fill(startColor4, endColor4))
gradientFills.add(Fill(startColor5, endColor5))
set1.fills = gradientFills
val dataSets = ArrayList<IBarDataSet>()
dataSets.add(set1)
val data = BarData(dataSets)
data.setValueTextSize(10f)
data.setValueTypeface(tfLight)
data.barWidth = 0.9f
binding.chart1.data = data
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.bar, 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/BarChartActivity.kt".toUri()
startActivity(i)
}
R.id.actionToggleValues -> {
binding.chart1.barData?.dataSets?.forEach {
it.isDrawValues = !it.isDrawValues
}
binding.chart1.invalidate()
}
R.id.actionToggleIcons -> {
binding.chart1.barData?.dataSets?.forEach { set ->
set.isDrawIcons = !set.isDrawIcons
}
binding.chart1.invalidate()
}
R.id.actionToggleHighlight -> {
binding.chart1.barData?.let {
it.isHighlight = !it.isHighlight
}
binding.chart1.invalidate()
}
R.id.actionTogglePinch -> {
binding.chart1.isPinchZoom = !binding.chart1.isPinchZoom
binding.chart1.invalidate()
}
R.id.actionToggleAutoScaleMinMax -> {
binding.chart1.isAutoScaleMinMax = !binding.chart1.isAutoScaleMinMax
binding.chart1.notifyDataSetChanged()
}
R.id.actionToggleBarBorders -> {
binding.chart1.barData?.dataSets?.map { it as BarDataSet }?.forEach { set ->
set.barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f
}
binding.chart1.invalidate()
}
R.id.animateX -> {
binding.chart1.animateX(2000)
}
R.id.animateY -> {
binding.chart1.animateY(2000)
}
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)
}
}
R.id.actionRotateXAxisLabelsBy45Deg -> {
binding.chart1.xAxis.labelRotationAngle = 45f
binding.chart1.notifyDataSetChanged()
binding.chart1.invalidate()
}
}
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(binding.seekBarX.progress, binding.seekBarY.progress.toFloat())
binding.chart1.invalidate()
}
override fun saveToGallery() {
saveToGallery(binding.chart1, "BarChartActivity")
}
override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit
override fun onStopTrackingTouch(seekBar: SeekBar?) = Unit
private val onValueSelectedRectF = RectF()
override fun onValueSelected(entryFloat: EntryFloat, highlight: Highlight) {
val bounds = onValueSelectedRectF
binding.chart1.getBarBounds(entryFloat as BarEntryFloat, bounds)
val position = binding.chart1.getPosition(entryFloat, AxisDependency.LEFT)
Timber.i("bounds $bounds")
Timber.i("position = $position")
Timber.i("x-index low: ${+binding.chart1.lowestVisibleX}, high: ${+binding.chart1.highestVisibleX}")
PointF.recycleInstance(position)
}
override fun onNothingSelected() = Unit
}