Skip to content

Commit ac2c242

Browse files
authored
Merge pull request #346 from AppDevNext/BarBufferKotlin
BarBuffer Kotlin
2 parents 9a9c891 + c61c900 commit ac2c242

File tree

4 files changed

+145
-159
lines changed

4 files changed

+145
-159
lines changed

MPChartLib/src/main/java/com/github/mikephil/charting/buffer/AbstractBuffer.kt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ abstract class AbstractBuffer<T>(size: Int) {
2828
/** indicates to which x-index the visible data ranges */
2929
protected var to: Int = 0
3030

31-
/**
32-
* Initialization with buffer-size.
33-
*
34-
* @param size
35-
*/
3631
init {
3732
index = 0
3833
buffer = FloatArray(size)
@@ -63,12 +58,8 @@ abstract class AbstractBuffer<T>(size: Int) {
6358

6459
/**
6560
* Returns the size (length) of the buffer array.
66-
*
67-
* @return
6861
*/
69-
fun size(): Int {
70-
return buffer.size
71-
}
62+
fun size() = buffer.size
7263

7364
/**
7465
* Set the phases used for animations.

MPChartLib/src/main/java/com/github/mikephil/charting/buffer/BarBuffer.java

Lines changed: 0 additions & 145 deletions
This file was deleted.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
}

MPChartLib/src/main/java/com/github/mikephil/charting/buffer/HorizontalBarBuffer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public HorizontalBarBuffer(int size, int dataSetCount, boolean containsStacks) {
1414
public void feed(IBarDataSet data) {
1515

1616
float size = data.getEntryCount() * phaseX;
17-
float barWidthHalf = mBarWidth / 2f;
17+
float barWidthHalf = barWidth / 2f;
1818

1919
for (int i = 0; i < size; i++) {
2020

@@ -27,12 +27,12 @@ public void feed(IBarDataSet data) {
2727
float y = e.getY();
2828
float[] vals = e.getYVals();
2929

30-
if (!mContainsStacks || vals == null) {
30+
if (!containsStacks || vals == null) {
3131

3232
float bottom = x - barWidthHalf;
3333
float top = x + barWidthHalf;
3434
float left, right;
35-
if (mInverted) {
35+
if (inverted) {
3636
left = y >= 0 ? y : 0;
3737
right = y <= 0 ? y : 0;
3838
} else {
@@ -72,7 +72,7 @@ public void feed(IBarDataSet data) {
7272
float bottom = x - barWidthHalf;
7373
float top = x + barWidthHalf;
7474
float left, right;
75-
if (mInverted) {
75+
if (inverted) {
7676
left = y >= yStart ? y : yStart;
7777
right = y <= yStart ? y : yStart;
7878
} else {

0 commit comments

Comments
 (0)