-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathBubbleChartActivity.kt
More file actions
223 lines (185 loc) · 7.81 KB
/
BubbleChartActivity.kt
File metadata and controls
223 lines (185 loc) · 7.81 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
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.content.res.ResourcesCompat
import androidx.core.net.toUri
import info.appdev.chartexample.DataTools.Companion.getValues
import info.appdev.chartexample.databinding.ActivityBubblechartBinding
import info.appdev.chartexample.notimportant.DemoBase
import info.appdev.charting.components.Legend
import info.appdev.charting.components.XAxis
import info.appdev.charting.data.BubbleData
import info.appdev.charting.data.BubbleDataSet
import info.appdev.charting.data.BubbleEntryFloat
import info.appdev.charting.data.EntryFloat
import info.appdev.charting.highlight.Highlight
import info.appdev.charting.interfaces.datasets.IBubbleDataSet
import info.appdev.charting.listener.OnChartValueSelectedListener
import info.appdev.charting.utils.ColorTemplate
import info.appdev.charting.utils.PointF
import timber.log.Timber
class BubbleChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener {
private lateinit var binding: ActivityBubblechartBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityBubblechartBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.seekBarX.setOnSeekBarChangeListener(this)
binding.seekBarY.setOnSeekBarChangeListener(this)
binding.chart1.description.isEnabled = false
binding.chart1.setOnChartValueSelectedListener(this)
binding.chart1.setDrawGridBackground(false)
binding.chart1.setTouchEnabled(true)
// enable scaling and dragging
binding.chart1.isDragEnabled = true
binding.chart1.setScaleEnabled(true)
binding.chart1.setMaxVisibleValueCount(200)
binding.chart1.isPinchZoom = true
binding.seekBarX.progress = 10
binding.seekBarY.progress = 50
binding.chart1.legend.apply {
verticalAlignment = Legend.LegendVerticalAlignment.TOP
horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
orientation = Legend.LegendOrientation.VERTICAL
setDrawInside(false)
typeface = tfLight
}
val yl = binding.chart1.axisLeft
yl.typeface = tfLight
yl.spaceTop = 30f
yl.spaceBottom = 30f
yl.isDrawZeroLine = false
binding.chart1.axisRight.isEnabled = false
val xl = binding.chart1.xAxis
xl.position = XAxis.XAxisPosition.BOTTOM
xl.typeface = tfLight
}
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
val count = binding.seekBarX.progress
val range = binding.seekBarY.progress
binding.tvXMax.text = count.toString()
binding.tvYMax.text = range.toString()
val values1 = ArrayList<BubbleEntryFloat>()
val values2 = ArrayList<BubbleEntryFloat>()
val values3 = ArrayList<BubbleEntryFloat>()
val sampleValues = getValues(100)
for (i in 0..<count) {
values1.add(
BubbleEntryFloat(
i.toFloat(),
(sampleValues[i + 1]!! * range).toFloat(),
(sampleValues[i]!!.toFloat() * range),
ResourcesCompat.getDrawable(resources, R.drawable.star, null)
)
)
values2.add(
BubbleEntryFloat(
i.toFloat(),
(sampleValues[i + 2]!! * range).toFloat(),
(sampleValues[i + 1]!!.toFloat() * range),
ResourcesCompat.getDrawable(resources, R.drawable.star, null)
)
)
values3.add(BubbleEntryFloat(i.toFloat(), (sampleValues[i]!! * range).toFloat(), (sampleValues[i + 2]!!.toFloat() * range)))
}
// create a dataset and give it a type
val set1 = BubbleDataSet(values1, "DS 1")
set1.isDrawIcons = false
set1.setColor(ColorTemplate.COLORFUL_COLORS[0], 130)
set1.isDrawValues = true
val set2 = BubbleDataSet(values2, "DS 2")
set2.isDrawIcons = false
set2.iconsOffset = PointF(0f, 15f)
set2.setColor(ColorTemplate.COLORFUL_COLORS[1], 130)
set2.isDrawValues = true
val set3 = BubbleDataSet(values3, "DS 3")
set3.setColor(ColorTemplate.COLORFUL_COLORS[2], 130)
set3.isDrawValues = true
val dataSets = ArrayList<IBubbleDataSet>()
dataSets.add(set1) // add the data sets
dataSets.add(set2)
dataSets.add(set3)
// create a data object with the data sets
val data = BubbleData(dataSets)
data.setDrawValues(false)
data.setValueTypeface(tfLight)
data.setValueTextSize(8f)
data.setValueTextColor(Color.WHITE)
data.setHighlightCircleWidth(1.5f)
binding.chart1.data = data
binding.chart1.invalidate()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.bubble, 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/BubbleChartActivity.kt".toUri()
startActivity(i)
}
R.id.actionToggleValues -> {
binding.chart1.bubbleData?.dataSets?.forEach {
it.isDrawValues = !it.isDrawValues
}
binding.chart1.invalidate()
}
R.id.actionToggleIcons -> {
binding.chart1.bubbleData?.dataSets?.forEach { set ->
set.isDrawIcons = !set.isDrawIcons
}
binding.chart1.invalidate()
}
R.id.actionToggleHighlight -> {
binding.chart1.bubbleData?.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.actionSave -> {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery()
} else {
requestStoragePermission(binding.chart1)
}
}
R.id.animateX -> {
binding.chart1.animateX(2000)
}
R.id.animateY -> {
binding.chart1.animateY(2000)
}
R.id.animateXY -> {
binding.chart1.animateXY(2000, 2000)
}
}
return true
}
override fun saveToGallery() {
saveToGallery(binding.chart1, "BubbleChartActivity")
}
override fun onValueSelected(entryFloat: EntryFloat, highlight: Highlight) {
Timber.i("Value: ${entryFloat.y}, xIndex: ${entryFloat.x}, DataSet index: ${highlight.dataSetIndex}")
}
override fun onNothingSelected() = Unit
override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit
override fun onStopTrackingTouch(seekBar: SeekBar?) = Unit
}