Entry.java 4.2 KB

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