|
| 1 | +package com.github.mikephil.charting.buffer |
| 2 | + |
| 3 | +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet |
| 4 | +import kotlin.math.abs |
| 5 | + |
| 6 | +open class BarBuffer(size: Int, dataSetCount: Int, containsStacks: Boolean) : AbstractBuffer<IBarDataSet?>(size) { |
| 7 | + protected var dataSetIndex: Int = 0 |
| 8 | + protected var dataSetCount: Int = 1 |
| 9 | + |
| 10 | + @JvmField |
| 11 | + protected var containsStacks: Boolean = false |
| 12 | + |
| 13 | + @JvmField |
| 14 | + protected var inverted: Boolean = false |
| 15 | + |
| 16 | + /** width of the bar on the x-axis, in values (not pixels) */ |
| 17 | + @JvmField |
| 18 | + protected var barWidth: Float = 1f |
| 19 | + |
| 20 | + init { |
| 21 | + this.dataSetCount = dataSetCount |
| 22 | + this.containsStacks = containsStacks |
| 23 | + } |
| 24 | + |
| 25 | + fun setBarWidth(barWidthGiven: Float) { |
| 26 | + this.barWidth = barWidthGiven |
| 27 | + } |
| 28 | + |
| 29 | + fun setDataSet(index: Int) { |
| 30 | + this.dataSetIndex = index |
| 31 | + } |
| 32 | + |
| 33 | + fun setInverted(invertedGiven: Boolean) { |
| 34 | + this.inverted = invertedGiven |
| 35 | + } |
| 36 | + |
| 37 | + protected fun addBar(left: Float, top: Float, right: Float, bottom: Float) { |
| 38 | + buffer[index++] = left |
| 39 | + buffer[index++] = top |
| 40 | + buffer[index++] = right |
| 41 | + buffer[index++] = bottom |
| 42 | + } |
| 43 | + |
| 44 | + override fun toString(): String { |
| 45 | + return "BarBuffer{" + |
| 46 | + "dataSetIndex=" + dataSetIndex + |
| 47 | + ", dataSetCount=" + dataSetCount + |
| 48 | + ", containsStacks=" + containsStacks + |
| 49 | + ", inverted=" + inverted + |
| 50 | + ", barWidth=" + barWidth + |
| 51 | + ", buffer=" + buffer.contentToString() + |
| 52 | + ", index=" + index + |
| 53 | + '}' |
| 54 | + } |
| 55 | + |
| 56 | + override fun feed(data: IBarDataSet?) { |
| 57 | + val size = (data?.entryCount ?: 0) * phaseX |
| 58 | + val barWidthHalf = barWidth / 2f |
| 59 | + |
| 60 | + var i = 0 |
| 61 | + while (i < size) { |
| 62 | + val e = data?.getEntryForIndex(i) |
| 63 | + |
| 64 | + if (e == null) { |
| 65 | + i++ |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + val x = e.x |
| 70 | + var y = e.y |
| 71 | + val vals = e.yVals |
| 72 | + |
| 73 | + if (!containsStacks || vals == null) { |
| 74 | + val left = x - barWidthHalf |
| 75 | + val right = x + barWidthHalf |
| 76 | + var bottom: Float |
| 77 | + var top: Float |
| 78 | + |
| 79 | + if (inverted) { |
| 80 | + bottom = if (y >= 0) y else 0f |
| 81 | + top = if (y <= 0) y else 0f |
| 82 | + } else { |
| 83 | + top = if (y >= 0) y else 0f |
| 84 | + bottom = if (y <= 0) y else 0f |
| 85 | + } |
| 86 | + |
| 87 | + // multiply the height of the rect with the phase |
| 88 | + if (top > 0) top *= phaseY |
| 89 | + else bottom *= phaseY |
| 90 | + |
| 91 | + addBar(left, top, right, bottom) |
| 92 | + } else { |
| 93 | + var posY = 0f |
| 94 | + var negY = -e.negativeSum |
| 95 | + var yStart: Float |
| 96 | + |
| 97 | + // fill the stack |
| 98 | + for (k in vals.indices) { |
| 99 | + val value = vals[k] |
| 100 | + |
| 101 | + if (value == 0.0f && (posY == 0.0f || negY == 0.0f)) { |
| 102 | + // Take care of the situation of a 0.0 value, which overlaps a non-zero bar |
| 103 | + y = value |
| 104 | + yStart = y |
| 105 | + } else if (value >= 0.0f) { |
| 106 | + y = posY |
| 107 | + yStart = posY + value |
| 108 | + posY = yStart |
| 109 | + } else { |
| 110 | + y = negY |
| 111 | + yStart = (negY + abs(value.toDouble())).toFloat() |
| 112 | + negY += abs(value.toDouble()).toFloat() |
| 113 | + } |
| 114 | + |
| 115 | + val left = x - barWidthHalf |
| 116 | + val right = x + barWidthHalf |
| 117 | + var bottom: Float |
| 118 | + var top: Float |
| 119 | + |
| 120 | + if (inverted) { |
| 121 | + bottom = if (y >= yStart) y else yStart |
| 122 | + top = if (y <= yStart) y else yStart |
| 123 | + } else { |
| 124 | + top = if (y >= yStart) y else yStart |
| 125 | + bottom = if (y <= yStart) y else yStart |
| 126 | + } |
| 127 | + |
| 128 | + // multiply the height of the rect with the phase |
| 129 | + top *= phaseY |
| 130 | + bottom *= phaseY |
| 131 | + |
| 132 | + addBar(left, top, right, bottom) |
| 133 | + } |
| 134 | + } |
| 135 | + i++ |
| 136 | + } |
| 137 | + |
| 138 | + reset() |
| 139 | + } |
| 140 | +} |
0 commit comments