Skip to content

Commit 6c0c61f

Browse files
committed
Data as var (the magic)
1 parent 8837a0f commit 6c0c61f

21 files changed

Lines changed: 371 additions & 78 deletions

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ListViewMultiChartActivity : DemoBase() {
4141
binding = ActivityListviewChartBinding.inflate(layoutInflater)
4242
setContentView(binding.root)
4343

44-
val list = ArrayList<ChartItem>()
44+
val list = ArrayList<ChartItem<*>>()
4545

4646
// 30 items
4747
for (i in 0..29) {
@@ -59,7 +59,7 @@ class ListViewMultiChartActivity : DemoBase() {
5959
}
6060

6161
/** adapter that supports 3 different item types */
62-
private class ChartDataAdapter(context: Context, objects: MutableList<ChartItem>) : ArrayAdapter<ChartItem>(context, 0, objects) {
62+
private class ChartDataAdapter(context: Context, objects: MutableList<ChartItem<*>>) : ArrayAdapter<ChartItem<*>>(context, 0, objects) {
6363
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
6464
return getItem(position)!!.getView(position, convertView, context)!!
6565
}

app/src/main/kotlin/info/appdev/chartexample/listviewitems/BarChartItem.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import android.view.View
88
import info.appdev.charting.charts.BarChart
99
import info.appdev.charting.components.XAxis.XAxisPosition
1010
import info.appdev.charting.data.BarData
11-
import info.appdev.charting.data.ChartData
1211
import info.appdev.chartexample.R
1312

14-
class BarChartItem(chartData: ChartData<*>, context: Context) : ChartItem(chartData) {
13+
class BarChartItem(chartData: BarData, context: Context) : ChartItem<BarData>(chartData) {
1514
private val typeface: Typeface? = Typeface.createFromAsset(context.assets, "OpenSans-Regular.ttf")
1615

1716
override val itemType: Int

app/src/main/kotlin/info/appdev/chartexample/listviewitems/ChartItem.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import info.appdev.charting.data.ChartData
88
* Base class of the Chart ListView items
99
*/
1010
@Suppress("unused")
11-
abstract class ChartItem internal constructor(var chartData: ChartData<*>) {
11+
abstract class ChartItem<T : ChartData<*>> internal constructor(var chartData: T) {
1212
abstract val itemType: Int
1313

1414
abstract fun getView(position: Int, convertView: View?, context: Context?): View?

app/src/main/kotlin/info/appdev/chartexample/listviewitems/LineChartItem.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import android.view.LayoutInflater
77
import android.view.View
88
import info.appdev.charting.charts.LineChart
99
import info.appdev.charting.components.XAxis.XAxisPosition
10-
import info.appdev.charting.data.ChartData
1110
import info.appdev.charting.data.LineData
1211
import info.appdev.chartexample.R
1312

14-
class LineChartItem(chartData: ChartData<*>, context: Context) : ChartItem(chartData) {
13+
class LineChartItem(lineData: LineData, context: Context) : ChartItem<LineData>(lineData) {
1514
private val typeface: Typeface? = Typeface.createFromAsset(context.assets, "OpenSans-Regular.ttf")
1615

1716
override val itemType: Int

app/src/main/kotlin/info/appdev/chartexample/listviewitems/PieChartItem.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ import android.view.LayoutInflater
1212
import android.view.View
1313
import info.appdev.charting.charts.PieChart
1414
import info.appdev.charting.components.Legend
15-
import info.appdev.charting.data.ChartData
1615
import info.appdev.charting.data.PieData
1716
import info.appdev.charting.formatter.PercentFormatter
1817
import info.appdev.charting.utils.ColorTemplate
1918
import info.appdev.chartexample.R
2019

21-
class PieChartItem(chartData: ChartData<*>, context: Context) : ChartItem(chartData) {
20+
class PieChartItem(pieData: PieData, context: Context) : ChartItem<PieData>(pieData) {
2221
private val typeface: Typeface? = Typeface.createFromAsset(context.assets, "OpenSans-Regular.ttf")
2322
private val centerText: SpannableString
2423

chartLib/src/main/kotlin/info/appdev/charting/charts/BarLineChartBase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import kotlin.math.min
4747
@Suppress("unused")
4848
@SuppressLint("RtlHardcoded")
4949
abstract class BarLineChartBase<T : BarLineScatterCandleBubbleData<IBarLineScatterCandleBubbleDataSet<out Entry>>> : Chart<T>,
50-
BarLineScatterCandleBubbleDataProvider {
50+
BarLineScatterCandleBubbleDataProvider<T> {
5151
/**
5252
* the maximum number of entries to which values will be drawn
5353
* (entry numbers greater than this value will cause value-labels to disappear)

chartLib/src/main/kotlin/info/appdev/charting/charts/Chart.kt

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import kotlin.math.abs
4949
import kotlin.math.max
5050

5151
@Suppress("unused")
52-
abstract class Chart<T : ChartData<out IDataSet<out Entry>>> : ViewGroup, IBaseProvider {
52+
abstract class Chart<T : ChartData<out IDataSet<out Entry>>> : ViewGroup, IBaseProvider<T> {
5353
/**
5454
* Returns true if log-output is enabled for the chart, fals if not.
5555
*/
@@ -237,35 +237,6 @@ abstract class Chart<T : ChartData<out IDataSet<out Entry>>> : ViewGroup, IBaseP
237237
this.legendRenderer = LegendRenderer(this.viewPortHandler, this.legend)
238238
}
239239

240-
/**
241-
* Sets a new data object for the chart. The data object contains all values
242-
* and information needed for displaying.
243-
*/
244-
open fun setData(data: T?) {
245-
mData = data
246-
mOffsetsCalculated = false
247-
248-
if (data == null) {
249-
return
250-
}
251-
252-
// calculate how many digits are needed
253-
setupDefaultFormatter(data.yMin, data.yMax)
254-
255-
for (set in mData!!.dataSets!!) {
256-
if (set.needsFormatter() || set.valueFormatter === mDefaultValueFormatter) {
257-
set.valueFormatter = mDefaultValueFormatter
258-
}
259-
}
260-
261-
// let the chart know there is new data
262-
notifyDataSetChanged()
263-
264-
if (this.isLogEnabled) {
265-
Timber.i("Data is set.")
266-
}
267-
}
268-
269240
/**
270241
* Clears the chart from all data (sets it to null) and refreshes it (by
271242
* calling invalidate()).
@@ -1053,11 +1024,36 @@ abstract class Chart<T : ChartData<out IDataSet<out Entry>>> : ViewGroup, IBaseP
10531024
}
10541025

10551026
/**
1056-
* Returns the ChartData object that has been set for the chart.
1027+
* Data object for the chart. The data object contains all values and information needed for displaying.
10571028
*/
1058-
override fun getData(): T? {
1059-
return mData
1060-
}
1029+
override var data: T?
1030+
get() = mData
1031+
set(value) {
1032+
mData = value
1033+
1034+
if (value == null) {
1035+
mOffsetsCalculated = false
1036+
return
1037+
}
1038+
1039+
mOffsetsCalculated = false
1040+
1041+
// calculate how many digits are needed
1042+
setupDefaultFormatter(value.yMin, value.yMax)
1043+
1044+
for (set in mData!!.dataSets!!) {
1045+
if (set.needsFormatter() || set.valueFormatter === mDefaultValueFormatter) {
1046+
set.valueFormatter = mDefaultValueFormatter
1047+
}
1048+
}
1049+
1050+
// let the chart know there is new data
1051+
notifyDataSetChanged()
1052+
1053+
if (isLogEnabled) {
1054+
Timber.i("Data is set.")
1055+
}
1056+
}
10611057

10621058
var renderer: DataRenderer?
10631059
/**

0 commit comments

Comments
 (0)