Skip to content

Commit 191ec27

Browse files
committed
Bugfixes in LineChart.
1 parent bdac172 commit 191ec27

9 files changed

Lines changed: 91 additions & 67 deletions

File tree

MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity1.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ protected void onCreate(Bundle savedInstanceState) {
7777
// enable scaling and dragging
7878
mChart.setDragEnabled(true);
7979
mChart.setScaleEnabled(true);
80+
// mChart.setScaleXEnabled(true);
81+
// mChart.setScaleYEnabled(true);
8082

8183
// if disabled, scaling can be done on x- and y-axis separately
8284
mChart.setPinchZoom(true);
@@ -94,17 +96,17 @@ protected void onCreate(Bundle savedInstanceState) {
9496
// enable/disable highlight indicators (the lines that indicate the
9597
// highlighted Entry)
9698
mChart.setHighlightIndicatorEnabled(false);
97-
99+
98100
// add data
99101
setData(45, 100);
100102
mChart.animateX(2500);
101-
// mChart.setVisibleYRange(30, AxisDependency.LEFT);
103+
// mChart.setVisibleYRange(30, AxisDependency.LEFT);
102104

103-
// // restrain the maximum scale-out factor
104-
// mChart.setScaleMinima(3f, 3f);
105-
//
106-
// // center the view to a specific position inside the chart
107-
// mChart.centerViewPort(10, 50, AxisDependency.LEFT);
105+
// // restrain the maximum scale-out factor
106+
// mChart.setScaleMinima(3f, 3f);
107+
//
108+
// // center the view to a specific position inside the chart
109+
// mChart.centerViewPort(10, 50, AxisDependency.LEFT);
108110

109111
// get the legend (only possible after setting data)
110112
Legend l = mChart.getLegend();
@@ -268,7 +270,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
268270
tvY.setText("" + (mSeekBarY.getProgress()));
269271

270272
setData(mSeekBarX.getProgress() + 1, mSeekBarY.getProgress());
271-
273+
272274
// redraw
273275
mChart.invalidate();
274276
}
@@ -294,7 +296,8 @@ private void setData(int count, float range) {
294296

295297
ArrayList<Entry> yVals = new ArrayList<Entry>();
296298

297-
for (int i = 10; i < count-10; i++) {
299+
for (int i = 0; i < count; i++) {
300+
298301
float mult = (range + 1);
299302
float val = (float) (Math.random() * mult) + 3;// + (float)
300303
// ((mult *
@@ -337,7 +340,7 @@ private void setData(int count, float range) {
337340
ll2.enableDashedLine(10f, 10f, 0f);
338341
ll2.setLabelPosition(LimitLabelPosition.POS_RIGHT);
339342
ll2.setTextSize(10f);
340-
343+
341344
YAxis leftAxis = mChart.getAxisLeft();
342345
leftAxis.addLimitLine(ll1);
343346
leftAxis.addLimitLine(ll2);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private void setData(int count, float range) {
122122
set1.setDrawValues(false);
123123
set1.setDrawCircles(false);
124124
set1.setDrawCubic(false);
125-
set1.setDrawFilled(false);
125+
set1.setDrawFilled(true);
126126

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

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public abstract class BarLineChartBase<T extends BarLineScatterCandleData<? exte
6464
/** if true, dragging is enabled for the chart */
6565
private boolean mDragEnabled = true;
6666

67-
/** if true, scaling is enabled for the chart */
68-
private boolean mScaleEnabled = true;
67+
private boolean mScaleXEnabled = true;
68+
private boolean mScaleYEnabled = true;
6969

7070
/** if true, data filtering is enabled */
7171
protected boolean mFilterData = false;
@@ -776,22 +776,29 @@ public boolean isDragEnabled() {
776776

777777
/**
778778
* Set this to true to enable scaling (zooming in and out by gesture) for
779-
* the chart (this does not effect dragging).
779+
* the chart (this does not effect dragging) on both X- and Y-Axis.
780780
*
781781
* @param enabled
782782
*/
783783
public void setScaleEnabled(boolean enabled) {
784-
this.mScaleEnabled = enabled;
784+
this.mScaleXEnabled = enabled;
785+
this.mScaleYEnabled = enabled;
785786
}
786-
787-
/**
788-
* Returns true if scaling (zooming in and out by gesture) is enabled for
789-
* the chart, false if not.
790-
*
791-
* @return
792-
*/
793-
public boolean isScaleEnabled() {
794-
return mScaleEnabled;
787+
788+
public void setScaleXEnabled(boolean enabled) {
789+
mScaleXEnabled = enabled;
790+
}
791+
792+
public void setScaleYEnabled(boolean enabled) {
793+
mScaleYEnabled = enabled;
794+
}
795+
796+
public boolean isScaleXEnabled() {
797+
return mScaleXEnabled;
798+
}
799+
800+
public boolean isScaleYEnabled() {
801+
return mScaleYEnabled;
795802
}
796803

797804
/**

MPChartLib/src/com/github/mikephil/charting/charts/Chart.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,9 +1373,16 @@ public PointF getCenterOfView() {
13731373
* @param enabled
13741374
*/
13751375
public void setHardwareAccelerationEnabled(boolean enabled) {
1376-
if (enabled)
1377-
setLayerType(View.LAYER_TYPE_HARDWARE, null);
1378-
else
1379-
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1376+
1377+
if (android.os.Build.VERSION.SDK_INT >= 11) {
1378+
1379+
if (enabled)
1380+
setLayerType(View.LAYER_TYPE_HARDWARE, null);
1381+
else
1382+
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1383+
} else {
1384+
Log.e(LOG_TAG,
1385+
"Cannot enable/disable hardware acceleration for devices below API level 11.");
1386+
}
13801387
}
13811388
}

MPChartLib/src/com/github/mikephil/charting/data/LineDataSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public boolean isDrawCirclesEnabled() {
170170

171171
/**
172172
* If set to true, the linechart lines are drawn in cubic-style instead of
173-
* linear. Default: false
173+
* linear. This affects performance! Default: false
174174
*
175175
* @param enabled
176176
*/

MPChartLib/src/com/github/mikephil/charting/data/LineRadarDataSet.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public abstract class LineRadarDataSet<T extends Entry> extends BarLineScatterCa
1919

2020
/** transparency used for filling line surface */
2121
private int mFillAlpha = 85;
22-
22+
2323
/** the width of the drawn data lines */
2424
private float mLineWidth = 2.5f;
25-
25+
2626
/** if true, the data will also be drawn filled */
2727
private boolean mDrawFilled = false;
28-
29-
// private Shader mShader;
30-
28+
29+
// private Shader mShader;
30+
3131
public LineRadarDataSet(ArrayList<T> yVals, String label) {
3232
super(yVals, label);
3333
}
@@ -69,7 +69,7 @@ public int getFillAlpha() {
6969
public void setFillAlpha(int alpha) {
7070
mFillAlpha = alpha;
7171
}
72-
72+
7373
/**
7474
* set the line width of the chart (min = 0.2f, max = 10f); default 1f NOTE:
7575
* thinner line == better performance, thicker line == worse performance
@@ -93,11 +93,11 @@ public void setLineWidth(float width) {
9393
public float getLineWidth() {
9494
return mLineWidth;
9595
}
96-
96+
9797
/**
9898
* Set to true if the DataSet should be drawn filled (surface), and not just
99-
* as a line, disabling this will give up to 20% performance boost on large
100-
* datasets, default: false
99+
* as a line, disabling this will give great performance boost! default:
100+
* false
101101
*
102102
* @param filled
103103
*/
@@ -113,12 +113,4 @@ public void setDrawFilled(boolean filled) {
113113
public boolean isDrawFilledEnabled() {
114114
return mDrawFilled;
115115
}
116-
117-
// public void setShader(Shader s) {
118-
// mShader = s;
119-
// }
120-
//
121-
// public Shader getShader() {
122-
// return mShader;
123-
// }
124116
}

MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
public class BarLineChartTouchListener<T extends BarLineChartBase<? extends BarLineScatterCandleData<? extends BarLineScatterCandleDataSet<? extends Entry>>>>
2929
extends SimpleOnGestureListener implements OnTouchListener {
3030

31-
// private static final long REFRESH_MILLIS = 20;
31+
// private static final long REFRESH_MILLIS = 20;
3232

3333
/** the original touch-matrix from the chart */
3434
private Matrix mMatrix = new Matrix();
@@ -84,7 +84,7 @@ public boolean onTouch(View v, MotionEvent event) {
8484
mGestureDetector.onTouchEvent(event);
8585
}
8686

87-
if (!mChart.isDragEnabled() && !mChart.isScaleEnabled())
87+
if (!mChart.isDragEnabled() && (!mChart.isScaleXEnabled() && !mChart.isScaleYEnabled()))
8888
return true;
8989

9090
// Handle touch events here...
@@ -140,7 +140,7 @@ public boolean onTouch(View v, MotionEvent event) {
140140

141141
mChart.disableScroll();
142142

143-
if (mChart.isScaleEnabled())
143+
if (mChart.isScaleXEnabled() || mChart.isScaleYEnabled())
144144
performZoom(event);
145145

146146
} else if (mTouchMode == NONE
@@ -168,18 +168,19 @@ public boolean onTouch(View v, MotionEvent event) {
168168
}
169169

170170
// Perform the transformation, update the chart
171-
// if (needsRefresh())
172-
mMatrix = mChart.getViewPortHandler().refresh(mMatrix, mChart, true);
171+
// if (needsRefresh())
172+
mMatrix = mChart.getViewPortHandler().refresh(mMatrix, mChart, true);
173173

174174
return true; // indicate event was handled
175175
}
176176

177-
// private boolean needsRefresh() {
178-
// if (System.currentTimeMillis() - mChart.getLastDrawMillis() > REFRESH_MILLIS) {
179-
// return true;
180-
// } else
181-
// return false;
182-
// }
177+
// private boolean needsRefresh() {
178+
// if (System.currentTimeMillis() - mChart.getLastDrawMillis() >
179+
// REFRESH_MILLIS) {
180+
// return true;
181+
// } else
182+
// return false;
183+
// }
183184

184185
/**
185186
* ################ ################ ################ ################
@@ -248,9 +249,10 @@ private void performZoom(MotionEvent event) {
248249
// scale
249250

250251
mMatrix.set(mSavedMatrix);
251-
mMatrix.postScale(scale, scale, t.x, t.y);
252+
mMatrix.postScale((mChart.isScaleXEnabled()) ? scale : 1f,
253+
(mChart.isScaleYEnabled()) ? scale : 1f, t.x, t.y);
252254

253-
} else if (mTouchMode == X_ZOOM) {
255+
} else if (mTouchMode == X_ZOOM && mChart.isScaleXEnabled()) {
254256

255257
float xDist = getXDist(event);
256258
float scaleX = xDist / mSavedXDist; // x-axis
@@ -259,7 +261,7 @@ private void performZoom(MotionEvent event) {
259261
mMatrix.set(mSavedMatrix);
260262
mMatrix.postScale(scaleX, 1f, t.x, t.y);
261263

262-
} else if (mTouchMode == Y_ZOOM) {
264+
} else if (mTouchMode == Y_ZOOM && mChart.isScaleYEnabled()) {
263265

264266
float yDist = getYDist(event);
265267
float scaleY = yDist / mSavedYDist; // y-axis

MPChartLib/src/com/github/mikephil/charting/renderer/LineChartRenderer.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ protected void drawLinear(Canvas c, LineDataSet dataSet, ArrayList<Entry> entrie
304304
// more than 1 color
305305
if (dataSet.getColors().size() > 1) {
306306

307-
for (int j = 0; j < buffer.size() - 3; j += 2) {
307+
for (int j = 0; j < buffer.size(); j += 4) {
308308

309309
if (!mViewPortHandler.isInBoundsRight(buffer.buffer[j]))
310310
break;
@@ -319,20 +319,27 @@ protected void drawLinear(Canvas c, LineDataSet dataSet, ArrayList<Entry> entrie
319319
continue;
320320

321321
// get the color that is set for this line-segment
322-
mRenderPaint.setColor(dataSet.getColor(j / 2));
322+
mRenderPaint.setColor(dataSet.getColor(j / 4));
323323

324324
c.drawLine(buffer.buffer[j], buffer.buffer[j + 1],
325325
buffer.buffer[j + 2], buffer.buffer[j + 3], mRenderPaint);
326326
}
327327

328328
} else { // only one color per dataset
329-
330-
int from = mMinX * 4;
331-
int range = (mMaxX * 4 - mMinX) + 4;
329+
330+
Entry entryFrom = dataSet.getEntryForXIndex(mMinX);
331+
Entry entryTo = dataSet.getEntryForXIndex(mMaxX);
332+
333+
int minx = dataSet.getEntryPosition(entryFrom);
334+
int maxx = dataSet.getEntryPosition(entryTo);
335+
336+
int from = minx * 4;
337+
int range = (maxx * 4 - from) + 4;
332338
int to = range + from;
333339

334340
mRenderPaint.setColor(dataSet.getColor());
335-
341+
342+
// c.drawLines(buffer.buffer, mRenderPaint);
336343
c.drawLines(buffer.buffer, from, to >= buffer.size() ? buffer.size() - from : range,
337344
mRenderPaint);
338345
}
@@ -347,6 +354,12 @@ protected void drawLinear(Canvas c, LineDataSet dataSet, ArrayList<Entry> entrie
347354

348355
protected void drawLinearFill(Canvas c, LineDataSet dataSet, ArrayList<Entry> entries,
349356
Transformer trans) {
357+
358+
Entry entryFrom = dataSet.getEntryForXIndex(mMinX-2);
359+
Entry entryTo = dataSet.getEntryForXIndex(mMaxX+2);
360+
361+
int minx = dataSet.getEntryPosition(entryFrom);
362+
int maxx = dataSet.getEntryPosition(entryTo);
350363

351364
mRenderPaint.setStyle(Paint.Style.FILL);
352365

@@ -357,7 +370,7 @@ protected void drawLinearFill(Canvas c, LineDataSet dataSet, ArrayList<Entry> en
357370
Path filled = generateFilledPath(
358371
entries,
359372
mChart.getFillFormatter().getFillLinePosition(dataSet, mChart.getLineData(),
360-
mChart.getYChartMax(), mChart.getYChartMin()), mMinX, mMaxX + 1);
373+
mChart.getYChartMax(), mChart.getYChartMin()), minx, maxx);
361374

362375
trans.pathValueToPixel(filled);
363376

screenshots/combined_chart.png

94.9 KB
Loading

0 commit comments

Comments
 (0)