Skip to content

Commit

Permalink
Fix IndexOutOfBoundsException thrown when drawing data set values (Ph…
Browse files Browse the repository at this point in the history
…ilJay#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:
PhilJay#2450
PhilJay#2455
PhilJay#2849
PhilJay#2869
PhilJay#2952
PhilJay#3011
PhilJay#3124
PhilJay#3171
PhilJay#3213
PhilJay#3361
PhilJay#3566
PhilJay#3623
PhilJay#3696
PhilJay#3755
PhilJay#4015
  • Loading branch information
RobertZagorski authored and hannesa2 committed Jul 31, 2018
1 parent 32aef31 commit 502767e
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,12 @@ public void drawValues(Canvas c) {
for (int i = 0; i < mChart.getBarData().getDataSetCount(); i++) {

IBarDataSet dataSet = dataSets.get(i);

if (!shouldDrawValues(dataSet))
if (dataSet.getEntryCount() == 0) {
continue;
}
if (!shouldDrawValues(dataSet)) {
continue;
}

// apply the text-styling defined by the DataSet
applyValueTextStyle(dataSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ public void drawValues(Canvas c) {
for (int i = 0; i < dataSets.size(); i++) {

IBubbleDataSet dataSet = dataSets.get(i);

if (!shouldDrawValues(dataSet))
if (dataSet.getEntryCount() == 0) {
continue;
}
if (!shouldDrawValues(dataSet)) {
continue;
}

// apply the text-styling defined by the DataSet
applyValueTextStyle(dataSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,12 @@ public void drawValues(Canvas c) {
for (int i = 0; i < dataSets.size(); i++) {

ICandleDataSet dataSet = dataSets.get(i);

if (!shouldDrawValues(dataSet))
if (dataSet.getEntryCount() == 0) {
continue;
}
if (!shouldDrawValues(dataSet)) {
continue;
}

// apply the text-styling defined by the DataSet
applyValueTextStyle(dataSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
final float barWidthHalf = barWidth / 2.0f;
float x;

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

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

IBarDataSet dataSet = dataSets.get(i);

if (!shouldDrawValues(dataSet))
if (dataSet.getEntryCount() == 0) {
continue;
}
if (!shouldDrawValues(dataSet)) {
continue;
}

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

Expand Down Expand Up @@ -229,8 +232,8 @@ public void drawValues(Canvas c) {
Utils.drawImage(
c,
icon,
(int)px,
(int)py,
(int) px,
(int) py,
icon.getIntrinsicWidth(),
icon.getIntrinsicHeight());
}
Expand Down Expand Up @@ -300,8 +303,8 @@ public void drawValues(Canvas c) {
Utils.drawImage(
c,
icon,
(int)px,
(int)py,
(int) px,
(int) py,
icon.getIntrinsicWidth(),
icon.getIntrinsicHeight());
}
Expand Down Expand Up @@ -378,8 +381,8 @@ public void drawValues(Canvas c) {
Utils.drawImage(
c,
icon,
(int)(x + iconsOffset.x),
(int)(y + iconsOffset.y),
(int) (x + iconsOffset.x),
(int) (y + iconsOffset.y),
icon.getIntrinsicWidth(),
icon.getIntrinsicHeight());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,12 @@ public void drawValues(Canvas c) {
for (int i = 0; i < dataSets.size(); i++) {

ILineDataSet dataSet = dataSets.get(i);

if (!shouldDrawValues(dataSet))
if (dataSet.getEntryCount() == 0) {
continue;
}
if (!shouldDrawValues(dataSet)) {
continue;
}

// apply the text-styling defined by the DataSet
applyValueTextStyle(dataSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,13 @@ public void drawValues(Canvas c) {
for (int i = 0; i < dataSets.size(); i++) {

IPieDataSet dataSet = dataSets.get(i);

if (dataSet.getEntryCount() == 0) {
continue;
}
final boolean drawValues = dataSet.isDrawValuesEnabled();

if (!drawValues && !drawEntryLabels)
if (!drawValues && !drawEntryLabels) {
continue;
}

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

mValuePaint.setTextAlign(Align.RIGHT);

if(drawXOutside)
if (drawXOutside)
mEntryLabelsPaint.setTextAlign(Align.RIGHT);

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

if(drawXOutside)
if (drawXOutside)
mEntryLabelsPaint.setTextAlign(Align.LEFT);

labelPtx = pt2x + offset;
Expand Down Expand Up @@ -611,8 +613,8 @@ public void drawValues(Canvas c) {
Utils.drawImage(
c,
icon,
(int)x,
(int)y,
(int) x,
(int) y,
icon.getIntrinsicWidth(),
icon.getIntrinsicHeight());
}
Expand Down Expand Up @@ -690,6 +692,7 @@ protected void drawHole(Canvas c) {
}

protected Path mDrawCenterTextPathBuffer = new Path();

/**
* draws the description text in the center of the pie chart makes most
* sense when center-hole is enabled
Expand Down Expand Up @@ -763,6 +766,7 @@ protected void drawCenterText(Canvas c) {
}

protected RectF mDrawHighlightedRectF = new RectF();

@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {

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

final RectF highlightedCircleBox = mDrawHighlightedRectF;
highlightedCircleBox.set(0,0,0,0);
highlightedCircleBox.set(0, 0, 0, 0);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void drawData(Canvas c) {
}

protected Path mDrawDataSetSurfacePathBuffer = new Path();

/**
* Draws the RadarDataSet
*
Expand All @@ -89,7 +90,7 @@ protected void drawDataSet(Canvas c, IRadarDataSet dataSet, int mostEntries) {
float factor = mChart.getFactor();

MPPointF center = mChart.getCenterOffsets();
MPPointF pOut = MPPointF.getInstance(0,0);
MPPointF pOut = MPPointF.getInstance(0, 0);
Path surface = mDrawDataSetSurfacePathBuffer;
surface.reset();

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

MPPointF center = mChart.getCenterOffsets();
MPPointF pOut = MPPointF.getInstance(0,0);
MPPointF pIcon = MPPointF.getInstance(0,0);
MPPointF pOut = MPPointF.getInstance(0, 0);
MPPointF pIcon = MPPointF.getInstance(0, 0);

float yoffset = Utils.convertDpToPixel(5f);

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

IRadarDataSet dataSet = mChart.getData().getDataSetByIndex(i);

if (!shouldDrawValues(dataSet))
if (dataSet.getEntryCount() == 0) {
continue;
}
if (!shouldDrawValues(dataSet)) {
continue;
}

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

RadarEntry entry = dataSet.getEntryForIndex(j);

Utils.getPosition(
center,
(entry.getY() - mChart.getYChartMin()) * factor * phaseY,
sliceangle * j * phaseX + mChart.getRotationAngle(),
pOut);
Utils.getPosition(
center,
(entry.getY() - mChart.getYChartMin()) * factor * phaseY,
sliceangle * j * phaseX + mChart.getRotationAngle(),
pOut);

if (dataSet.isDrawValuesEnabled()) {
drawValue(c,
Expand Down Expand Up @@ -216,8 +220,8 @@ public void drawValues(Canvas c) {
Utils.drawImage(
c,
icon,
(int)pIcon.x,
(int)pIcon.y,
(int) pIcon.x,
(int) pIcon.y,
icon.getIntrinsicWidth(),
icon.getIntrinsicHeight());
}
Expand Down Expand Up @@ -255,7 +259,7 @@ protected void drawWeb(Canvas c) {
final int xIncrements = 1 + mChart.getSkipWebLineCount();
int maxEntryCount = mChart.getData().getMaxEntryCountSet().getEntryCount();

MPPointF p = MPPointF.getInstance(0,0);
MPPointF p = MPPointF.getInstance(0, 0);
for (int i = 0; i < maxEntryCount; i += xIncrements) {

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

int labelCount = mChart.getYAxis().mEntryCount;

MPPointF p1out = MPPointF.getInstance(0,0);
MPPointF p2out = MPPointF.getInstance(0,0);
MPPointF p1out = MPPointF.getInstance(0, 0);
MPPointF p2out = MPPointF.getInstance(0, 0);
for (int j = 0; j < labelCount; j++) {

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

MPPointF center = mChart.getCenterOffsets();
MPPointF pOut = MPPointF.getInstance(0,0);
MPPointF pOut = MPPointF.getInstance(0, 0);

RadarData radarData = mChart.getData();

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

protected Path mDrawHighlightCirclePathBuffer = new Path();

public void drawHighlightCircle(Canvas c,
MPPointF point,
float innerRadius,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ public void drawValues(Canvas c) {

IScatterDataSet dataSet = dataSets.get(i);

if (!shouldDrawValues(dataSet))
if (dataSet.getEntryCount() == 0) {
continue;
}
if (!shouldDrawValues(dataSet)) {
continue;
}

// apply the text-styling defined by the DataSet
applyValueTextStyle(dataSet);
Expand Down

0 comments on commit 502767e

Please sign in to comment.