-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCandleStickChartActivity.kt
More file actions
206 lines (163 loc) · 6.99 KB
/
CandleStickChartActivity.kt
File metadata and controls
206 lines (163 loc) · 6.99 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
package info.appdev.chartexample
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.graphics.Paint
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.ActivityCandlechartBinding
import info.appdev.chartexample.notimportant.DemoBase
import info.appdev.charting.components.XAxis.XAxisPosition
import info.appdev.charting.components.YAxis.AxisDependency
import info.appdev.charting.data.CandleData
import info.appdev.charting.data.CandleDataSet
import info.appdev.charting.data.CandleEntryFloat
class CandleStickChartActivity : DemoBase(), OnSeekBarChangeListener {
private lateinit var binding: ActivityCandlechartBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityCandlechartBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.seekBarX.setOnSeekBarChangeListener(this)
binding.seekBarY.setOnSeekBarChangeListener(this)
binding.chart1.setBackgroundColor(Color.WHITE)
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)
val xAxis = binding.chart1.xAxis
xAxis.position = XAxisPosition.BOTTOM
xAxis.isDrawGridLines = false
val leftAxis = binding.chart1.axisLeft
// leftAxis.setEnabled(false);
leftAxis.setLabelCount(7, false)
leftAxis.isDrawGridLines = false
leftAxis.isDrawAxisLine = false
val rightAxis = binding.chart1.axisRight
rightAxis.isEnabled = false
// rightAxis.setStartAtZero(false);
// setting data
binding.seekBarX.progress = 40
binding.seekBarY.progress = 100
binding.chart1.legend.isEnabled = false
}
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
val progress: Int = (binding.seekBarX.progress)
binding.tvXMax.text = progress.toString()
binding.tvYMax.text = binding.seekBarY.progress.toString()
binding.chart1.resetTracking()
val values = ArrayList<CandleEntryFloat>()
val sampleValues = getValues(100)
for (i in 0..<progress) {
val multi = (binding.seekBarY.progress + 1).toFloat()
val `val` = (sampleValues[i]!!.toFloat() * 40) + multi
val high = (sampleValues[i]!!.toFloat() * 9) + 8f
val low = (sampleValues[i]!!.toFloat() * 8) + 8f
val open = (sampleValues[i]!!.toFloat() * 6) + 1f
val close = (sampleValues[i]!!.toFloat() * 7) + 1f
val even = i % 2 == 0
values.add(
CandleEntryFloat(
i.toFloat(), `val` + high,
`val` - low,
if (even) `val` + open else `val` - open,
if (even) `val` - close else `val` + close,
ResourcesCompat.getDrawable(resources, R.drawable.star, null)
)
)
}
val set1 = CandleDataSet(values, "Data Set")
set1.isDrawIcons = false
set1.axisDependency = AxisDependency.LEFT
// set1.setColor(Color.rgb(80, 80, 80));
set1.shadowColor = Color.DKGRAY
set1.shadowWidth = 0.7f
set1.decreasingColor = Color.BLUE
set1.decreasingPaintStyle = Paint.Style.FILL
set1.increasingColor = Color.rgb(122, 242, 84)
set1.increasingPaintStyle = Paint.Style.STROKE
set1.neutralColor = Color.BLUE
//set1.setHighlightLineWidth(1f);
val data = CandleData(set1)
binding.chart1.data = data
binding.chart1.invalidate()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.candle, 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/CandleStickChartActivity.kt".toUri()
startActivity(i)
}
R.id.actionToggleValues -> {
binding.chart1.candleData?.dataSets?.forEach {
it.isDrawValues = !it.isDrawValues
}
binding.chart1.invalidate()
}
R.id.actionToggleIcons -> {
binding.chart1.candleData?.dataSets?.forEach { set ->
set.isDrawIcons = !set.isDrawIcons
}
binding.chart1.invalidate()
}
R.id.actionToggleHighlight -> {
binding.chart1.candleData?.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.actionToggleMakeShadowSameColorAsCandle -> {
binding.chart1.candleData?.dataSets?.forEach { set ->
(set as CandleDataSet).shadowColorSameAsCandle = !set.shadowColorSameAsCandle
}
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)
}
}
}
return true
}
override fun saveToGallery() {
saveToGallery(binding.chart1, "CandleStickChartActivity")
}
override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit
override fun onStopTrackingTouch(seekBar: SeekBar?) = Unit
}