Skip to content

Commit b0caebe

Browse files
committed
Improved animations, bugfixes.
1 parent 21d0a67 commit b0caebe

File tree

7 files changed

+87
-108
lines changed

7 files changed

+87
-108
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected void onCreate(Bundle savedInstanceState) {
7070
mChart.setPinchZoom(false);
7171

7272
// draw shadows for each bar that show the maximum value
73-
mChart.setDrawBarShadow(true);
73+
// mChart.setDrawBarShadow(true);
7474

7575
mChart.setUnit(" €");
7676

@@ -243,7 +243,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
243243

244244
for (int i = 0; i < mSeekBarX.getProgress()+1; i++) {
245245
float mult = (mSeekBarY.getProgress() + 1);
246-
float val = (float) (Math.random() * mult) + 3;
246+
float val = (float) (Math.random() * mult) - 50;
247247
yVals1.add(new Entry(val, i));
248248
}
249249

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

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11

22
package com.github.mikephil.charting.charts;
33

4-
import android.animation.Animator;
5-
import android.animation.ObjectAnimator;
6-
import android.annotation.TargetApi;
74
import android.content.Context;
85
import android.graphics.Color;
96
import android.graphics.Paint;
10-
import android.graphics.Path;
117
import android.graphics.RectF;
12-
import android.os.Build;
138
import android.util.AttributeSet;
149
import android.util.Log;
1510

@@ -171,10 +166,10 @@ public void setData(BarData data) {
171166
protected void calcMinMax(boolean fixedValues) {
172167
super.calcMinMax(fixedValues);
173168

174-
if (!mStartAtZero && getYMin() >= 0f) {
175-
mYChartMin = getYMin();
176-
mDeltaY = Math.abs(mYChartMax - mYChartMin);
177-
}
169+
// if (!mStartAtZero && getYMin() >= 0f) {
170+
// mYChartMin = getYMin();
171+
// mDeltaY = Math.abs(mYChartMax - mYChartMin);
172+
// }
178173

179174
// increase deltax by 1 because the bars have a width of 1
180175
mDeltaX++;
@@ -186,9 +181,6 @@ protected void drawHighlights() {
186181
// if there are values to highlight and highlighnting is enabled, do it
187182
if (mHighlightEnabled && mHighLightIndicatorEnabled && valuesToHighlight()) {
188183

189-
// distance between highlight arrow and bar
190-
float offsetY = mDeltaY * 0.04f;
191-
192184
for (int i = 0; i < mIndicesToHightlight.length; i++) {
193185

194186
Highlight h = mIndicesToHightlight[i];
@@ -204,30 +196,26 @@ protected void drawHighlights() {
204196

205197
Entry e = getEntryByDataSetIndex(index, dataSetIndex);
206198

207-
float y = e.getSum() * mPhaseY;
208-
float top = y >= 0 ? y : 0;
209-
float bottom = y <= 0 ? y : 0;
210-
211-
float left = index + ds.getBarSpace() / 2f;
212-
float right = index + 1f - ds.getBarSpace() / 2f;
213-
214-
RectF highlight = new RectF(left, top, right, bottom);
215-
transformRect(highlight);
216-
217-
mDrawCanvas.drawRect(highlight, mHighlightPaint);
218-
219-
if (mDrawHighlightArrow) {
220-
221-
mHighlightPaint.setAlpha(200);
222-
223-
Path arrow = new Path();
224-
arrow.moveTo(index + 0.5f, y + offsetY * 0.3f);
225-
arrow.lineTo(index + 0.2f, y + offsetY);
226-
arrow.lineTo(index + 0.8f, y + offsetY);
227-
228-
transformPath(arrow);
229-
mDrawCanvas.drawPath(arrow, mHighlightPaint);
230-
}
199+
prepareBar(e.getXIndex(), e.getSum(), ds.getBarSpace());
200+
201+
mDrawCanvas.drawRect(mBarRect, mHighlightPaint);
202+
203+
// if (mDrawHighlightArrow) {
204+
//
205+
//
206+
// // distance between highlight arrow and bar
207+
// float offsetY = mDeltaY * 0.04f;
208+
//
209+
// mHighlightPaint.setAlpha(200);
210+
//
211+
// Path arrow = new Path();
212+
// arrow.moveTo(index + 0.5f, y + offsetY * 0.3f);
213+
// arrow.lineTo(index + 0.2f, y + offsetY);
214+
// arrow.lineTo(index + 0.8f, y + offsetY);
215+
//
216+
// transformPath(arrow);
217+
// mDrawCanvas.drawPath(arrow, mHighlightPaint);
218+
// }
231219
}
232220
}
233221
}
@@ -339,14 +327,15 @@ protected void drawData() {
339327
*/
340328
private void prepareBar(float x, float y, float space) {
341329

342-
float left = x + space / 2f;
343-
float right = x + 1f - space / 2f;
344-
float top = (y >= 0 ? y : 0) * mPhaseY;
345-
float bottom = (y <= 0 ? y : 0) * mPhaseY;
330+
float spaceHalf = space / 2f;
331+
float left = x + spaceHalf;
332+
float right = x + 1f - spaceHalf;
333+
float top = y >= 0 ? y : 0;
334+
float bottom = y <= 0 ? y : 0;
346335

347336
mBarRect.set(left, top, right, bottom);
348337

349-
transformRect(mBarRect);
338+
transformRectWithPhase(mBarRect);
350339

351340
// if a shadow is drawn, prepare it too
352341
if (mDrawBarShadow) {

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11

22
package com.github.mikephil.charting.charts;
33

4-
import android.animation.ObjectAnimator;
5-
import android.animation.ValueAnimator;
64
import android.annotation.TargetApi;
75
import android.content.Context;
86
import android.graphics.Canvas;
@@ -517,16 +515,25 @@ protected void calcMinMax(boolean fixedValues) {
517515

518516
// additional handling for space (default 10% space), spacing only
519517
// applies with non-rounded y-label
520-
float space = mDeltaY / 100f * 15f;
518+
float space = Math.abs(mDeltaY / 100f * 15f);
521519

522520
if (mStartAtZero) {
523-
mYChartMin = 0;
521+
522+
if (mYChartMax < 0) {
523+
mYChartMax = 0;
524+
// calc delta
525+
mYChartMin = mYChartMin - space;
526+
} else {
527+
mYChartMin = 0;
528+
// calc delta
529+
mYChartMax = mYChartMax + space;
530+
}
524531
} else {
525-
mYChartMin = mYChartMin - space;
532+
533+
mYChartMin = mYChartMin - space / 2f;
534+
mYChartMax = mYChartMax + space / 2f;
526535
}
527536

528-
// calc delta
529-
mYChartMax = mYChartMax + space;
530537
mDeltaY = Math.abs(mYChartMax - mYChartMin);
531538
}
532539

@@ -708,6 +715,7 @@ protected void drawYLabels() {
708715
transformPointArray(positions);
709716

710717
float xoffset = Utils.convertDpToPixel(5f);
718+
float yoffset = Utils.calcTextHeight(mYLabelPaint, "A") / 2.5f;
711719

712720
mYLabelPaint.setTypeface(mYLabels.getTypeface());
713721
mYLabelPaint.setTextSize(mYLabels.getTextSize());
@@ -716,22 +724,22 @@ protected void drawYLabels() {
716724
if (mYLabels.getPosition() == YLabelPosition.LEFT) {
717725

718726
mYLabelPaint.setTextAlign(Align.RIGHT);
719-
drawYLabels(mOffsetLeft - xoffset, positions);
727+
drawYLabels(mOffsetLeft - xoffset, positions, yoffset);
720728

721729
} else if (mYLabels.getPosition() == YLabelPosition.RIGHT) {
722730

723731
mYLabelPaint.setTextAlign(Align.LEFT);
724-
drawYLabels(getWidth() - mOffsetRight + xoffset, positions);
732+
drawYLabels(getWidth() - mOffsetRight + xoffset, positions, yoffset);
725733

726734
} else { // BOTH SIDED Y-AXIS LABELS
727735

728736
// draw left legend
729737
mYLabelPaint.setTextAlign(Align.RIGHT);
730-
drawYLabels(mOffsetLeft - xoffset, positions);
738+
drawYLabels(mOffsetLeft - xoffset, positions, yoffset);
731739

732740
// draw right legend
733741
mYLabelPaint.setTextAlign(Align.LEFT);
734-
drawYLabels(getWidth() - mOffsetRight + xoffset, positions);
742+
drawYLabels(getWidth() - mOffsetRight + xoffset, positions, yoffset);
735743
}
736744
}
737745

@@ -741,7 +749,7 @@ protected void drawYLabels() {
741749
* @param xPos
742750
* @param positions
743751
*/
744-
private void drawYLabels(float xPos, float[] positions) {
752+
private void drawYLabels(float xPos, float[] positions, float yOffset) {
745753

746754
// draw
747755
for (int i = 0; i < mYLabels.mEntryCount; i++) {
@@ -753,10 +761,10 @@ private void drawYLabels(float xPos, float[] positions) {
753761
return;
754762

755763
if (mYLabels.isDrawUnitsInYLabelEnabled()) {
756-
mDrawCanvas.drawText(text + mUnit, xPos, positions[i * 2 + 1],
764+
mDrawCanvas.drawText(text + mUnit, xPos, positions[i * 2 + 1] + yOffset,
757765
mYLabelPaint);
758766
} else {
759-
mDrawCanvas.drawText(text, xPos, positions[i * 2 + 1], mYLabelPaint);
767+
mDrawCanvas.drawText(text, xPos, positions[i * 2 + 1] + yOffset, mYLabelPaint);
760768
}
761769
}
762770
}

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

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -585,44 +585,6 @@ protected float[] generateTransformedValues(ArrayList<Entry> entries, float xOff
585585
return valuePoints;
586586
}
587587

588-
protected float[] generateTransformedValuesForStacks(ArrayList<Entry> entries, int entryCount,
589-
float xOffset) {
590-
591-
float[] valuePoints = new float[entryCount * 2];
592-
int cnt = 0;
593-
594-
for (int i = 0; i < entries.size(); i++) {
595-
596-
Entry e = entries.get(i);
597-
598-
float[] vals = e.getVals();
599-
600-
// if the current entry has no stack
601-
if (vals == null) {
602-
603-
valuePoints[cnt] = e.getXIndex() + xOffset;
604-
valuePoints[cnt + 1] = e.getVal();
605-
cnt += 2;
606-
} else {
607-
608-
float all = e.getSum();
609-
610-
for (int j = 0; j < vals.length; j++) {
611-
612-
all -= vals[j];
613-
614-
valuePoints[cnt] = e.getXIndex() + xOffset;
615-
valuePoints[cnt + 1] = vals[j] + all;
616-
cnt += 2;
617-
}
618-
}
619-
}
620-
621-
transformPointArray(valuePoints);
622-
623-
return valuePoints;
624-
}
625-
626588
/**
627589
* transform a path with all the given matrices VERY IMPORTANT: keep order
628590
* to value-touch-offset
@@ -673,6 +635,21 @@ protected void transformRect(RectF r) {
673635
mMatrixOffset.mapRect(r);
674636
}
675637

638+
/**
639+
* Transform a rectangle with all matrices with potential animation phases.
640+
*
641+
* @param r
642+
*/
643+
protected void transformRectWithPhase(RectF r) {
644+
645+
// multiply the height of the rect with the phase
646+
r.top *= mPhaseY;
647+
648+
mMatrixValueToPx.mapRect(r);
649+
mMatrixTouch.mapRect(r);
650+
mMatrixOffset.mapRect(r);
651+
}
652+
676653
/**
677654
* transforms multiple rects with all matrices
678655
*

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ protected void drawHighlights() {
8686

8787
int xIndex = mIndicesToHightlight[i].getXIndex(); // get the
8888
// x-position
89-
90-
if(xIndex > mDeltaX * mPhaseX) continue;
91-
92-
float y = set.getYValForXIndex(xIndex) * mPhaseY; // get the y-position
89+
90+
if (xIndex > mDeltaX * mPhaseX)
91+
continue;
92+
93+
float y = set.getYValForXIndex(xIndex) * mPhaseY; // get the
94+
// y-position
9395

9496
float[] pts = new float[] {
9597
xIndex, mYChartMax, xIndex, mYChartMin, 0, y, mDeltaX, y
@@ -197,15 +199,21 @@ && isOffContentBottom(valuePoints[j + 1]))
197199
}
198200
}
199201
}
200-
202+
203+
/**
204+
* Generates the path that is used for filled drawing.
205+
*
206+
* @param entries
207+
* @return
208+
*/
201209
private Path generateFilledPath(ArrayList<Entry> entries) {
202-
210+
203211
Path filled = new Path();
204212
filled.moveTo(entries.get(0).getXIndex(), entries.get(0).getVal() * mPhaseY);
205213

206214
// create a new path
207215
for (int x = 1; x < entries.size() * mPhaseX; x++) {
208-
216+
209217
Entry e = entries.get(x);
210218
filled.lineTo(e.getXIndex(), e.getVal() * mPhaseY);
211219
}
@@ -214,7 +222,7 @@ private Path generateFilledPath(ArrayList<Entry> entries) {
214222
filled.lineTo(entries.get((int) ((entries.size() - 1) * mPhaseX)).getXIndex(), mYChartMin);
215223
filled.lineTo(entries.get(0).getXIndex(), mYChartMin);
216224
filled.close();
217-
225+
218226
return filled;
219227
}
220228

@@ -326,7 +334,7 @@ protected void drawAdditional() {
326334
dataSet.getCircleSize() / 2,
327335
mCirclePaintInner);
328336
}
329-
} // else do nothing
337+
} // else do nothing
330338

331339
}
332340
}

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

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

22
package com.github.mikephil.charting.charts;
33

4-
import android.animation.ValueAnimator;
54
import android.content.Context;
65
import android.graphics.Canvas;
76
import android.graphics.Color;
@@ -11,7 +10,6 @@
1110
import android.graphics.PointF;
1211
import android.graphics.RectF;
1312
import android.graphics.Typeface;
14-
import android.os.Handler;
1513
import android.util.AttributeSet;
1614
import android.util.Log;
1715
import android.view.MotionEvent;
@@ -22,7 +20,6 @@
2220
import com.github.mikephil.charting.data.PieDataSet;
2321
import com.github.mikephil.charting.listener.PieChartTouchListener;
2422
import com.github.mikephil.charting.utils.Legend.LegendPosition;
25-
import com.github.mikephil.charting.utils.XLabels.XLabelPosition;
2623
import com.github.mikephil.charting.utils.Utils;
2724

2825
import java.text.DecimalFormat;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ protected void drawValues() {
137137

138138
float shapeSize = dataSet.getScatterShapeSize();
139139

140-
for (int j = 0; j < positions.length; j += 2) {
140+
for (int j = 0; j < positions.length * mPhaseX; j += 2) {
141141

142142
if (isOffContentRight(positions[j]))
143143
break;

0 commit comments

Comments
 (0)