Skip to content

Commit 09eab6a

Browse files
committed
Data as var (the magic)
1 parent 874c664 commit 09eab6a

21 files changed

+209
-77
lines changed

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
/**

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

Lines changed: 134 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import info.appdev.charting.data.LineData
1212
import info.appdev.charting.data.ScatterData
1313
import info.appdev.charting.highlight.CombinedHighlighter
1414
import info.appdev.charting.highlight.Highlight
15+
import info.appdev.charting.interfaces.dataprovider.BarDataProvider
1516
import info.appdev.charting.interfaces.dataprovider.CombinedDataProvider
17+
import info.appdev.charting.interfaces.dataprovider.LineDataProvider
1618
import info.appdev.charting.interfaces.datasets.IDataSet
1719
import info.appdev.charting.renderer.CombinedChartRenderer
1820
import timber.log.Timber
@@ -25,22 +27,25 @@ open class CombinedChart : BarLineChartBase<CombinedData>, CombinedDataProvider
2527
/**
2628
* if set to true, all values are drawn above their bars, instead of below their top
2729
*/
28-
override var isDrawValueAboveBarEnabled: Boolean = true
30+
var isDrawValueAboveBarEnabled: Boolean = true
2931

3032

3133
/**
3234
* Set this to true to make the highlight operation full-bar oriented,
3335
* false to make it highlight single values (relevant only for stacked).
3436
*/
35-
override var isHighlightFullBarEnabled: Boolean = false
37+
var isHighlightFullBarEnabled: Boolean = false
3638

3739
/**
3840
* if set to true, a grey area is drawn behind each bar that indicates the maximum value
3941
*/
40-
override var isDrawBarShadowEnabled: Boolean = false
42+
var isDrawBarShadowEnabled: Boolean = false
4143

4244
protected var mDrawOrder: MutableList<DrawOrder>? = null
4345

46+
lateinit var barDataProvider : BarDataProvider
47+
lateinit var lineDataProvider : LineDataProvider
48+
4449
/**
4550
* enum that allows to specify the order in which the different data objects for the combined-chart are drawn
4651
*/
@@ -62,7 +67,132 @@ open class CombinedChart : BarLineChartBase<CombinedData>, CombinedDataProvider
6267
DrawOrder.BAR, DrawOrder.BUBBLE, DrawOrder.LINE, DrawOrder.CANDLE, DrawOrder.SCATTER
6368
)
6469

