|
| 1 | +package com.github.mikephil.charting.data |
| 2 | + |
| 3 | +import android.graphics.drawable.Drawable |
| 4 | +import android.os.Parcel |
| 5 | +import android.os.ParcelFormatException |
| 6 | +import android.os.Parcelable |
| 7 | +import com.github.mikephil.charting.utils.Utils |
| 8 | +import java.io.Serializable |
| 9 | +import kotlin.math.abs |
| 10 | + |
| 11 | +/** |
| 12 | + * Class representing one entry in the chart. Might contain multiple values. |
| 13 | + * Might only contain a single value depending on the used constructor. |
| 14 | + */ |
| 15 | +open class Entry : BaseEntry, Parcelable, Serializable { |
| 16 | + |
| 17 | + private var _x: Float = 0f |
| 18 | + open var x: Float |
| 19 | + get() = _x |
| 20 | + set(value) { |
| 21 | + _x = value |
| 22 | + } |
| 23 | + |
| 24 | + constructor() |
| 25 | + |
| 26 | + /** |
| 27 | + * A Entry represents one single entry in the chart. |
| 28 | + * |
| 29 | + * @param x the x value |
| 30 | + * @param y the y value (the actual value of the entry) |
| 31 | + */ |
| 32 | + constructor(x: Float, y: Float) : super(y) { |
| 33 | + this._x = x |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * A Entry represents one single entry in the chart. |
| 38 | + * |
| 39 | + * @param x the x value |
| 40 | + * @param y the y value (the actual value of the entry) |
| 41 | + * @param data Spot for additional data this Entry represents. |
| 42 | + */ |
| 43 | + constructor(x: Float, y: Float, data: Any?) : super(y, data) { |
| 44 | + this._x = x |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * A Entry represents one single entry in the chart. |
| 49 | + * |
| 50 | + * @param x the x value |
| 51 | + * @param y the y value (the actual value of the entry) |
| 52 | + * @param icon icon image |
| 53 | + */ |
| 54 | + constructor(x: Float, y: Float, icon: Drawable?) : super(y, icon) { |
| 55 | + this._x = x |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * A Entry represents one single entry in the chart. |
| 60 | + * |
| 61 | + * @param x the x value |
| 62 | + * @param y the y value (the actual value of the entry) |
| 63 | + * @param icon icon image |
| 64 | + * @param data Spot for additional data this Entry represents. |
| 65 | + */ |
| 66 | + constructor(x: Float, y: Float, icon: Drawable?, data: Any?) : super(y, icon, data) { |
| 67 | + this._x = x |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * returns an exact copy of the entry |
| 72 | + * |
| 73 | + * @return |
| 74 | + */ |
| 75 | + open fun copy(): Entry? { |
| 76 | + val e = Entry(x, y, data) |
| 77 | + return e |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Compares value, xIndex and data of the entries. Returns true if entries |
| 82 | + * are equal in those points, false if not. Does not check by hash-code like |
| 83 | + * it's done by the "equals" method. |
| 84 | + * |
| 85 | + * @param e |
| 86 | + * @return |
| 87 | + */ |
| 88 | + fun equalTo(e: Entry?): Boolean { |
| 89 | + if (e == null) return false |
| 90 | + |
| 91 | + if (e.data !== this.data) return false |
| 92 | + |
| 93 | + if (abs((e.x - this.x).toDouble()) > Utils.FLOAT_EPSILON) return false |
| 94 | + |
| 95 | + if (abs((e.y - this.y).toDouble()) > Utils.FLOAT_EPSILON) return false |
| 96 | + |
| 97 | + return true |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * returns a string representation of the entry containing x-index and value |
| 102 | + */ |
| 103 | + override fun toString(): String { |
| 104 | + return "Entry, x: $x y: $y" |
| 105 | + } |
| 106 | + |
| 107 | + override fun describeContents(): Int { |
| 108 | + return 0 |
| 109 | + } |
| 110 | + |
| 111 | + override fun writeToParcel(dest: Parcel, flags: Int) { |
| 112 | + dest.writeFloat(this.x) |
| 113 | + dest.writeFloat(this.y) |
| 114 | + if (data != null) { |
| 115 | + if (data is Parcelable) { |
| 116 | + dest.writeInt(1) |
| 117 | + dest.writeParcelable(data as Parcelable?, flags) |
| 118 | + } else { |
| 119 | + throw ParcelFormatException("Cannot parcel an Entry with non-parcelable data") |
| 120 | + } |
| 121 | + } else { |
| 122 | + dest.writeInt(0) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + protected constructor(`in`: Parcel) { |
| 127 | + this._x = `in`.readFloat() |
| 128 | + this._y = `in`.readFloat() |
| 129 | + if (`in`.readInt() == 1) { |
| 130 | + this.data = `in`.readParcelable(Any::class.java.classLoader) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + companion object { |
| 135 | + @JvmField |
| 136 | + val CREATOR: Parcelable.Creator<Entry?> = object : Parcelable.Creator<Entry?> { |
| 137 | + override fun createFromParcel(source: Parcel): Entry { |
| 138 | + return Entry(source) |
| 139 | + } |
| 140 | + |
| 141 | + override fun newArray(size: Int): Array<Entry?> { |
| 142 | + return arrayOfNulls(size) |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments