Skip to content

Commit 80ebcb7

Browse files
committed
Improved CandleStickChart performance.
1 parent 6020ed3 commit 80ebcb7

9 files changed

Lines changed: 171 additions & 83 deletions

File tree

‎MPChartExample/res/layout/activity_barchart.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
android:layout_marginBottom="35dp"
3131
android:layout_toLeftOf="@+id/tvXMax"
3232
android:layout_marginRight="5dp"
33-
android:max="2000"
33+
android:max="1500"
3434
android:paddingBottom="12dp" />
3535

3636
<TextView

‎MPChartExample/res/layout/activity_candlechart.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
android:layout_marginBottom="35dp"
3131
android:layout_toLeftOf="@+id/tvXMax"
3232
android:layout_marginRight="5dp"
33-
android:max="500"
33+
android:max="3000"
3434
android:paddingBottom="12dp" />
3535

3636
<TextView

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
182182
@Override
183183
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
184184

185-
tvX.setText("" + (mSeekBarX.getProgress() + 1));
185+
tvX.setText("" + (mSeekBarX.getProgress() * 3 + 1));
186186
tvY.setText("" + (mSeekBarY.getProgress()));
187187

188188
ArrayList<String> xVals = new ArrayList<String>();
@@ -194,7 +194,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
194194
ArrayList<BarEntry> yVals2 = new ArrayList<BarEntry>();
195195
ArrayList<BarEntry> yVals3 = new ArrayList<BarEntry>();
196196

197-
float mult = mSeekBarY.getProgress() * 10000000f;
197+
float mult = mSeekBarY.getProgress() * 1000f;
198198

199199
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
200200
float val = (float) (Math.random() * mult) + 3;
@@ -227,8 +227,8 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
227227
dataSets.add(set3);
228228

