-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathBarChartPositiveNegative.kt
More file actions
164 lines (135 loc) · 5.51 KB
/
BarChartPositiveNegative.kt
File metadata and controls
164 lines (135 loc) · 5.51 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
package info.appdev.chartexample
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.core.net.toUri
import info.appdev.chartexample.databinding.ActivityBarchartNoseekbarBinding
import info.appdev.chartexample.notimportant.DemoBase
import info.appdev.charting.components.AxisBase
import info.appdev.charting.components.XAxis.XAxisPosition
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.formatter.IValueFormatter
import info.appdev.charting.utils.ViewPortHandler
import java.text.DecimalFormat
import kotlin.math.max
import kotlin.math.min
class BarChartPositiveNegative : DemoBase() {
private lateinit var binding: ActivityBarchartNoseekbarBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityBarchartNoseekbarBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.chart1.setBackgroundColor(Color.WHITE)
binding.chart1.extraTopOffset = -30f
binding.chart1.extraBottomOffset = 10f
binding.chart1.extraLeftOffset = 70f
binding.chart1.extraRightOffset = 70f
binding.chart1.isDrawBarShadow = false
binding.chart1.isDrawValueAboveBar = true
binding.chart1.description.isEnabled = false
// 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.typeface = tfRegular
xAxis.isDrawGridLines = false
xAxis.isDrawAxisLine = false
xAxis.textColor = Color.LTGRAY
xAxis.textSize = 13f
xAxis.labelCount = 5
xAxis.centerAxisLabels = true
xAxis.granularity = 1f
val left = binding.chart1.axisLeft
left.setDrawLabels(false)
left.spaceTop = 25f
left.spaceBottom = 25f
left.isDrawAxisLine = false
left.isDrawGridLines = false
left.isDrawZeroLine = true // draw a zero line
left.zeroLineColor = Color.GRAY
left.zeroLineWidth = 0.7f
binding.chart1.axisRight.isEnabled = false
binding.chart1.legend.isEnabled = false
// THIS IS THE ORIGINAL DATA YOU WANT TO PLOT
val data: MutableList<Data> = mutableListOf()
data.add(Data(0f, -224.1f, "12-29"))
data.add(Data(1f, 238.5f, "12-30"))
data.add(Data(2f, 1280.1f, "12-31"))
data.add(Data(3f, -442.3f, "01-01"))
data.add(Data(4f, -2280.1f, "01-02"))
xAxis.valueFormatter = object : IAxisValueFormatter {
override fun getFormattedValue(value: Float, axis: AxisBase?): String {
return data[min(max(value.toInt(), 0), data.size - 1)].xAxisValue.toString()
}
}
setData(data)
}
private fun setData(dataList: MutableList<Data>) {
val values = ArrayList<BarEntryFloat>()
val colors: MutableList<Int> = ArrayList()
val green = Color.rgb(110, 190, 102)
val red = Color.rgb(211, 74, 88)
for (i in dataList.indices) {
val d = dataList[i]
val entry = BarEntryFloat(d.xValue, d.yValue)
values.add(entry)
// specific colors
if (d.yValue >= 0) colors.add(red)
else colors.add(green)
}
val set: BarDataSet
if (binding.chart1.barData != null &&
binding.chart1.barData!!.dataSetCount > 0
) {
set = binding.chart1.barData!!.getDataSetByIndex(0) as BarDataSet
set.entries = values
binding.chart1.barData?.notifyDataChanged()
binding.chart1.notifyDataSetChanged()
} else {
set = BarDataSet(values, "Values")
set.setColors(colors)
set.valueTextColors = colors
val data = BarData(set)
data.setValueTextSize(13f)
data.setValueTypeface(tfRegular)
data.setValueFormatter(ValueFormatter())
data.barWidth = 0.8f
binding.chart1.data = data
binding.chart1.invalidate()
}
}
/**
* Demo class representing data.
*/
private class Data(val xValue: Float, val yValue: Float, val xAxisValue: String?)
private class ValueFormatter : IValueFormatter {
private val mFormat: DecimalFormat = DecimalFormat("######.0")
override fun getFormattedValue(value: Float, entryFloat: EntryFloat?, dataSetIndex: Int, viewPortHandler: ViewPortHandler?): String {
return mFormat.format(value.toDouble())
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.only_github, 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/BarChartPositiveNegative.java".toUri()
startActivity(i)
}
}
return true
}
public override fun saveToGallery() { /* Intentionally left empty */
}
}