Skip to content

Commit b658da4

Browse files
committed
HorizontalBarBuffer Kotlin
1 parent ac2c242 commit b658da4

File tree

3 files changed

+87
-95
lines changed

3 files changed

+87
-95
lines changed

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

Lines changed: 0 additions & 94 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.github.mikephil.charting.buffer
2+
3+
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet
4+
import kotlin.math.abs
5+
6+
class HorizontalBarBuffer(size: Int, dataSetCount: Int, containsStacks: Boolean) : BarBuffer(size, dataSetCount, containsStacks) {
7+
override fun feed(data: IBarDataSet?) {
8+
val size = (data?.entryCount ?: 0) * phaseX
9+
val barWidthHalf = barWidth / 2f
10+
11+
var i = 0
12+
while (i < size) {
13+
val e = data?.getEntryForIndex(i)
14+
15+
if (e == null) {
16+
i++
17+
continue
18+
}
19+
20+
val x = e.x
21+
var y = e.y
22+
val vals = e.yVals
23+
24+
if (!containsStacks || vals == null) {
25+
val bottom = x - barWidthHalf
26+
val top = x + barWidthHalf
27+
var left: Float
28+
var right: Float
29+
if (inverted) {
30+
left = if (y >= 0) y else 0f
31+
right = if (y <= 0) y else 0f
32+
} else {
33+
right = if (y >= 0) y else 0f
34+
left = if (y <= 0) y else 0f
35+
}
36+
37+
// multiply the height of the rect with the phase
38+
if (right > 0) right *= phaseY
39+
else left *= phaseY
40+
41+
addBar(left, top, right, bottom)
42+
} else {
43+
var posY = 0f
44+
var negY = -e.negativeSum
45+
var yStart: Float
46+
47+
// fill the stack
48+
for (k in vals.indices) {
49+
val value = vals[k]
50+
51+
if (value >= 0f) {
52+
y = posY
53+
yStart = posY + value
54+
posY = yStart
55+
} else {
56+
y = negY
57+
yStart = (negY + abs(value.toDouble())).toFloat()
58+
negY += abs(value.toDouble()).toFloat()
59+
}
60+
61+
val bottom = x - barWidthHalf
62+
val top = x + barWidthHalf
63+
var left: Float
64+
var right: Float
65+
if (inverted) {
66+
left = if (y >= yStart) y else yStart
67+
right = if (y <= yStart) y else yStart
68+
} else {
69+
right = if (y >= yStart) y else yStart
70+
left = if (y <= yStart) y else yStart
71+
}
72+
73+
// multiply the height of the rect with the phase
74+
right *= phaseY
75+
left *= phaseY
76+
77+
addBar(left, top, right, bottom)
78+
}
79+
}
80+
i++
81+
}
82+
83+
reset()
84+
}
85+
}

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import kotlin.math.ceil
1818
import kotlin.math.min
1919

2020
open class BarChartRenderer(
21-
@JvmField var chart: BarDataProvider, animator: ChartAnimator?,
21+
@JvmField var chart: BarDataProvider,
22+
animator: ChartAnimator?,
2223
viewPortHandler: ViewPortHandler?
2324
) : BarLineScatterCandleBubbleRenderer(animator, viewPortHandler) {
2425
/**

0 commit comments

Comments
 (0)