BarBuffer.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package com.github.mikephil.charting.buffer;
  2. import android.util.Log;
  3. import com.github.mikephil.charting.data.BarEntry;
  4. import java.util.List;
  5. public class BarBuffer extends AbstractBuffer<BarEntry> {
  6. protected float mBarSpace = 0f;
  7. protected float mGroupSpace = 0f;
  8. protected int mDataSetIndex = 0;
  9. protected int mDataSetCount = 1;
  10. protected boolean mContainsStacks = false;
  11. protected boolean mInverted = false;
  12. public BarBuffer(int size, float groupspace, int dataSetCount, boolean containsStacks) {
  13. super(size);
  14. this.mGroupSpace = groupspace;
  15. this.mDataSetCount = dataSetCount;
  16. this.mContainsStacks = containsStacks;
  17. }
  18. public void setBarSpace(float barspace) {
  19. this.mBarSpace = barspace;
  20. }
  21. public void setDataSet(int index) {
  22. this.mDataSetIndex = index;
  23. }
  24. public void setInverted(boolean inverted) {
  25. this.mInverted = inverted;
  26. }
  27. protected void addBar(float left, float top, float right, float bottom) {
  28. try {
  29. buffer[index++] = left;
  30. buffer[index++] = top;
  31. buffer[index++] = right;
  32. buffer[index++] = bottom;
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. Log.i("MPAndroidChart", "addBar:");
  36. }
  37. }
  38. @Override
  39. public void feed(List<BarEntry> entries) {
  40. float size = entries.size() * phaseX;
  41. int dataSetOffset = (mDataSetCount - 1);
  42. float barSpaceHalf = mBarSpace / 2f;
  43. float groupSpaceHalf = mGroupSpace / 2f;
  44. float barWidth = 0.5f;
  45. for (int i = 0; i < size; i++) {
  46. BarEntry e = entries.get(i);
  47. // calculate the x-position, depending on datasetcount
  48. float x = e.getXIndex() + i * dataSetOffset + mDataSetIndex
  49. + mGroupSpace * i + groupSpaceHalf;
  50. float y = e.getVal();
  51. float [] vals = e.getVals();
  52. if(mInverted) { // inverted axis, here, I chose performance over readability
  53. if(!mContainsStacks || vals == null) {
  54. float left = x - barWidth + barSpaceHalf;
  55. float right = x + barWidth - barSpaceHalf;
  56. float bottom = y >= 0 ? y : 0;
  57. float top = y <= 0 ? y : 0;
  58. // multiply the height of the rect with the phase
  59. if (top > 0)
  60. top *= phaseY;
  61. else
  62. bottom *= phaseY;
  63. addBar(left, top, right, bottom);
  64. } else {
  65. float all = e.getVal();
  66. // fill the stack
  67. for (int k = 0; k < vals.length; k++) {
  68. all -= vals[k];
  69. y = vals[k] + all;
  70. float left = x - barWidth + barSpaceHalf;
  71. float right = x + barWidth - barSpaceHalf;
  72. float bottom = y >= 0 ? y : 0;
  73. float top = y <= 0 ? y : 0;
  74. // multiply the height of the rect with the phase
  75. if (top > 0)
  76. top *= phaseY;
  77. else
  78. bottom *= phaseY;
  79. addBar(left, top, right, bottom);
  80. }
  81. }
  82. } else { // non inverted axis
  83. if(!mContainsStacks || vals == null) {
  84. float left = x - barWidth + barSpaceHalf;
  85. float right = x + barWidth - barSpaceHalf;
  86. float top = y >= 0 ? y : 0;
  87. float bottom = y <= 0 ? y : 0;
  88. // multiply the height of the rect with the phase
  89. if (top > 0)
  90. top *= phaseY;
  91. else
  92. bottom *= phaseY;
  93. addBar(left, top, right, bottom);
  94. } else {
  95. float all = e.getVal();
  96. // fill the stack
  97. for (int k = 0; k < vals.length; k++) {
  98. all -= vals[k];
  99. y = vals[k] + all;
  100. float left = x - barWidth + barSpaceHalf;
  101. float right = x + barWidth - barSpaceHalf;
  102. float top = y >= 0 ? y : 0;
  103. float bottom = y <= 0 ? y : 0;
  104. // multiply the height of the rect with the phase
  105. if (top > 0)
  106. top *= phaseY;
  107. else
  108. bottom *= phaseY;
  109. addBar(left, top, right, bottom);
  110. }
  111. }
  112. }
  113. }
  114. reset();
  115. }
  116. }