Skip to content

Commit c739d62

Browse files
committed
Performance improvements concerning LineChart, still bugs.
1 parent edd5132 commit c739d62

22 files changed

Lines changed: 396 additions & 108 deletions

‎MPChartExample/AndroidManifest.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
<application
1313
android:allowBackup="true"
14+
android:hardwareAccelerated="true"
1415
android:icon="@drawable/ic_launcher"
1516
android:label="@string/app_name"
1617
android:theme="@style/AppTheme" >

‎MPChartExample/res/layout/activity_performance_linechart.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
android:layout_marginBottom="35dp"
2020
android:layout_toLeftOf="@+id/tvValueCount"
2121
android:layout_marginRight="5dp"
22-
android:max="3000"
22+
android:max="29000"
2323
android:paddingBottom="12dp" />
2424

2525
<TextView
2626
android:id="@+id/tvValueCount"
27-
android:layout_width="50dp"
27+
android:layout_width="60dp"
2828
android:layout_height="wrap_content"
2929
android:layout_alignBottom="@+id/seekbarValues"
3030
android:layout_alignParentRight="true"

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ protected void onCreate(Bundle savedInstanceState) {
7474
@Override
7575
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
7676

77-
mTvCount.setText("" + (mSeekBarValues.getProgress()));
77+
int count = mSeekBarValues.getProgress() + 1000;
78+
mTvCount.setText("" + count);
79+
80+
mChart.resetTracking();
7881

79-
setData(mSeekBarValues.getProgress(), 500f);
82+
setData(count, 500f);
8083

8184
// redraw
8285
mChart.invalidate();
@@ -115,9 +118,11 @@ private void setData(int count, float range) {
115118
LineDataSet set1 = new LineDataSet(yVals, "DataSet 1");
116119

117120
set1.setColor(Color.BLACK);
118-
set1.setLineWidth(1f);
121+
set1.setLineWidth(0.5f);
119122
set1.setDrawValues(false);
120123
set1.setDrawCircles(false);
124+
set1.setDrawCubic(false);
125+
set1.setDrawFilled(false);
121126

122127
// create a data object with the datasets
123128
LineData data = new LineData(xVals, set1);

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/notimportant/MainActivity.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ protected void onCreate(Bundle savedInstanceState) {
111111
objects.add(new ContentItem(
112112
"Dynamical data adding",
113113
"This Activity demonstrates dynamical adding of Entries and DataSets (real time graph)."));
114-
// objects.add(new ContentItem(
115-
// "Performance Line Chart",
116-
// "Renders up to 3000 objects."));
114+
objects.add(new ContentItem(
115+
"Performance Line Chart",
116+
"Renders up to 30.000 objects smoothly."));
117117

118118
MyAdapter adapter = new MyAdapter(this, objects);
119119

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
package com.github.mikephil.charting.buffer;
3+
4+
import java.util.ArrayList;
5+
6+
/**
7+
* Buffer class to boost performance while drawing.
8+
*
9+
* @author Philipp Jahoda
10+
* @param <T>
11+
*/
12+
public abstract class AbstractBuffer<T> {
13+
14+
protected int index = 0;
15+
public final float[] buffer;
16+
17+
/**
18+
* Initialization with buffer-size.
19+
*
20+
* @param size
21+
*/
22+
public AbstractBuffer(int size) {
23+
index = 0;
24+
buffer = new float[size];
25+
}
26+
27+
/**
28+
* Resets the buffer index to 0.
29+
*/
30+
public void reset() {
31+
index = 0;
32+
}
33+
34+
/**
35+
* Returns the size of the buffer array.
36+
*
37+
* @return
38+
*/
39+
public int size() {
40+
return buffer.length;
41+
}
42+
43+
/**
44+
* Builds up the buffer with the provided data and resets the buffer-index
45+
* after feed-completion.
46+
*
47+
* @param entries
48+
*/
49+
public abstract void feed(ArrayList<T> entries);
50+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
package com.github.mikephil.charting.buffer;
3+
4+
import com.github.mikephil.charting.data.Entry;
5+
6+
import java.util.ArrayList;
7+
8+
public class LineBuffer extends AbstractBuffer<Entry> {
9+
10+
public LineBuffer(int size) {
11+
super(size);
12+
}
13+
14+
public void moveTo(float x, float y) {
15+
16+
if (index != 0)
17+
return;
18+
19+
buffer[index++] = x;
20+
buffer[index++] = y;
21+
}
22+
23+
public void lineTo(float x, float y) {
24+
25+
if (index == 2) {
26+
buffer[index++] = x;
27+
buffer[index++] = y;
28+
} else {
29+
30+
float prevX = buffer[index - 2];
31+
float prevY = buffer[index - 1];
32+
buffer[index++] = prevX;
33+
buffer[index++] = prevY;
34+
buffer[index++] = x;
35+
buffer[index++] = y;
36+
}
37+
}
38+
39+
@Override
40+
public void feed(ArrayList<Entry> entries) {
41+
moveTo(entries.get(0).getXIndex(), entries.get(0).getVal());
42+
43+
for (int i = 1; i < entries.size(); i++) {
44+
45+
Entry e = entries.get(i);
46+
lineTo(e.getXIndex(), e.getVal());
47+
}
48+
49+
reset();
50+
}
51+
}

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

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ protected void init() {
143143
mGridBackgroundPaint.setColor(Color.rgb(240, 240, 240)); // light
144144
// grey
145145
}
146+
147+
// for performance tracking
148+
private long totalTime = 0;
149+
private long drawCycles = 0;
146150

147151
@Override
148152
protected void onDraw(Canvas canvas) {
@@ -169,59 +173,70 @@ protected void onDraw(Canvas canvas) {
169173
calcModulus();
170174

171175
// execute all drawing commands
172-
drawGridBackground();
176+
drawGridBackground(canvas);
173177

174178
mAxisRendererLeft.computeAxis(mAxisLeft.mAxisMinimum, mAxisLeft.mAxisMaximum);
175179
mAxisRendererRight.computeAxis(mAxisRight.mAxisMinimum, mAxisRight.mAxisMaximum);
176180

177181
// make sure the graph values and grid cannot be drawn outside the
178182
// content-rect
179-
int clipRestoreCount = mDrawCanvas.save();
180-
mDrawCanvas.clipRect(mViewPortHandler.getContentRect());
183+
int clipRestoreCount = canvas.save();
184+
canvas.clipRect(mViewPortHandler.getContentRect());
181185

182-
mXAxisRenderer.renderGridLines(mDrawCanvas);
183-
mAxisRendererLeft.renderGridLines(mDrawCanvas);
184-
mAxisRendererRight.renderGridLines(mDrawCanvas);
186+
mXAxisRenderer.renderGridLines(canvas);
187+
mAxisRendererLeft.renderGridLines(canvas);
188+
mAxisRendererRight.renderGridLines(canvas);
185189

186-
mAxisRendererLeft.renderLimitLines(mDrawCanvas);
187-
mAxisRendererRight.renderLimitLines(mDrawCanvas);
188-
189-
mRenderer.drawData(mDrawCanvas);
190+
mRenderer.drawData(canvas);
191+
192+
mAxisRendererLeft.renderLimitLines(canvas);
193+
mAxisRendererRight.renderLimitLines(canvas);
190194

191195
// if highlighting is enabled
192196
if (mHighlightEnabled && mHighLightIndicatorEnabled && valuesToHighlight())
193-
mRenderer.drawHighlighted(mDrawCanvas, mIndicesToHightlight);
197+
mRenderer.drawHighlighted(canvas, mIndicesToHightlight);
194198

195199
// Removes clipping rectangle
196-
mDrawCanvas.restoreToCount(clipRestoreCount);
200+
canvas.restoreToCount(clipRestoreCount);
197201

198-
mRenderer.drawExtras(mDrawCanvas);
202+
mRenderer.drawExtras(canvas);
199203

200-
mXAxisRenderer.renderAxisLabels(mDrawCanvas);
201-
mXAxisRenderer.renderAxisLine(mDrawCanvas);
204+
mXAxisRenderer.renderAxisLabels(canvas);
205+
mXAxisRenderer.renderAxisLine(canvas);
202206

203-
mAxisRendererLeft.renderAxisLabels(mDrawCanvas);
204-
mAxisRendererLeft.renderAxisLine(mDrawCanvas);
207+
mAxisRendererLeft.renderAxisLabels(canvas);
208+
mAxisRendererLeft.renderAxisLine(canvas);
205209

206-
mAxisRendererRight.renderAxisLabels(mDrawCanvas);
207-
mAxisRendererRight.renderAxisLine(mDrawCanvas);
210+
mAxisRendererRight.renderAxisLabels(canvas);
211+
mAxisRendererRight.renderAxisLine(canvas);
208212

209-
mRenderer.drawValues(mDrawCanvas);
213+
mRenderer.drawValues(canvas);
210214

211-
mLegendRenderer.renderLegend(mDrawCanvas, mLegend);
215+
mLegendRenderer.renderLegend(canvas, mLegend);
212216
// drawLegend();
213217

214-
drawMarkers();
218+
drawMarkers(canvas);
215219

216-
drawDescription();
220+
drawDescription(canvas);
217221

218-
canvas.drawBitmap(mDrawBitmap, 0, 0, mDrawPaint);
222+
// canvas.drawBitmap(mDrawBitmap, 0, 0, mDrawPaint);
219223

220-
if (mLogEnabled) {
224+
if (true) {
221225
long drawtime = (System.currentTimeMillis() - starttime);
222-
Log.i(LOG_TAG, "DrawTime: " + drawtime + " ms");
226+
totalTime += drawtime;
227+
drawCycles += 1;
228+
long average = totalTime / drawCycles;
229+
Log.i(LOG_TAG, "Drawtime: " + drawtime + " ms, average: " + average + " ms, cycles: " + drawCycles);
223230
}
224231
}
232+
233+
/**
234+
* RESET PERFORMANCE TRACKING FIELDS
235+
*/
236+
public void resetTracking() {
237+
totalTime = 0;
238+
drawCycles = 0;
239+
}
225240

226241
protected void prepareValuePxMatrix() {
227242

@@ -252,6 +267,8 @@ public void notifyDataSetChanged() {
252267
if (mLogEnabled)
253268
Log.i(LOG_TAG, "Preparing...");
254269
}
270+
271+
mRenderer.initBuffers();
255272

256273
calcMinMax();
257274

@@ -429,13 +446,13 @@ protected float[] getMarkerPosition(Entry e, int dataSetIndex) {
429446
/**
430447
* draws the grid background
431448
*/
432-
protected void drawGridBackground() {
449+
protected void drawGridBackground(Canvas c) {
433450

434451
if (!mDrawGridBackground)
435452
return;
436453

437454
// draw the grid background
438-
mDrawCanvas.drawRect(mViewPortHandler.getContentRect(), mGridBackgroundPaint);
455+
c.drawRect(mViewPortHandler.getContentRect(), mGridBackgroundPaint);
439456
}
440457

441458
/**

0 commit comments

Comments
 (0)