Skip to content

Commit 6a7500e

Browse files
committed
Kotlin items
1 parent 0063449 commit 6a7500e

File tree

10 files changed

+271
-320
lines changed

10 files changed

+271
-320
lines changed

app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ class ListViewMultiChartActivity : DemoBase() {
6565
}
6666

6767
/** adapter that supports 3 different item types */
68-
private class ChartDataAdapter(context: Context, objects: MutableList<ChartItem?>) : ArrayAdapter<ChartItem?>(context, 0, objects) {
68+
private class ChartDataAdapter(context: Context, objects: MutableList<ChartItem?>) : ArrayAdapter<ChartItem>(context, 0, objects) {
6969
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
70-
return getItem(position)!!.getView(position, convertView, context)
70+
return getItem(position)!!.getView(position, convertView, context)!!
7171
}
7272

7373
override fun getItemViewType(position: Int): Int {
7474
// return the views type
7575
val ci = getItem(position)
76-
return if (ci != null) ci.getItemType() else 0
76+
return if (ci != null) ci.itemType else 0
7777
}
7878

7979
override fun getViewTypeCount(): Int {

app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.java

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package info.appdev.chartexample.listviewitems
2+
3+
import android.annotation.SuppressLint
4+
import android.content.Context
5+
import android.graphics.Typeface
6+
import android.view.LayoutInflater
7+
import android.view.View
8+
import com.github.mikephil.charting.charts.BarChart
9+
import com.github.mikephil.charting.components.XAxis.XAxisPosition
10+
import com.github.mikephil.charting.data.BarData
11+
import com.github.mikephil.charting.data.ChartData
12+
import info.appdev.chartexample.R
13+
14+
class BarChartItem(cd: ChartData<*>, c: Context) : ChartItem(cd) {
15+
private val typeface: Typeface? = Typeface.createFromAsset(c.assets, "OpenSans-Regular.ttf")
16+
17+
override val itemType: Int
18+
get() = TYPE_BARCHART
19+
20+
@SuppressLint("InflateParams")
21+
override fun getView(position: Int, convertView: View?, c: Context?): View? {
22+
var convertView = convertView
23+
val holder: ViewHolder
24+
25+
if (convertView == null) {
26+
holder = ViewHolder()
27+
28+
convertView = LayoutInflater.from(c).inflate(
29+
R.layout.list_item_barchart, null
30+
)
31+
holder.chart = convertView.findViewById(R.id.chart)
32+
33+
convertView.tag = holder
34+
} else {
35+
holder = convertView.tag as ViewHolder
36+
}
37+
38+
// apply styling
39+
holder.chart!!.description.isEnabled = false
40+
holder.chart!!.setDrawGridBackground(false)
41+
holder.chart!!.setDrawBarShadow(false)
42+
43+
val xAxis = holder.chart!!.xAxis
44+
xAxis.position = XAxisPosition.BOTTOM
45+
xAxis.typeface = typeface
46+
xAxis.setDrawGridLines(false)
47+
xAxis.setDrawAxisLine(true)
48+
49+
val leftAxis = holder.chart!!.axisLeft
50+
leftAxis.typeface = typeface
51+
leftAxis.setLabelCount(5, false)
52+
leftAxis.spaceTop = 20f
53+
leftAxis.setAxisMinimum(0f) // this replaces setStartAtZero(true)
54+
55+
val rightAxis = holder.chart!!.axisRight
56+
rightAxis.typeface = typeface
57+
rightAxis.setLabelCount(5, false)
58+
rightAxis.spaceTop = 20f
59+
rightAxis.setAxisMinimum(0f) // this replaces setStartAtZero(true)
60+
61+
chartData.setValueTypeface(typeface)
62+
63+
// set data
64+
holder.chart!!.setData(chartData as BarData?)
65+
holder.chart!!.setFitBars(true)
66+
67+
// do not forget to refresh the chart
68+
// holder.chart.invalidate();
69+
holder.chart!!.animateY(700)
70+
71+
return convertView
72+
}
73+
74+
private class ViewHolder {
75+
var chart: BarChart? = null
76+
}
77+
}

app/src/main/java/info/appdev/chartexample/listviewitems/ChartItem.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package info.appdev.chartexample.listviewitems
2+
3+
import android.content.Context
4+
import android.view.View
5+
import com.github.mikephil.charting.data.ChartData
6+
7+
/**
8+
* Base class of the Chart ListView items
9+
*/
10+
@Suppress("unused")
11+
abstract class ChartItem internal constructor(@JvmField var chartData: ChartData<*>) {
12+
abstract val itemType: Int
13+
14+
abstract fun getView(position: Int, convertView: View?, c: Context?): View?
15+
16+
companion object {
17+
const val TYPE_BARCHART: Int = 0
18+
const val TYPE_LINECHART: Int = 1
19+
const val TYPE_PIECHART: Int = 2
20+
}
21+
}

app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.java

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)