Skip to content

Commit fa97f61

Browse files
committed
Performance improvements concerning BarChart.
1 parent 80ebcb7 commit fa97f61

20 files changed

Lines changed: 278 additions & 149 deletions

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/CubicLineChartActivity.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private void setData(int count, float range) {
260260

261261
ArrayList<Entry> vals1 = new ArrayList<Entry>();
262262

263-
for (int i = 0; i < count; i++) {
263+
for (int i = 5; i < count / 2; i+=2) {
264264
float mult = (range + 1);
265265
float val = (float) (Math.random() * mult) + 20;// + (float)
266266
// ((mult *

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/DynamicalAddingActivity.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ protected void onCreate(Bundle savedInstanceState) {
3838

3939
// add an empty data object
4040
mChart.setData(new LineData());
41+
// mChart.getXAxis().setDrawLabels(false);
42+
// mChart.getXAxis().setDrawGridLines(false);
4143

4244
mChart.invalidate();
4345
}

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/StackedBarActivity.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ protected void onCreate(Bundle savedInstanceState) {
8181
yLabels.setValueFormatter(new MyValueFormatter());
8282

8383
mChart.getAxisRight().setValueFormatter(new MyValueFormatter());
84+
mChart.getAxisRight().setDrawGridLines(false);
8485

8586
XAxis xLabels = mChart.getXAxis();
8687
xLabels.setPosition(XAxisPosition.TOP);
@@ -223,7 +224,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
223224
}
224225

225226
BarDataSet set1 = new BarDataSet(yVals1, "Statistics Vienna 2014");
226-
set1.setColors(ColorTemplate.VORDIPLOM_COLORS);
227+
set1.setColors(getColors());
227228
set1.setStackLabels(new String[] {
228229
"Births", "Divorces", "Marriages"
229230
});
@@ -263,4 +264,18 @@ public void onNothingSelected() {
263264
// TODO Auto-generated method stub
264265

265266
}
267+
268+
private int[] getColors() {
269+
270+
int stacksize = 3;
271+
272+
// have as many colors as stack-values per entry
273+
int []colors = new int[stacksize];
274+
275+
for(int i = 0; i < stacksize; i++) {
276+
colors[i] = ColorTemplate.VORDIPLOM_COLORS[i];
277+
}
278+
279+
return colors;
280+
}
266281
}

‎MPChartLib/src/com/github/mikephil/charting/buffer/AbstractBuffer.java‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@
44
import java.util.ArrayList;
55

66
/**
7-
* Buffer class to boost performance while drawing.
7+
* Buffer class to boost performance while drawing. Concept: Replace instead of
8+
* recreate.
89
*
910
* @author Philipp Jahoda
10-
* @param <T>
11+
* @param <T> The data the buffer accepts to be fed with.
1112
*/
1213
public abstract class AbstractBuffer<T> {
1314

15+
/** index in the buffer */
1416
protected int index = 0;
17+
18+
/** float-buffer that holds the data points to draw, order: x,y,x,y,... */
1519
public final float[] buffer;
1620

21+
/** animation phase x-axis */
22+
protected float phaseX = 1f;
23+
24+
/** animation phase y-axis */
25+
protected float phaseY = 1f;
26+
1727
/**
1828
* Initialization with buffer-size.
1929
*
@@ -25,24 +35,35 @@ public AbstractBuffer(int size) {
2535
}
2636

2737
/**
28-
* Resets the buffer index to 0.
38+
* Resets the buffer index to 0 and makes the buffer reusable.
2939
*/
3040
public void reset() {
3141
index = 0;
3242
}
3343

3444
/**
35-
* Returns the size of the buffer array.
45+
* Returns the size (length) of the buffer array.
3646
*
3747
* @return
3848
*/
3949
public int size() {
4050
return buffer.length;
4151
}
4252

53+
/**
54+
* Set the phases used for animations.
55+
*
56+
* @param phaseX
57+
* @param phaseY
58+
*/
59+
public void setPhases(float phaseX, float phaseY) {
60+
this.phaseX = phaseX;
61+
this.phaseY = phaseY;
62+
}
63+
4364
/**
4465
* Builds up the buffer with the provided data and resets the buffer-index
45-
* after feed-completion.
66+
* after feed-completion. This needs to run FAST.
4667
*
4768
* @param entries
4869
*/
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}

‎MPChartLib/src/com/github/mikephil/charting/buffer/CandleBodyBuffer.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void setBodySpace(float bodySpace) {
1717
this.mBodySpace = bodySpace;
1818
}
1919

20-
public void addBody(float left, float top, float right, float bottom) {
20+
private void addBody(float left, float top, float right, float bottom) {
2121

2222
buffer[index++] = left;
2323
buffer[index++] = top;
@@ -27,11 +27,13 @@ public void addBody(float left, float top, float right, float bottom) {
2727

2828
@Override
2929
public void feed(ArrayList<CandleEntry> entries) {
30+
31+
float size = entries.size() * phaseX;
3032

31-
for (int i = 0; i < entries.size(); i++) {
33+
for (int i = 0; i < size; i++) {
3234

3335
CandleEntry e = entries.get(i);
34-
addBody(e.getXIndex() - 0.5f + mBodySpace, e.getClose(), e.getXIndex() + 0.5f - mBodySpace, e.getOpen());
36+
addBody(e.getXIndex() - 0.5f + mBodySpace, e.getClose() * phaseY, e.getXIndex() + 0.5f - mBodySpace, e.getOpen() * phaseY);
3537
}
3638

3739
reset();

‎MPChartLib/src/com/github/mikephil/charting/buffer/CandleShadowBuffer.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public CandleShadowBuffer(int size) {
1111
super(size);
1212
}
1313

14-
public void addShadow(float x1, float y1, float x2, float y2) {
14+
private void addShadow(float x1, float y1, float x2, float y2) {
1515

1616
buffer[index++] = x1;
1717
buffer[index++] = y1;
@@ -21,11 +21,13 @@ public void addShadow(float x1, float y1, float x2, float y2) {
2121

2222
@Override
2323
public void feed(ArrayList<CandleEntry> entries) {
24+
25+
float size = entries.size() * phaseX;
2426

25-
for (int i = 0; i < entries.size(); i++) {
27+
for (int i = 0; i < size; i++) {
2628

2729
CandleEntry e = entries.get(i);
28-
addShadow(e.getXIndex(), e.getHigh(), e.getXIndex(), e.getLow());
30+
addShadow(e.getXIndex(), e.getHigh() * phaseY, e.getXIndex(), e.getLow() * phaseY);
2931
}
3032

3133
reset();

‎MPChartLib/src/com/github/mikephil/charting/buffer/LineBuffer.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public void moveTo(float x, float y) {
1818

1919
buffer[index++] = x;
2020
buffer[index++] = y;
21+
22+
// in case just one entry, this is overwritten when lineTo is called
23+
buffer[index] = x;
24+
buffer[index + 1] = y;
2125
}
2226

2327
public void lineTo(float x, float y) {
@@ -39,11 +43,13 @@ public void lineTo(float x, float y) {
3943
@Override
4044
public void feed(ArrayList<Entry> entries) {
4145
moveTo(entries.get(0).getXIndex(), entries.get(0).getVal());
46+
47+
float size = entries.size() * phaseX;
4248

43-
for (int i = 1; i < entries.size(); i++) {
49+
for (int i = 1; i < size; i++) {
4450

4551
Entry e = entries.get(i);
46-
lineTo(e.getXIndex(), e.getVal());
52+
lineTo(e.getXIndex(), e.getVal() * phaseY);
4753
}
4854

4955
reset();

‎MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ protected void calcModulus() {
414414
if (mLogEnabled)
415415
Log.i(LOG_TAG, "X-Axis modulus: " + mXAxis.mAxisLabelModulus + ", x-axis label width: "
416416
+ mXAxis.mLabelWidth + ", content width: " + mViewPortHandler.contentWidth());
417+
418+
if (mXAxis.mAxisLabelModulus < 1)
419+
mXAxis.mAxisLabelModulus = 1;
417420
}
418421

419422
@Override

‎MPChartLib/src/com/github/mikephil/charting/charts/HorizontalBarChart.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ protected void calcModulus() {
137137
mXAxis.mAxisLabelModulus = (int) Math
138138
.ceil((mData.getXValCount() * mXAxis.mLabelHeight)
139139
/ (mViewPortHandler.contentHeight() * values[Matrix.MSCALE_Y]));
140+
141+
if (mXAxis.mAxisLabelModulus < 1)
142+
mXAxis.mAxisLabelModulus = 1;
140143
}
141144

142145
@Override

0 commit comments

Comments
 (0)