229229
BarData data = new BarData(xVals, dataSets);
230-
data.setValueFormatter(new LargeValueFormatter());
231-
230+
// data.setValueFormatter(new LargeValueFormatter());
231+
232232
// add space between the dataset groups in percent of bar-width
233233
data.setGroupSpace(80f);
234234
data.setValueTypeface(tf);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected void onCreate(Bundle savedInstanceState) {
7474
// rightAxis.setStartAtZero(false);
7575

7676
// setting data
77-
mSeekBarX.setProgress(15);
77+
mSeekBarX.setProgress(40);
7878
mSeekBarY.setProgress(100);
7979

8080
mChart.getLegend().setEnabled(false);
@@ -161,10 +161,12 @@ public boolean onOptionsItemSelected(MenuItem item) {
161161
@Override
162162
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
163163

164-
int prog = (mSeekBarX.getProgress() + 1) * 2;
164+
int prog = (mSeekBarX.getProgress() + 1);
165165

166166
tvX.setText("" + prog);
167167
tvY.setText("" + (mSeekBarY.getProgress()));
168+
169+
mChart.resetTracking();
168170

169171
ArrayList<CandleEntry> yVals1 = new ArrayList<CandleEntry>();
170172

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
package com.github.mikephil.charting.buffer;
3+
4+
import com.github.mikephil.charting.data.CandleEntry;
5+
6+
import java.util.ArrayList;
7+
8+
public class CandleBodyBuffer extends AbstractBuffer<CandleEntry> {
9+
10+
private float mBodySpace = 0f;
11+
12+
public CandleBodyBuffer(int size) {
13+
super(size);
14+
}
15+
16+
public void setBodySpace(float bodySpace) {
17+
this.mBodySpace = bodySpace;
18+
}
19+
20+
public void addBody(float left, float top, float right, float bottom) {
21+
22+
buffer[index++] = left;
23+
buffer[index++] = top;
24+
buffer[index++] = right;
25+
buffer[index++] = bottom;
26+
}
27+
28+
@Override
29+
public void feed(ArrayList<CandleEntry> entries) {
30+
31+
for (int i = 0; i < entries.size(); i++) {
32+
33+
CandleEntry e = entries.get(i);
34+
addBody(e.getXIndex() - 0.5f + mBodySpace, e.getClose(), e.getXIndex() + 0.5f - mBodySpace, e.getOpen());
35+
}
36+
37+
reset();
38+
}
39+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
package com.github.mikephil.charting.buffer;
3+
4+
import com.github.mikephil.charting.data.CandleEntry;
5+
6+
import java.util.ArrayList;
7+
8+
public class CandleShadowBuffer extends AbstractBuffer<CandleEntry> {
9+
10+
public CandleShadowBuffer(int size) {
11+
super(size);
12+
}
13+
14+
public void addShadow(float x1, float y1, float x2, float y2) {
15+
16+
buffer[index++] = x1;
17+
buffer[index++] = y1;
18+
buffer[index++] = x2;
19+
buffer[index++] = y2;
20+
}
21+
22+
@Override
23+
public void feed(ArrayList<CandleEntry> entries) {
24+
25+
for (int i = 0; i < entries.size(); i++) {
26+
27+
CandleEntry e = entries.get(i);
28+
addShadow(e.getXIndex(), e.getHigh(), e.getXIndex(), e.getLow());
29+
}
30+
31+
reset();
32+
}
33+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ protected void onDraw(Canvas canvas) {
221221

222222
// canvas.drawBitmap(mDrawBitmap, 0, 0, mDrawPaint);
223223

224-
if (mLogEnabled) {
224+
if (true) {
225225
long drawtime = (System.currentTimeMillis() - starttime);
226226
totalTime += drawtime;
227227
drawCycles += 1;

‎MPChartLib/src/com/github/mikephil/charting/renderer/CandleStickChartRenderer.java‎

Lines changed: 85 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import android.graphics.Paint;
66

77
import com.github.mikephil.charting.animation.ChartAnimator;
8+
import com.github.mikephil.charting.buffer.CandleBodyBuffer;
9+
import com.github.mikephil.charting.buffer.CandleShadowBuffer;
810
import com.github.mikephil.charting.data.CandleData;
911
import com.github.mikephil.charting.data.CandleDataSet;
1012
import com.github.mikephil.charting.data.CandleEntry;
@@ -18,6 +20,9 @@
1820
public class CandleStickChartRenderer extends DataRenderer {
1921

2022
protected CandleDataProvider mChart;
23+
24+
private CandleShadowBuffer[] mShadowBuffers;
25+
private CandleBodyBuffer[] mBodyBuffers;
2126

2227
public CandleStickChartRenderer(CandleDataProvider chart, ChartAnimator animator,
2328
ViewPortHandler viewPortHandler) {
@@ -27,8 +32,15 @@ public CandleStickChartRenderer(CandleDataProvider chart, ChartAnimator animator
2732

2833
@Override
2934
public void initBuffers() {
30-
// TODO Auto-generated method stub
31-
35+
CandleData candleData = mChart.getCandleData();
36+
mShadowBuffers = new CandleShadowBuffer[candleData.getDataSetCount()];
37+
mBodyBuffers = new CandleBodyBuffer[candleData.getDataSetCount()];
38+
39+
for (int i = 0; i < mShadowBuffers.length; i++) {
40+
CandleDataSet set = candleData.getDataSetByIndex(i);
41+
mShadowBuffers[i] = new CandleShadowBuffer(set.getValueCount() * 4);
42+
mBodyBuffers[i] = new CandleBodyBuffer(set.getValueCount() * 4);
43+
}
3244
}
3345

3446
@Override
@@ -44,53 +56,52 @@ public void drawData(Canvas c) {
4456
}
4557

4658
protected void drawDataSet(Canvas c, CandleDataSet dataSet) {
47-
48-
// pre allocate
49-
float[] shadowPoints = new float[4];
50-
float[] bodyPoints = new float[4];
59+
60+
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
61+
calcXBounds(trans);
62+
63+
int dataSetIndex = mChart.getCandleData().getIndexOfDataSet(dataSet);
5164

5265
ArrayList<CandleEntry> entries = dataSet.getYVals();
66+
67+
// draw the shadow
68+
int range = (mMaxX - mMinX) * 4;
69+
int from = mMinX * 4;
70+
71+
CandleShadowBuffer shadowBuffer = mShadowBuffers[dataSetIndex];
72+
shadowBuffer.feed(entries);
5373

74+
trans.pointValuesToPixel(shadowBuffer.buffer);
75+
76+
mRenderPaint.setStyle(Paint.Style.STROKE);
77+
mRenderPaint.setColor(dataSet.getColor());
5478
mRenderPaint.setStrokeWidth(dataSet.getShadowWidth());
5579

56-
for (int j = 0; j < entries.size() * mAnimator.getPhaseX(); j++) {
80+
c.drawLines(shadowBuffer.buffer, from, range, mRenderPaint);
81+
82+
CandleBodyBuffer bodyBuffer = mBodyBuffers[dataSetIndex];
83+
bodyBuffer.setBodySpace(dataSet.getBodySpace());
84+
bodyBuffer.feed(entries);
85+
86+
trans.pointValuesToPixel(bodyBuffer.buffer);
87+
88+
for (int j = 0; j < bodyBuffer.size(); j+=4) {
89+
90+
// get the entry
91+
CandleEntry e = entries.get(j / 4);
92+
93+
if (!fitsBounds(e.getXIndex(), mMinX, mMaxX))
94+
continue;
5795

5896
// get the color that is specified for this position from
5997
// the DataSet, this will reuse colors, if the index is out
6098
// of bounds
6199
mRenderPaint.setColor(dataSet.getColor(j));
62100

63-
// get the entry
64-
CandleEntry e = entries.get(j);
65-
66-
Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());
67-
68-
// transform the entries values for shadow and body
69-
transformShadow(shadowPoints, e, trans);
70-
transformBody(bodyPoints, e, dataSet.getBodySpace(), trans);
71-
72-
float xShadow = shadowPoints[0];
73-
float leftBody = bodyPoints[0];
74-
float rightBody = bodyPoints[2];
75-
76-
float high = shadowPoints[1];
77-
float low = shadowPoints[3];
78-
79-
float open = bodyPoints[1];
80-
float close = bodyPoints[3];
81-
82-
if (!mViewPortHandler.isInBoundsRight(leftBody))
83-
break;
84-
85-
// make sure the lines don't do shitty things outside
86-
// bounds
87-
if (j != 0 && !mViewPortHandler.isInBoundsLeft(rightBody)
88-
&& !mViewPortHandler.isInBoundsTop(low)
89-
&& !mViewPortHandler.isInBoundsBottom(high))
90-
continue;
91-
92-
// draw the shadow
93-
c.drawLine(xShadow, low, xShadow, high, mRenderPaint);
101+
float leftBody = bodyBuffer.buffer[j];
102+
float open = bodyBuffer.buffer[j + 1];
103+
float rightBody = bodyBuffer.buffer[j + 2];
104+
float close = bodyBuffer.buffer[j + 3];
94105

95106
// decide weather the body is hollow or filled
96107
if (open > close) {
@@ -108,42 +119,42 @@ protected void drawDataSet(Canvas c, CandleDataSet dataSet) {
108119
}
109120
}
110121

111-
/**
112-
* Transforms the values of an entry in order to draw the candle-body.
113-
*
114-
* @param bodyPoints
115-
* @param e
116-
* @param bodySpace
117-
*/
118-
private void transformBody(float[] bodyPoints, CandleEntry e, float bodySpace, Transformer trans) {
119-
120-
float phase = mAnimator.getPhaseY();
121-
122-
bodyPoints[0] = e.getXIndex() - 0.5f + bodySpace;
123-
bodyPoints[1] = e.getClose() * phase;
124-
bodyPoints[2] = e.getXIndex() + 0.5f - bodySpace;
125-
bodyPoints[3] = e.getOpen() * phase;
126-
127-
trans.pointValuesToPixel(bodyPoints);
128-
}
129-
130-
/**
131-
* Transforms the values of an entry in order to draw the candle-shadow.
132-
*
133-
* @param shadowPoints
134-
* @param e
135-
*/
136-
private void transformShadow(float[] shadowPoints, CandleEntry e, Transformer trans) {
137-
138-
float phase = mAnimator.getPhaseY();
139-
140-
shadowPoints[0] = e.getXIndex();
141-
shadowPoints[1] = e.getHigh() * phase;
142-
shadowPoints[2] = e.getXIndex();
143-
shadowPoints[3] = e.getLow() * phase;
144-
145-
trans.pointValuesToPixel(shadowPoints);
146-
}
122+
// /**
123+
// * Transforms the values of an entry in order to draw the candle-body.
124+
// *
125+
// * @param bodyPoints
126+
// * @param e
127+
// * @param bodySpace
128+
// */
129+
// private void transformBody(float[] bodyPoints, CandleEntry e, float bodySpace, Transformer trans) {
130+
//
131+
// float phase = mAnimator.getPhaseY();
132+
//
133+
// bodyPoints[0] = e.getXIndex() - 0.5f + bodySpace;
134+
// bodyPoints[1] = e.getClose() * phase;
135+
// bodyPoints[2] = e.getXIndex() + 0.5f - bodySpace;
136+
// bodyPoints[3] = e.getOpen() * phase;
137+
//
138+
// trans.pointValuesToPixel(bodyPoints);
139+
// }
140+
//
141+
// /**
142+
// * Transforms the values of an entry in order to draw the candle-shadow.
143+
// *
144+
// * @param shadowPoints
145+
// * @param e
146+
// */
147+
// private void transformShadow(float[] shadowPoints, CandleEntry e, Transformer trans) {
148+
//
149+
// float phase = mAnimator.getPhaseY();
150+
//
151+
// shadowPoints[0] = e.getXIndex();
152+
// shadowPoints[1] = e.getHigh() * phase;
153+
// shadowPoints[2] = e.getXIndex();
154+
// shadowPoints[3] = e.getLow() * phase;
155+
//
156+
// trans.pointValuesToPixel(shadowPoints);
157+
// }
147158

148159
@Override
149160
public void drawValues(Canvas c) {

‎MPChartLib/src/com/github/mikephil/charting/renderer/XAxisRenderer.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ public void renderGridLines(Canvas c) {
183183

184184
for (int i = minx; i < maxx; i += mXAxis.mAxisLabelModulus) {
185185

186+
if (!fitsBounds(i, minx, maxx))
187+
continue;
188+
186189
position[0] = i;
187190

188191
mTrans.pointValuesToPixel(position);

0 commit comments

Comments
 (0)