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