Skip to content

Commit

Permalink
Removed duplicate code
Browse files Browse the repository at this point in the history
Testing for `mInverted` in the main `if` or inner `if` do not have a meaning performance-wise, as in both cases it's just one boolean check.
  • Loading branch information
danielgindi committed Jun 24, 2015
1 parent a9e3350 commit 932405a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 162 deletions.
124 changes: 41 additions & 83 deletions MPChartLib/src/com/github/mikephil/charting/buffer/BarBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,109 +60,67 @@ public void feed(List<BarEntry> entries) {
+ mGroupSpace * i + groupSpaceHalf;
float y = e.getVal();
float [] vals = e.getVals();

if(mInverted) { // inverted axis, here, I chose performance over readability

if(!mContainsStacks || vals == null) {

float left = x - barWidth + barSpaceHalf;
float right = x + barWidth - barSpaceHalf;
float bottom = y >= 0 ? y : 0;
float top = y <= 0 ? y : 0;

// multiply the height of the rect with the phase
if (top > 0)
top *= phaseY;
else
bottom *= phaseY;

addBar(left, top, right, bottom);

if (!mContainsStacks || vals == null) {

float left = x - barWidth + barSpaceHalf;
float right = x + barWidth - barSpaceHalf;
float bottom, top;
if (mInverted) {
bottom = y >= 0 ? y : 0;
top = y <= 0 ? y : 0;
} else {
top = y >= 0 ? y : 0;
bottom = y <= 0 ? y : 0;
}

float allPos = e.getPositiveSum();
float allNeg = e.getNegativeSum();
// multiply the height of the rect with the phase
if (top > 0)
top *= phaseY;
else
bottom *= phaseY;

// fill the stack
for (int k = 0; k < vals.length; k++) {
addBar(left, top, right, bottom);

float value = vals[k];
} else {

if(value >= 0f) {
float allPos = e.getPositiveSum();
float allNeg = e.getNegativeSum();

allPos -= value;
y = value + allPos;
} else {
allNeg -= Math.abs(value);
y = value + allNeg;
}
// fill the stack
for (int k = 0; k < vals.length; k++) {

float left = x - barWidth + barSpaceHalf;
float right = x + barWidth - barSpaceHalf;
float bottom = y >= 0 ? y : 0;
float top = y <= 0 ? y : 0;
float value = vals[k];

// multiply the height of the rect with the phase
if (top > 0)
top *= phaseY;
else
bottom *= phaseY;
if(value >= 0f) {

addBar(left, top, right, bottom);
allPos -= value;
y = value + allPos;
} else {
allNeg -= Math.abs(value);
y = value + allNeg;
}
}

} else { // non inverted axis

if(!mContainsStacks || vals == null) {


float left = x - barWidth + barSpaceHalf;
float right = x + barWidth - barSpaceHalf;
float top = y >= 0 ? y : 0;
float bottom = y <= 0 ? y : 0;

float bottom, top;
if (mInverted) {
bottom = y >= 0 ? y : 0;
top = y <= 0 ? y : 0;
} else {
top = y >= 0 ? y : 0;
bottom = y <= 0 ? y : 0;
}

// multiply the height of the rect with the phase
if (top > 0)
top *= phaseY;
else
bottom *= phaseY;

addBar(left, top, right, bottom);

} else {

float allPos = e.getPositiveSum();
float allNeg = e.getNegativeSum();

// fill the stack
for (int k = 0; k < vals.length; k++) {

float value = vals[k];

if(value >= 0f) {

allPos -= value;
y = value + allPos;
} else {
allNeg -= Math.abs(value);
y = value + allNeg;
}

float left = x - barWidth + barSpaceHalf;
float right = x + barWidth - barSpaceHalf;
float top = y >= 0 ? y : 0;
float bottom = y <= 0 ? y : 0;

// multiply the height of the rect with the phase
if (top > 0)
top *= phaseY;
else
bottom *= phaseY;

addBar(left, top, right, bottom);
}
}
}
}
}
}

reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,65 +31,56 @@ public void feed(List<BarEntry> entries) {
float y = e.getVal();
float[] vals = e.getVals();

if(mInverted) { // inverted axis

if (!mContainsStacks || vals == null) {

float bottom = x - barWidth + barSpaceHalf;
float top = x + barWidth - barSpaceHalf;
float left = y >= 0 ? y : 0;
float right = y <= 0 ? y : 0;

// multiply the height of the rect with the phase
if (right > 0)
right *= phaseY;
else
left *= phaseY;

addBar(left, top, right, bottom);

if (!mContainsStacks || vals == null) {

float bottom = x - barWidth + barSpaceHalf;
float top = x + barWidth - barSpaceHalf;
float left, right;
if (mInverted) {
left = y >= 0 ? y : 0;
right = y <= 0 ? y : 0;
} else {
right = y >= 0 ? y : 0;
left = y <= 0 ? y : 0;
}

float allPos = e.getPositiveSum();
float allNeg = e.getNegativeSum();
// multiply the height of the rect with the phase
if (right > 0)
right *= phaseY;
else
left *= phaseY;

// fill the stack
for (int k = 0; k < vals.length; k++) {
addBar(left, top, right, bottom);

float value = vals[k];
} else {

if(value >= 0f) {
float allPos = e.getPositiveSum();
float allNeg = e.getNegativeSum();

allPos -= value;
y = value + allPos;
} else {
allNeg -= Math.abs(value);
y = value + allNeg;
}
// fill the stack
for (int k = 0; k < vals.length; k++) {

float bottom = x - barWidth + barSpaceHalf;
float top = x + barWidth - barSpaceHalf;
float left = y >= 0 ? y : 0;
float right = y <= 0 ? y : 0;
float value = vals[k];

// multiply the height of the rect with the phase
if (right > 0)
right *= phaseY;
else
left *= phaseY;
if(value >= 0f) {

addBar(left, top, right, bottom);
allPos -= value;
y = value + allPos;
} else {
allNeg -= Math.abs(value);
y = value + allNeg;
}
}

} else { // not inverted

if (!mContainsStacks || vals == null) {

float bottom = x - barWidth + barSpaceHalf;
float top = x + barWidth - barSpaceHalf;
float right = y >= 0 ? y : 0;
float left = y <= 0 ? y : 0;
float left, right;
if (mInverted) {
left = y >= 0 ? y : 0;
right = y <= 0 ? y : 0;
} else {
right = y >= 0 ? y : 0;
left = y <= 0 ? y : 0;
}

// multiply the height of the rect with the phase
if (right > 0)
Expand All @@ -98,39 +89,6 @@ public void feed(List<BarEntry> entries) {
left *= phaseY;

addBar(left, top, right, bottom);

} else {

float allPos = e.getPositiveSum();
float allNeg = e.getNegativeSum();

// fill the stack
for (int k = 0; k < vals.length; k++) {

float value = vals[k];

if(value >= 0f) {

allPos -= value;
y = value + allPos;
} else {
allNeg -= Math.abs(value);
y = value + allNeg;
}

float bottom = x - barWidth + barSpaceHalf;
float top = x + barWidth - barSpaceHalf;
float right = y >= 0 ? y : 0;
float left = y <= 0 ? y : 0;

// multiply the height of the rect with the phase
if (right > 0)
right *= phaseY;
else
left *= phaseY;

addBar(left, top, right, bottom);
}
}
}
}
Expand Down

0 comments on commit 932405a

Please sign in to comment.