65-
setHighlighter(CombinedHighlighter(this, this))
70+
// Create BarDataProvider adapter for CombinedHighlighter
71+
barDataProvider = object : BarDataProvider {
72+
override val barData: BarData?
73+
get() = this@CombinedChart.barData
74+
75+
override var isDrawBarShadowEnabled: Boolean
76+
get() = this@CombinedChart.isDrawBarShadowEnabled
77+
set(value) { this@CombinedChart.isDrawBarShadowEnabled = value }
78+
79+
override var isDrawValueAboveBarEnabled: Boolean
80+
get() = this@CombinedChart.isDrawValueAboveBarEnabled
81+
set(value) { this@CombinedChart.isDrawValueAboveBarEnabled = value }
82+
83+
override var isHighlightFullBarEnabled: Boolean
84+
get() = this@CombinedChart.isHighlightFullBarEnabled
85+
set(value) { this@CombinedChart.isHighlightFullBarEnabled = value }
86+
87+
override var data: BarData?
88+
get() = this@CombinedChart.barData
89+
set(_) {}
90+
91+
override fun getTransformer(axis: info.appdev.charting.components.YAxis.AxisDependency?) =
92+
this@CombinedChart.getTransformer(axis)
93+
94+
override fun isInverted(axis: info.appdev.charting.components.YAxis.AxisDependency?) =
95+
this@CombinedChart.isInverted(axis)
96+
97+
override val lowestVisibleX: Float
98+
get() = this@CombinedChart.lowestVisibleX
99+
100+
override val highestVisibleX: Float
101+
get() = this@CombinedChart.highestVisibleX
102+
103+
override val xChartMin: Float
104+
get() = this@CombinedChart.xChartMin
105+
106+
override val xChartMax: Float
107+
get() = this@CombinedChart.xChartMax
108+
109+
override val xRange: Float
110+
get() = this@CombinedChart.xRange
111+
112+
override val yChartMin: Float
113+
get() = this@CombinedChart.yChartMin
114+
115+
override val yChartMax: Float
116+
get() = this@CombinedChart.yChartMax
117+
118+
override var maxHighlightDistance: Float
119+
get() = this@CombinedChart.maxHighlightDistance
120+
set(value) { this@CombinedChart.maxHighlightDistance = value }
121+
122+
override val centerOfView: info.appdev.charting.utils.PointF
123+
get() = this@CombinedChart.centerOfView
124+
125+
override val centerOffsets: info.appdev.charting.utils.PointF
126+
get() = this@CombinedChart.centerOffsets
127+
128+
override val contentRect: android.graphics.RectF
129+
get() = this@CombinedChart.contentRect
130+
131+
override val defaultValueFormatter: info.appdev.charting.formatter.IValueFormatter
132+
get() = this@CombinedChart.defaultValueFormatter
133+
134+
override val maxVisibleCount: Int
135+
get() = this@CombinedChart.maxVisibleCount
136+
}
137+
138+
lineDataProvider = object : LineDataProvider {
139+
140+
141+
override fun getTransformer(axis: info.appdev.charting.components.YAxis.AxisDependency?) =
142+
this@CombinedChart.getTransformer(axis)
143+
144+
override fun isInverted(axis: info.appdev.charting.components.YAxis.AxisDependency?) =
145+
this@CombinedChart.isInverted(axis)
146+
147+
override val lowestVisibleX: Float
148+
get() = this@CombinedChart.lowestVisibleX
149+
150+
override val highestVisibleX: Float
151+
get() = this@CombinedChart.highestVisibleX
152+
153+
override val xChartMin: Float
154+
get() = this@CombinedChart.xChartMin
155+
156+
override val xChartMax: Float
157+
get() = this@CombinedChart.xChartMax
158+
159+
override val xRange: Float
160+
get() = this@CombinedChart.xRange
161+
162+
override val yChartMin: Float
163+
get() = this@CombinedChart.yChartMin
164+
165+
override val yChartMax: Float
166+
get() = this@CombinedChart.yChartMax
167+
168+
override var maxHighlightDistance: Float
169+
get() = this@CombinedChart.maxHighlightDistance
170+
set(value) { this@CombinedChart.maxHighlightDistance = value }
171+
172+
override val centerOfView: info.appdev.charting.utils.PointF
173+
get() = this@CombinedChart.centerOfView
174+
175+
override val centerOffsets: info.appdev.charting.utils.PointF
176+
get() = this@CombinedChart.centerOffsets
177+
178+
override val contentRect: android.graphics.RectF
179+
get() = this@CombinedChart.contentRect
180+
181+
override val defaultValueFormatter: info.appdev.charting.formatter.IValueFormatter
182+
get() = this@CombinedChart.defaultValueFormatter
183+
184+
override var data: LineData?
185+
get() = this@CombinedChart.lineData
186+
set(value) {}
187+
188+
override val maxVisibleCount: Int
189+
get() = this@CombinedChart.maxVisibleCount
190+
override val lineData: LineData?
191+
get() = this@CombinedChart.lineData
192+
193+
}
194+
195+
setHighlighter(CombinedHighlighter(this, barDataProvider))
66196

67197
// Old default behaviour
68198
isHighlightFullBarEnabled = true
@@ -73,12 +203,6 @@ open class CombinedChart : BarLineChartBase<CombinedData>, CombinedDataProvider
73203
override val combinedData: CombinedData?
74204
get() = mData
75205

76-
override fun setData(data: CombinedData?) {
77-
super.setData(data)
78-
setHighlighter(CombinedHighlighter(this, this))
79-
(mRenderer as CombinedChartRenderer).createRenderers()
80-
mRenderer?.initBuffers()
81-
}
82206

83207
/**
84208
* Returns the Highlight object (contains x-index and DataSet index) of the selected value at the given touch

chartLib/src/main/kotlin/info/appdev/charting/highlight/ChartHighlighter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import info.appdev.charting.utils.PointD
1010
import kotlin.math.abs
1111
import kotlin.math.hypot
1212

13-
open class ChartHighlighter<T : BarLineScatterCandleBubbleDataProvider>(protected var provider: T) : IHighlighter {
13+
open class ChartHighlighter<T : BarLineScatterCandleBubbleDataProvider<*>>(protected var provider: T) : IHighlighter {
1414
/**
1515
* buffer for storing previously highlighted values
1616
*/

chartLib/src/main/kotlin/info/appdev/charting/interfaces/dataprovider/BarDataProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package info.appdev.charting.interfaces.dataprovider
33
import info.appdev.charting.data.BarData
44
import info.appdev.charting.interfaces.dataprovider.base.BarLineScatterCandleBubbleDataProvider
55

6-
interface BarDataProvider : BarLineScatterCandleBubbleDataProvider {
6+
interface BarDataProvider : BarLineScatterCandleBubbleDataProvider<BarData> {
77
val barData: BarData?
88
var isDrawBarShadowEnabled: Boolean
99
var isDrawValueAboveBarEnabled: Boolean

0 commit comments

Comments
 (0)