Skip to content

Commit 502767e

Browse files
RobertZagorskihannesa2
authored andcommitted
Fix IndexOutOfBoundsException thrown when drawing data set values (#2450)
Crash was happening, when one of data in charts' data sets was empty. The fix is preventing from drawing anything on empty data set. Fixed for all chart types. Issues with the same stacktrace: #2450 #2455 #2849 #2869 #2952 #3011 #3124 #3171 #3213 #3361 #3566 #3623 #3696 #3755 #4015
1 parent 32aef31 commit 502767e

8 files changed

Lines changed: 70 additions & 42 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,12 @@ public void drawValues(Canvas c) {
229229
for (int i = 0; i < mChart.getBarData().getDataSetCount(); i++) {
230230

231231
IBarDataSet dataSet = dataSets.get(i);
232-
233-
if (!shouldDrawValues(dataSet))
232+
if (dataSet.getEntryCount() == 0) {
233+
continue;
234+
}
235+
if (!shouldDrawValues(dataSet)) {
234236
continue;
237+
}
235238

236239
// apply the text-styling defined by the DataSet
237240
applyValueTextStyle(dataSet);

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,12 @@ public void drawValues(Canvas c) {
130130
for (int i = 0; i < dataSets.size(); i++) {
131131

132132
IBubbleDataSet dataSet = dataSets.get(i);
133-
134-
if (!shouldDrawValues(dataSet))
133+
if (dataSet.getEntryCount() == 0) {
134+
continue;
135+
}
136+
if (!shouldDrawValues(dataSet)) {
135137
continue;
138+
}
136139

137140
// apply the text-styling defined by the DataSet
138141
applyValueTextStyle(dataSet);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,12 @@ public void drawValues(Canvas c) {
263263
for (int i = 0; i < dataSets.size(); i++) {
264264

265265
ICandleDataSet dataSet = dataSets.get(i);
266-
267-
if (!shouldDrawValues(dataSet))
266+
if (dataSet.getEntryCount() == 0) {
267+
continue;
268+
}
269+
if (!shouldDrawValues(dataSet)) {
268270
continue;
271+
}
269272

270273
// apply the text-styling defined by the DataSet
271274
applyValueTextStyle(dataSet);

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
7575
final float barWidthHalf = barWidth / 2.0f;
7676
float x;
7777

78-
for (int i = 0, count = Math.min((int)(Math.ceil((float)(dataSet.getEntryCount()) * phaseX)), dataSet.getEntryCount());
78+
for (int i = 0, count = Math.min((int) (Math.ceil((float) (dataSet.getEntryCount()) * phaseX)), dataSet.getEntryCount());
7979
i < count;
8080
i++) {
8181

@@ -157,9 +157,12 @@ public void drawValues(Canvas c) {
157157
for (int i = 0; i < mChart.getBarData().getDataSetCount(); i++) {
158158

159159
IBarDataSet dataSet = dataSets.get(i);
160-
161-
if (!shouldDrawValues(dataSet))
160+
if (dataSet.getEntryCount() == 0) {
161+
continue;
162+
}
163+
if (!shouldDrawValues(dataSet)) {
162164
continue;
165+
}
163166

164167
boolean isInverted = mChart.isInverted(dataSet.getAxisDependency());
165168

@@ -229,8 +232,8 @@ public void drawValues(Canvas c) {
229232
Utils.drawImage(
230233
c,
231234
icon,
232-
(int)px,
233-
(int)py,
235+
(int) px,
236+
(int) py,
234237
icon.getIntrinsicWidth(),
235238
icon.getIntrinsicHeight());
236239
}
@@ -300,8 +303,8 @@ public void drawValues(Canvas c) {
300303
Utils.drawImage(
301304
c,
302305
icon,
303-
(int)px,
304-
(int)py,
306+
(int) px,
307+
(int) py,
305308
icon.getIntrinsicWidth(),
306309
icon.getIntrinsicHeight());
307310
}
@@ -378,8 +381,8 @@ public void drawValues(Canvas c) {
378381
Utils.drawImage(
379382
c,
380383
icon,
381-
(int)(x + iconsOffset.x),
382-
(int)(y + iconsOffset.y),
384+
(int) (x + iconsOffset.x),
385+
(int) (y + iconsOffset.y),
383386
icon.getIntrinsicWidth(),
384387
icon.getIntrinsicHeight());
385388
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,12 @@ public void drawValues(Canvas c) {
529529
for (int i = 0; i < dataSets.size(); i++) {
530530

531531
ILineDataSet dataSet = dataSets.get(i);
532-
533-
if (!shouldDrawValues(dataSet))
532+
if (dataSet.getEntryCount() == 0) {
533+
continue;
534+
}
535+
if (!shouldDrawValues(dataSet)) {
534536
continue;
537+
}
535538

536539
// apply the text-styling defined by the DataSet
537540
applyValueTextStyle(dataSet);

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,13 @@ public void drawValues(Canvas c) {
423423
for (int i = 0; i < dataSets.size(); i++) {
424424

425425
IPieDataSet dataSet = dataSets.get(i);
426-
426+
if (dataSet.getEntryCount() == 0) {
427+
continue;
428+
}
427429
final boolean drawValues = dataSet.isDrawValuesEnabled();
428-
429-
if (!drawValues && !drawEntryLabels)
430+
if (!drawValues && !drawEntryLabels) {
430431
continue;
432+
}
431433

432434
final PieDataSet.ValuePosition xValuePosition = dataSet.getXValuePosition();
433435
final PieDataSet.ValuePosition yValuePosition = dataSet.getYValuePosition();
@@ -520,7 +522,7 @@ public void drawValues(Canvas c) {
520522

521523
mValuePaint.setTextAlign(Align.RIGHT);
522524

523-
if(drawXOutside)
525+
if (drawXOutside)
524526
mEntryLabelsPaint.setTextAlign(Align.RIGHT);
525527

526528
labelPtx = pt2x - offset;
@@ -530,7 +532,7 @@ public void drawValues(Canvas c) {
530532
pt2y = pt1y;
531533
mValuePaint.setTextAlign(Align.LEFT);
532534

533-
if(drawXOutside)
535+
if (drawXOutside)
534536
mEntryLabelsPaint.setTextAlign(Align.LEFT);
535537

536538
labelPtx = pt2x + offset;
@@ -611,8 +613,8 @@ public void drawValues(Canvas c) {
611613
Utils.drawImage(
612614
c,
613615
icon,
614-
(int)x,
615-
(int)y,
616+
(int) x,
617+
(int) y,
616618
icon.getIntrinsicWidth(),
617619
icon.getIntrinsicHeight());
618620
}
@@ -690,6 +692,7 @@ protected void drawHole(Canvas c) {
690692
}
691693

692694
protected Path mDrawCenterTextPathBuffer = new Path();
695+
693696
/**
694697
* draws the description text in the center of the pie chart makes most
695698
* sense when center-hole is enabled
@@ -763,6 +766,7 @@ protected void drawCenterText(Canvas c) {
763766
}
764767

765768
protected RectF mDrawHighlightedRectF = new RectF();
769+
766770
@Override
767771
public void drawHighlighted(Canvas c, Highlight[] indices) {
768772

@@ -782,7 +786,7 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
782786
: 0.f;
783787

784788
final RectF highlightedCircleBox = mDrawHighlightedRectF;
785-
highlightedCircleBox.set(0,0,0,0);
789+
highlightedCircleBox.set(0, 0, 0, 0);
786790

787791
for (int i = 0; i < indices.length; i++) {
788792

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public void drawData(Canvas c) {
7070
}
7171

7272
protected Path mDrawDataSetSurfacePathBuffer = new Path();
73+
7374
/**
7475
* Draws the RadarDataSet
7576
*
@@ -89,7 +90,7 @@ protected void drawDataSet(Canvas c, IRadarDataSet dataSet, int mostEntries) {
8990
float factor = mChart.getFactor();
9091

9192
MPPointF center = mChart.getCenterOffsets();
92-
MPPointF pOut = MPPointF.getInstance(0,0);
93+
MPPointF pOut = MPPointF.getInstance(0, 0);
9394
Path surface = mDrawDataSetSurfacePathBuffer;
9495
surface.reset();
9596

@@ -159,17 +160,20 @@ public void drawValues(Canvas c) {
159160
float factor = mChart.getFactor();
160161

161162
MPPointF center = mChart.getCenterOffsets();
162-
MPPointF pOut = MPPointF.getInstance(0,0);
163-
MPPointF pIcon = MPPointF.getInstance(0,0);
163+
MPPointF pOut = MPPointF.getInstance(0, 0);
164+
MPPointF pIcon = MPPointF.getInstance(0, 0);
164165

165166
float yoffset = Utils.convertDpToPixel(5f);
166167

167168
for (int i = 0; i < mChart.getData().getDataSetCount(); i++) {
168169

169170
IRadarDataSet dataSet = mChart.getData().getDataSetByIndex(i);
170-
171-
if (!shouldDrawValues(dataSet))
171+
if (dataSet.getEntryCount() == 0) {
172+
continue;
173+
}
174+
if (!shouldDrawValues(dataSet)) {
172175
continue;
176+
}
173177

174178
// apply the text-styling defined by the DataSet
175179
applyValueTextStyle(dataSet);
@@ -182,11 +186,11 @@ public void drawValues(Canvas c) {
182186

183187
RadarEntry entry = dataSet.getEntryForIndex(j);
184188

185-
Utils.getPosition(
186-
center,
187-
(entry.getY() - mChart.getYChartMin()) * factor * phaseY,
188-
sliceangle * j * phaseX + mChart.getRotationAngle(),
189-
pOut);
189+
Utils.getPosition(
190+
center,
191+
(entry.getY() - mChart.getYChartMin()) * factor * phaseY,
192+
sliceangle * j * phaseX + mChart.getRotationAngle(),
193+
pOut);
190194

191195
if (dataSet.isDrawValuesEnabled()) {
192196
drawValue(c,
@@ -216,8 +220,8 @@ public void drawValues(Canvas c) {
216220
Utils.drawImage(
217221
c,
218222
icon,
219-
(int)pIcon.x,
220-
(int)pIcon.y,
223+
(int) pIcon.x,
224+
(int) pIcon.y,
221225
icon.getIntrinsicWidth(),
222226
icon.getIntrinsicHeight());
223227
}
@@ -255,7 +259,7 @@ protected void drawWeb(Canvas c) {
255259
final int xIncrements = 1 + mChart.getSkipWebLineCount();
256260
int maxEntryCount = mChart.getData().getMaxEntryCountSet().getEntryCount();
257261

258-
MPPointF p = MPPointF.getInstance(0,0);
262+
MPPointF p = MPPointF.getInstance(0, 0);
259263
for (int i = 0; i < maxEntryCount; i += xIncrements) {
260264

261265
Utils.getPosition(
@@ -275,8 +279,8 @@ protected void drawWeb(Canvas c) {
275279

276280
int labelCount = mChart.getYAxis().mEntryCount;
277281

278-
MPPointF p1out = MPPointF.getInstance(0,0);
279-
MPPointF p2out = MPPointF.getInstance(0,0);
282+
MPPointF p1out = MPPointF.getInstance(0, 0);
283+
MPPointF p2out = MPPointF.getInstance(0, 0);
280284
for (int j = 0; j < labelCount; j++) {
281285

282286
for (int i = 0; i < mChart.getData().getEntryCount(); i++) {
@@ -305,7 +309,7 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
305309
float factor = mChart.getFactor();
306310

307311
MPPointF center = mChart.getCenterOffsets();
308-
MPPointF pOut = MPPointF.getInstance(0,0);
312+
MPPointF pOut = MPPointF.getInstance(0, 0);
309313

310314
RadarData radarData = mChart.getData();
311315

@@ -362,6 +366,7 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
362366
}
363367

364368
protected Path mDrawHighlightCirclePathBuffer = new Path();
369+
365370
public void drawHighlightCircle(Canvas c,
366371
MPPointF point,
367372
float innerRadius,

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,12 @@ public void drawValues(Canvas c) {
101101

102102
IScatterDataSet dataSet = dataSets.get(i);
103103

104-
if (!shouldDrawValues(dataSet))
104+
if (dataSet.getEntryCount() == 0) {
105105
continue;
106+
}
107+
if (!shouldDrawValues(dataSet)) {
108+
continue;
109+
}
106110

107111
// apply the text-styling defined by the DataSet
108112
applyValueTextStyle(dataSet);

0 commit comments

Comments
 (0)