Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener {
}

private fun createSet(): LineDataSet {
val set = LineDataSet(null, "DataSet 1")
val set = LineDataSet(label = "DataSet 1")
set.lineWidth = 2.5f
set.circleRadius = 4.5f
set.color = Color.rgb(240, 99, 99)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class RealtimeLineChartActivity : DemoBase(), OnChartValueSelectedListener {
}

private fun createSet(): LineDataSet {
val set = LineDataSet(null, "Dynamic Data")
val set = LineDataSet(label = "Dynamic Data")
set.axisDependency = AxisDependency.LEFT
set.color = ColorTemplate.holoBlue
set.setCircleColor(Color.WHITE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ class HorizontalBarComposeActivity : DemoBaseCompose() {
if (chartData != null && chartData.dataSetCount > 0) {
set1 = chartData.getDataSetByIndex(0) as BarDataSet
@Suppress("DEPRECATION")
set1.values = values
set1.entries = values
chartData.notifyDataChanged()
localChart.notifyDataSetChanged()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import info.appdev.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataS
/**
* Baseclass of all DataSets for Bar-, Line-, Scatter- and CandleStickChart.
*/
abstract class BarLineScatterCandleBubbleDataSet<T : Entry>(yVals: MutableList<T>?, label: String) :
abstract class BarLineScatterCandleBubbleDataSet<T : Entry>(yVals: MutableList<T>, label: String) :
DataSet<T>(yVals, label), IBarLineScatterCandleBubbleDataSet<T> {
/**
* Sets the color that is used for drawing the highlight indicators.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package info.appdev.charting.data
import info.appdev.charting.interfaces.datasets.IBubbleDataSet
import info.appdev.charting.utils.convertDpToPixel

open class BubbleDataSet(yVals: MutableList<BubbleEntry>?, label: String) : BarLineScatterCandleBubbleDataSet<BubbleEntry>(yVals, label), IBubbleDataSet {
open class BubbleDataSet(yVals: MutableList<BubbleEntry>, label: String) : BarLineScatterCandleBubbleDataSet<BubbleEntry>(yVals, label), IBubbleDataSet {
protected var mMaxSize: Float = 0f
protected var mNormalizeSize: Boolean = true

Expand All @@ -21,8 +21,8 @@ open class BubbleDataSet(yVals: MutableList<BubbleEntry>?, label: String) : BarL

override fun copy(): DataSet<BubbleEntry> {
val entries: MutableList<BubbleEntry> = ArrayList()
for (i in mEntries!!.indices) {
entries.add(mEntries!![i].copy())
for (i in mEntries.indices) {
entries.add(mEntries[i].copy())
}
val copied = BubbleDataSet(entries, label)
copy(copied)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import info.appdev.charting.utils.convertDpToPixel
/**
* DataSet for the CandleStickChart.
*/
open class CandleDataSet(yVals: MutableList<CandleEntry>?, label: String = "") : LineScatterCandleRadarDataSet<CandleEntry>(yVals, label), ICandleDataSet {
open class CandleDataSet(yVals: MutableList<CandleEntry>, label: String = "") : LineScatterCandleRadarDataSet<CandleEntry>(yVals, label), ICandleDataSet {
/**
* the width of the shadow of the candle
*/
Expand Down Expand Up @@ -72,10 +72,8 @@ open class CandleDataSet(yVals: MutableList<CandleEntry>?, label: String = "") :

override fun copy(): DataSet<CandleEntry> {
val entries: MutableList<CandleEntry> = mutableListOf()
mEntries?.let {
for (i in it.indices) {
entries.add(it[i].copy())
}
for (i in mEntries.indices) {
entries.add(mEntries[i].copy())
}

val copied = CandleDataSet(entries, label)
Expand Down
107 changes: 37 additions & 70 deletions chartLib/src/main/kotlin/info/appdev/charting/data/DataSet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlin.math.abs
* LineChart, or the values of a specific group of bars in the BarChart).
*/
abstract class DataSet<T : Entry>(
protected var mEntries: MutableList<T>?,
protected var mEntries: MutableList<T>,
label: String = ""
) : BaseDataSet<T>(label), Serializable {
/**
Expand All @@ -38,17 +38,12 @@ abstract class DataSet<T : Entry>(
override var xMin: Float = Float.MAX_VALUE
protected set


/**
* Creates a new DataSet object with the given values (entries) it represents. Also, a
* label that describes the DataSet can be specified. The label can also be
* used to retrieve the DataSet from a ChartData object.
*/
init {
if (mEntries == null) {
mEntries = ArrayList<T>()
}

calcMinMax()
}

Expand All @@ -58,7 +53,7 @@ abstract class DataSet<T : Entry>(
this.xMax = -Float.MAX_VALUE
this.xMin = Float.MAX_VALUE

if (mEntries == null || mEntries!!.isEmpty()) {
if (mEntries.isEmpty()) {
return
}

Expand All @@ -71,7 +66,7 @@ abstract class DataSet<T : Entry>(
this.yMax = -Float.MAX_VALUE
this.yMin = Float.MAX_VALUE

if (mEntries == null || mEntries!!.isEmpty()) {
if (mEntries.isEmpty()) {
return
}

Expand All @@ -85,7 +80,7 @@ abstract class DataSet<T : Entry>(
for (i in indexFrom..indexTo) {
// only recalculate y

calcMinMaxY(mEntries!![i])
calcMinMaxY(mEntries[i])
}
}

Expand Down Expand Up @@ -118,32 +113,13 @@ abstract class DataSet<T : Entry>(
}

override val entryCount: Int
get() = mEntries!!.size

@get:Deprecated("")
@set:Deprecated("")
var values: MutableList<T>?
/**
* This method is deprecated.
* Use getEntries() instead.
*/
get() = mEntries
/**
* This method is deprecated.
* Use setEntries(...) instead.
*/
set(values) {
this.entries = values
}
get() = mEntries.size

var entries: MutableList<T>?
/**
* Returns the array of entries that this DataSet represents.
*/
/**
* Returns the array of entries that this DataSet represents.
*/
var entries: MutableList<T>
get() = mEntries
/**
* Sets the array of entries that this DataSet represents, and calls notifyDataSetChanged()
*/
set(entries) {
mEntries = entries
notifyDataSetChanged()
Expand All @@ -161,42 +137,35 @@ abstract class DataSet<T : Entry>(
override fun toString(): String {
val buffer = StringBuilder()
buffer.append(toSimpleString())
for (i in mEntries!!.indices) {
buffer.append(mEntries!![i].toString()).append(" ")
for (i in mEntries.indices) {
buffer.append(mEntries[i].toString()).append(" ")
}
return buffer.toString()
}

/**
* Returns a simple string representation of the DataSet with the type and the number of Entries.
*/
fun toSimpleString() = "DataSet, label: $label, entries: ${mEntries!!.size}"
fun toSimpleString() = "DataSet, label: $label, entries: ${mEntries.size}"

override fun addEntryOrdered(entry: T) {
if (mEntries == null) {
mEntries = ArrayList()
}

calcMinMax(entry)

if (!mEntries!!.isEmpty() && mEntries!![mEntries!!.size - 1].x > entry.x) {
if (!mEntries.isEmpty() && mEntries[mEntries.size - 1].x > entry.x) {
val closestIndex = getEntryIndex(entry.x, entry.y, Rounding.UP)
mEntries!!.add(closestIndex, entry)
mEntries.add(closestIndex, entry)
} else {
mEntries!!.add(entry)
mEntries.add(entry)
}
}

override fun clear() {
mEntries!!.clear()
mEntries.clear()
notifyDataSetChanged()
}

override fun addEntry(entry: T): Boolean {
var values = this.entries
if (values == null) {
values = ArrayList()
}
val values = this.entries

calcMinMax(entry)

Expand All @@ -205,11 +174,9 @@ abstract class DataSet<T : Entry>(
}

override fun removeEntry(entry: T): Boolean {
if (mEntries == null)
return false

// remove the entry
val removed = mEntries!!.remove(entry)
val removed = mEntries.remove(entry)

if (removed) {
calcMinMax()
Expand All @@ -220,14 +187,14 @@ abstract class DataSet<T : Entry>(

override fun getEntryIndex(entry: T): Int {
// return getEntryIndex(entry)
return mEntries!!.indexOf(entry)
return mEntries.indexOf(entry)
}


override fun getEntryForXValue(xValue: Float, closestToY: Float, rounding: Rounding?): T? {
val index = getEntryIndex(xValue, closestToY, rounding)
if (index > -1) {
return mEntries!![index]
return mEntries[index]
}
return null
}
Expand All @@ -240,28 +207,28 @@ abstract class DataSet<T : Entry>(
if (index < 0) {
Timber.e("index $index is < 0 for getEntryForIndex")
return null
} else if (index >= mEntries!!.size) {
Timber.e("index " + index + "/" + mEntries!!.size + " is out of range for getEntryForIndex")
} else if (index >= mEntries.size) {
Timber.e("index $index / ${mEntries.size} is out of range for getEntryForIndex")
return null
}
return mEntries!![index]
return mEntries[index]
}

override fun getEntryIndex(xValue: Float, closestToY: Float, rounding: Rounding?): Int {
if (mEntries == null || mEntries!!.isEmpty()) {
if (mEntries.isEmpty()) {
return -1
}

var low = 0
var high = mEntries!!.size - 1
var high = mEntries.size - 1
var closest = high

while (low < high) {
val m = low + (high - low) / 2

val currentEntry: Entry = mEntries!![m]
val currentEntry: Entry = mEntries[m]

val nextEntry: Entry = mEntries!![m + 1]
val nextEntry: Entry = mEntries[m + 1]

val d1 = currentEntry.x - xValue
val d2 = nextEntry.x - xValue
Expand Down Expand Up @@ -291,11 +258,11 @@ abstract class DataSet<T : Entry>(
closest = high
}

val closestEntry: Entry = mEntries!![closest]
val closestEntry: Entry = mEntries[closest]
val closestXValue = closestEntry.x
if (rounding == Rounding.UP) {
// If rounding up, and found x-value is lower than specified x, and we can go upper...
if (closestXValue < xValue && closest < mEntries!!.size - 1) {
if (closestXValue < xValue && closest < mEntries.size - 1) {
++closest
}
} else if (rounding == Rounding.DOWN) {
Expand All @@ -307,7 +274,7 @@ abstract class DataSet<T : Entry>(

// Search by closest to y-value
if (!closestToY.isNaN()) {
while (closest > 0 && mEntries!![closest - 1].x == closestXValue) {
while (closest > 0 && mEntries[closest - 1].x == closestXValue) {
closest -= 1
}

Expand All @@ -316,11 +283,11 @@ abstract class DataSet<T : Entry>(

while (true) {
closest += 1
if (closest >= mEntries!!.size) {
if (closest >= mEntries.size) {
break
}

val value: T = mEntries!![closest]
val value: T = mEntries[closest]

if (value.x != closestXValue) {
break
Expand All @@ -341,23 +308,23 @@ abstract class DataSet<T : Entry>(
val entries: MutableList<T> = mutableListOf()

var low = 0
var high = mEntries!!.size - 1
var high = mEntries.size - 1

while (low <= high) {
var m = (high + low) / 2
var entry = mEntries!![m]
var entry = mEntries[m]

// if we have a match
if (xValue == entry.x) {
while (m > 0 && mEntries!![m - 1].x == xValue) {
while (m > 0 && mEntries[m - 1].x == xValue) {
m--
}

high = mEntries!!.size
high = mEntries.size

// loop over all "equal" entries
while (m < high) {
entry = mEntries!![m]
entry = mEntries[m]
if (entry.x == xValue) {
entries.add(entry)
} else {
Expand Down
Loading
Loading