Skip to content

Commit 1953fd1

Browse files
committed
AbstractBuffer buffer as MutableList
1 parent b073675 commit 1953fd1

10 files changed

+1186
-1117
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ package com.github.mikephil.charting.buffer
55
*
66
* @param <T> The data the buffer accepts to be fed with.
77
</T> */
8-
abstract class AbstractBuffer<T>(size: Int) {
8+
abstract class AbstractBuffer<T> {
99
/** index in the buffer */
1010
@JvmField
1111
protected var index: Int = 0
1212

1313
/** float-buffer that holds the data points to draw, order: x,y,x,y,... */
1414
@JvmField
15-
val buffer: FloatArray
15+
val buffer: MutableList<Float> = mutableListOf()
1616

1717
/** animation phase x-axis */
1818
@JvmField
@@ -35,7 +35,6 @@ abstract class AbstractBuffer<T>(size: Int) {
3535
*/
3636
init {
3737
index = 0
38-
buffer = FloatArray(size)
3938
}
4039

4140
/** limits the drawing on the x-axis */

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

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
public class BarBuffer extends AbstractBuffer<IBarDataSet> {
88

99
protected int mDataSetIndex = 0;
10-
protected int mDataSetCount = 1;
11-
protected boolean mContainsStacks = false;
10+
protected int mDataSetCount;
11+
protected boolean mContainsStacks;
1212
protected boolean mInverted = false;
1313

1414
/** width of the bar on the x-axis, in values (not pixels) */
1515
protected float mBarWidth = 1f;
1616

17-
public BarBuffer(int size, int dataSetCount, boolean containsStacks) {
18-
super(size);
19-
this.mDataSetCount = dataSetCount;
17+
public BarBuffer(int dataSetCount, boolean containsStacks) {
18+
super();
19+
this.mDataSetCount = dataSetCount;
2020
this.mContainsStacks = containsStacks;
2121
}
2222

@@ -33,11 +33,10 @@ public void setInverted(boolean inverted) {
3333
}
3434

3535
protected void addBar(float left, float top, float right, float bottom) {
36-
37-
buffer[index++] = left;
38-
buffer[index++] = top;
39-
buffer[index++] = right;
40-
buffer[index++] = bottom;
36+
buffer.add(left);
37+
buffer.add(top);
38+
buffer.add(right);
39+
buffer.add(bottom);
4140
}
4241

4342
@Override
@@ -83,45 +82,43 @@ public void feed(IBarDataSet data) {
8382

8483
float posY = 0f;
8584
float negY = -e.getNegativeSum();
86-
float yStart = 0f;
85+
float yStart;
8786

8887
// fill the stack
89-
for (int k = 0; k < vals.length; k++) {
90-
91-
float value = vals[k];
92-
93-
if (value == 0.0f && (posY == 0.0f || negY == 0.0f)) {
94-
// Take care of the situation of a 0.0 value, which overlaps a non-zero bar
95-
y = value;
96-
yStart = y;
97-
} else if (value >= 0.0f) {
98-
y = posY;
99-
yStart = posY + value;
100-
posY = yStart;
101-
} else {
102-
y = negY;
103-
yStart = negY + Math.abs(value);
104-
negY += Math.abs(value);
105-
}
106-
107-
float left = x - barWidthHalf;
108-
float right = x + barWidthHalf;
109-
float bottom, top;
110-
111-
if (mInverted) {
112-
bottom = y >= yStart ? y : yStart;
113-
top = y <= yStart ? y : yStart;
114-
} else {
115-
top = y >= yStart ? y : yStart;
116-
bottom = y <= yStart ? y : yStart;
117-
}
118-
119-
// multiply the height of the rect with the phase
120-
top *= phaseY;
121-
bottom *= phaseY;
122-
123-
addBar(left, top, right, bottom);
124-
}
88+
for (float value : vals) {
89+
90+
if (value == 0.0f && (posY == 0.0f || negY == 0.0f)) {
91+
// Take care of the situation of a 0.0 value, which overlaps a non-zero bar
92+
y = value;
93+
yStart = y;
94+
} else if (value >= 0.0f) {
95+
y = posY;
96+
yStart = posY + value;
97+
posY = yStart;
98+
} else {
99+
y = negY;
100+
yStart = negY + Math.abs(value);
101+
negY += Math.abs(value);
102+
}
103+
104+
float left = x - barWidthHalf;
105+
float right = x + barWidthHalf;
106+
float bottom, top;
107+
108+
if (mInverted) {
109+
bottom = Math.max(y, yStart);
110+
top = Math.min(y, yStart);
111+
} else {
112+
top = Math.max(y, yStart);
113+
bottom = Math.min(y, yStart);
114+
}
115+
116+
// multiply the height of the rect with the phase
117+
top *= phaseY;
118+
bottom *= phaseY;
119+
120+
addBar(left, top, right, bottom);
121+
}
125122
}
126123
}
127124

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class HorizontalBarBuffer extends BarBuffer {
88

99
public HorizontalBarBuffer(int size, int dataSetCount, boolean containsStacks) {
10-
super(size, dataSetCount, containsStacks);
10+
super(dataSetCount, containsStacks);
1111
}
1212

1313
@Override

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ open class BarChartRenderer(
6161
barData.dataSets.forEach {
6262
barBuffers.add(
6363
BarBuffer(
64-
it.entryCount * 4 * (if (it.isStacked) it.stackSize else 1),
6564
barData.dataSetCount, it.isStacked
6665
)
6766
)
@@ -164,7 +163,7 @@ open class BarChartRenderer(
164163

165164
buffer.feed(dataSet)
166165

167-
trans!!.pointValuesToPixel(buffer.buffer)
166+
trans!!.pointValuesToPixel(buffer.buffer.toFloatArray())
168167

169168
val isCustomFill = dataSet.fills != null && dataSet.fills.isNotEmpty()
170169
val isSingleColor = dataSet.colors.size == 1

0 commit comments

Comments
 (0)